SCA概念与应用实践(5. 一个例子演示)

5.         一个例子演示

 

 

这个例子参照http://wiki.eclipse.org/STP/SCA_Component/SCA_First_Steps_With_Composite_Designer, 但过程有些不同。

 

5.1.        Application定义

下图是我们要实现的application

5_1

 

图5_1

这个只有一层composite, 命名为 restaurant,

它包含5component

    RestaurantServiceComponent

        显示餐馆菜单

        结帐账单.

    MenuServiceComponent

        提供菜单名字和价格.

    BillServiceComponent

        计算账单,包括消费税和小费.

    VATServiceComponent

        计算包含消费税的账单价格.

    TipServiceComponent

        计算小费的价格.

 

5.2.        过程

5.2.1.       创建一个java project

Ø        选择菜单 New > Java Project.

Ø        Project 名字为"Restaurant2". 选择 “Create separate folders for sources and class files”,点击next

Ø        Libraries里面,添加User Library TUSCANY

5_2

              图5_2

Ø        点击”next””finish”.

 

5.3.        创建SCA Composite diagram

Ø        project上右键,选择 New > Other....

Ø        SCA目录下,选择 SCA Composite Diagram,点击next

          5_3   

图5_3

Ø        文件名字设为Restaurant2.composite_diagram,点击 Finish.

 

完成后SCA Composite Designer自动打开新创建的文件. diagram上右键,选择Show Properties 来打开Properties view

 

5_4

图5_4

5.4.        添加component

使用palette创建component

5_5

图5_5

5_6

图5_6

 

 

 

填入RestaurantServiceComponent作为component的名字。同样创建其他的compoentMenuServiceComponent, BillServiceComponent, VatServiceComponent, and TipServiceComponent.

5_7

 

图5_7 

5.5.        添加Service

       有两种方式给component添加service

       一个是使用palette,在palette上选择service,然后在要添加的component上点一下,一个service就添加到component

       第二个方法是把鼠标移到component上,一个工具条自动就出现,选择绿色的那个。

 5_8

图5_8

    * RestaurantServiceComponent上添加 RestaurantService,

    *MenuServiceComponent上添加MenuService,

    *BillServiceComponent上添加BillService,

    *VatServiceComponent上添加VatService,

    *TipServiceComponent上添加TipService.

 5_9

图5_9 

5.6.        添加References

类似方法给component添加reference

    * RestaurantServiceComponent上添加menuService,

    * RestaurantServiceComponent上添加billService,

    * BillServiceComponent上添加vatService,

* BillServiceComponent上添加tipService

5_10

图5_10 

5.7.        wire referenceservice

palette上的wirereferenceservice连起来

RestaurantServiceComponent上的referencemenuService)连到MenuServiceComponentserviceMenuService),以此类推,把所有reference都连到相应service

 5_11

图5_11

5.8.        Promote service

       RestaurantServiceComponent上的serivceRestaurantServicepromotecomposite层,这样外面就可以调用这个application提供的功能。

       RestaurantService绿色图标上右键,选择promot

5_12

图5_12

 

5.9.        SCA Composite Model Editor打开Restaurant2.comosite,在Restaurant2.comosite 右键选open with

 5_13

 图5_13

Text Editor打开

 5_14

图5_14 

可以在这两个editor里编辑composite,会自动同步

 

5.10.     service定义java interface

把鼠标移到RestaurantServiceComponent上的service上,会自动有一个工具条出现,选择add JavaInterface

 5_15

图5_15

选中新添加的JavaInterface图标, properties中设置Interface的类名,包括packagerestaurant2.api.RestaurantService

 5_16

图5_16

以此类推给所有service添加JavaInterface

名字分别为restaurant2.api.MenuService

restaurant2.api.BillService

restaurant2.api.VatService

restaurant2.api.TipService

 5_17

图5_17

5.11.     component定义java implementation class的名字

鼠标移到RestaurantServiceComponent上,会自动有一个工具条出现,选择add JavaImplementation

 5_18

 

图5_18

选中新添加的JavaImplementation图标, properties中设置class,包括packagerestaurant2.lib.RestaurantServiceImpl

 5_19

 

图5_19

以此类推,给所有component添加JavaImplementation 类名分别为

restaurant2.lib.MenuServiceImpl

restaurant2.lib.BillServiceImpl

restaurant2.lib.VatServiceImpl

restaurant2.lib.TipServiceImpl

 5_20

图5_20 

5.12.     生成代码框架

首先在project上右键选择 Add SCA Nature

 5_21

图5_21

然后在Restaurant2.composite上右键 选择SCA>Generate Java Skeleton…

 5_22

图5_22

出现确认窗口,点ok

 5_23

图5_23

生成后项目结构是

 5_24

图5_24

生成的代码只有一个空壳,要在里面添加方法定义和实现。生成的代码的样子

 

 

 

5.13.     接口类的定义

  • Restaurant Service
  
 
  • Menu Service
  
  • Bill Service
  
  • Vat Service
  
  • Tip Service
  

添加DTO Menu:

 

5.14.        实现类
  • Restaurant Service
  
 
  • Menu Service

 

 
  • Bill service
  
  • Vat Service

  • Tip service

5.15.      测试 Restaurant

创建一个新的测试类 Client。这个client会把Restaurant2发布到tuscany里面,然后调用promote出来的那个serviceinterface

 

5.16.        运行client:

class Client上右键选择【Run as > Java Application

运行结果为

5_25

图5_25

 到此一个简单的application就开发完了。后面会使用这个例子,结合SCA里面的概念仔细说明

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值