SCA初探

   SCA其实有点象SPRING,只不过sca注入的是一个个实在的服务而已,快要成为工业标准了。下面笔记DEMO中的一个典型计算器

加法的接口:

package calculator;

/**
 * The Add service interface
 */
public interface AddService {

    double add(double n1, double n2);

}

加法的实现:

package calculator;

/**
 * An implementation of the Add service
 */
public class AddServiceImpl implements AddService {

    public double add(double n1, double n2) {
        return n1 + n2;
    }

}

减法的接口:

package calculator;

/**
 * The interface for the multiply service
 */
public interface SubtractService {

    double subtract(double n1, double n2);

}
减法的实现:

package calculator;

/**
 * An implementation of the subtract service.
 */
public class SubtractServiceImpl implements SubtractService {

    public double subtract(double n1, double n2) {
        return n1 - n2;
    }

}

乘法的接口:

package calculator;

/**
 * The interface for the multiply service
 */
public interface MultiplyService {

    double multiply(double n1, double n2);

}

乘法的实现:

package calculator;

/**
 * An implementation of the Multiply service.
 */
public class MultiplyServiceImpl implements MultiplyService {

    public double multiply(double n1, double n2) {
        return n1 * n2;
    }

}

除法的接口:

package calculator;

/**
 * The divide service interface
 */
public interface DivideService {

    double divide(double n1, double n2);

}


除法的实现:

package calculator;

/**
 * An implementation of the Divide service.
 */
public class DivideServiceImpl implements DivideService {

    public double divide(double n1, double n2) {
        return n1 / n2;
    }


}

计算器的接口

package calculator;


/**
 * The Calculator service interface.
 */
public interface CalculatorService {

    double add(double n1, double n2);

    double subtract(double n1, double n2);

    double multiply(double n1, double n2);

    double divide(double n1, double n2);

}

计算器的实现

package calculator;

import org.osoa.sca.annotations.Reference;


/**
 * An implementation of the Calculator service.
 */
public class CalculatorServiceImpl implements CalculatorService {

    private AddService addService;
    private SubtractService subtractService;
    private MultiplyService multiplyService;
    private DivideService divideService;

    @Reference
    public void setAddService(AddService addService) {
        this.addService = addService;
    }

    @Reference
    public void setSubtractService(SubtractService subtractService) {
        this.subtractService = subtractService;
    }

    @Reference
    public void setDivideService(DivideService divideService) {
        this.divideService = divideService;
    }

    @Reference
    public void setMultiplyService(MultiplyService multiplyService) {
        this.multiplyService = multiplyService;
    }

    public double add(double n1, double n2) {
        return addService.add(n1, n2);
    }

    public double subtract(double n1, double n2) {
        return subtractService.subtract(n1, n2);
    }

    public double multiply(double n1, double n2) {
        return multiplyService.multiply(n1, n2);
    }

    public double divide(double n1, double n2) {
        return divideService.divide(n1, n2);
    }

}

calculator.composite组装文件

<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
           targetNamespace="http://sample"
           xmlns:sample="http://sample"
           name="Calculator">

    <component name="CalculatorServiceComponent">
  <implementation.java class="calculator.CalculatorServiceImpl"/>
        <reference name="addService" target="AddServiceComponent" />
        <reference name="subtractService" target="SubtractServiceComponent" />
        <reference name="multiplyService" target="MultiplyServiceComponent" />
        <reference name="divideService" target="DivideServiceComponent" />
    </component>

    <component name="AddServiceComponent">
        <implementation.java class="calculator.AddServiceImpl"/>
    </component>

    <component name="SubtractServiceComponent">
        <implementation.java class="calculator.SubtractServiceImpl"/>
    </component>

    <component name="MultiplyServiceComponent">
        <implementation.java class="calculator.MultiplyServiceImpl"/>
    </component>

    <component name="DivideServiceComponent">
        <implementation.java class="calculator.DivideServiceImpl"/>
    </component>

</composite>

客户端使用

package calculator;

import org.apache.tuscany.sca.host.embedded.SCADomain;

/**
 * This client program shows how to create an SCA runtime, start it,
 * and locate and invoke a SCA component
 */
public class CalculatorClient {
    public static void main(String[] args) throws Exception {

        SCADomain scaDomain = SCADomain.newInstance("Calculator.composite");
       
        CalculatorService calculatorService =
            scaDomain.getService(CalculatorService.class, "CalculatorServiceComponent");

        // Calculate
        System.out.println("3 + 2=" + calculatorService.add(3, 2));
        System.out.println("3 - 2=" + calculatorService.subtract(3, 2));
        System.out.println("3 * 2=" + calculatorService.multiply(3, 2));
        System.out.println("3 / 2=" + calculatorService.divide(3, 2));

        scaDomain.close();
    }

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值