Step 3: Defining interfaces and implementations
Now you need to define the interfaces and implementations for your application and associate them with the composite diagram:
Defining interfaces and implementations
To define interfaces and implementations for your application:
-
In the src
directory of the Restaurant project create a new package named restaurant
with two sub-packages named api
and lib
.

-
Define the interfaces of the services in the restaurant.api
package:
-
Restaurant service:
-
Menu service:
-
package restaurant.api;
public interface MenuService {
Menu[] getMenu();
double getPrice(Menu menu);
}
|
-
Bill service:
-
package restaurant.api;
public interface BillService {
double getBill(double menuPrice);
}
|
-
Vat Service:
-
package restaurant.api;
public interface VatService {
double getPriceWithVat(double price);
}
|
-
Tip Service:
-
package restaurant.api;
public interface TipService {
double getPriceWithTip(double price);
}
|
-
Define the interface of the Menu (Data Transfer Object):
package restaurant.api;
import java.io.Serializable;
public interface Menu extends Serializable {
String printMenu();
}
|
-
Define the implementations restaurant.lib
package.
-
Restaurant Service:
-
package restaurant.lib;
import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Service;
import restaurant.api.BillService;
import restaurant.api.Menu;
import restaurant.api.MenuService;
import restaurant.api.RestaurantService;
@Service(RestaurantService.class)
public class RestaurantServiceImpl implements RestaurantService {
private MenuService menuService;
private BillService billService;
@Reference
public void setMenuService(MenuService menuService) {
this.menuService = menuService;
}
@Reference
public void setBillService(BillService billService) {
this.billService = billService;
}
public double getBill(Menu menu) {
double menuPrice = this.menuService.getPrice(menu);
return this.billService.getBill(menuPrice);
}
public Menu[] getMenus() {
return this.menuService.getMenu();
}
}
|
- Menu Service:
package restaurant.lib;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Service;
import restaurant.api.Menu;
import restaurant.api.MenuService;
@Service(MenuService.class)
public class MenuServiceImpl implements MenuService {
private Menu[] menus;
private double[] prices;
@Init
public void init() {
this.menus = new Menu[] {
new MenuImpl(0, "Grilled hamburger with French fries" ),
new MenuImpl(1, "Roasted chicken with vegetables"),
new MenuImpl(2, "Duck breast in an orange sauce"),
new MenuImpl(3, "Duck foie gras & mango chutney") };
this.prices = new double[] { 10, 15, 35, 50 };
}
public Menu[] getMenu() {
return this.menus;
}
public double getPrice(Menu menu) {
return this.prices[((MenuImpl) menu).getId()];
}
}
|
- Bill Service:
package restaurant.lib;
import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Service;
import restaurant.api.BillService;
import restaurant.api.TipService;
import restaurant.api.VatService;
@Service(BillService.class)
public class BillServiceImpl implements BillService {
private VatService vatService;
private TipService tipService;
@Reference
public void setVatService(VatService vatService) {
this.vatService = vatService;
}
@Reference
public void setTipService(TipService tipService) {
this.tipService = tipService;
}
public double getBill(double menuPrice) {
double pricewithTaxRate =
this.vatService.getPriceWithVat(menuPrice);
double priceWithTipRate =
this.tipService.getPriceWithTip(pricewithTaxRate);
return priceWithTipRate;
}
}
|
- Vat Service:
package restaurant.lib;
import org.osoa.sca.annotations.Service;
import restaurant.api.VatService;
@Service(VatService.class)
public class VatServiceImpl implements VatService {
public double vatRate;
public VatServiceImpl(){
this.vatRate=19.6;
}
public double getPriceWithVat(double price) {
return price * this.vatRate/100 + price;
}
}
|
-
Tip Service:
-
package restaurant.lib;
import org.osoa.sca.annotations.Property;
import org.osoa.sca.annotations.Service;
import restaurant.api.TipService;
@Service(TipService.class)
public class TipServiceImpl implements TipService {
@Property
public double tipRate;
public TipServiceImpl(){
this.tipRate=10;
}
public double getPriceWithTip(double price) {
return price * this.tipRate/100 + price;
}
}
|
-
Menu:
-
package restaurant.lib;
import restaurant.api.Menu;
public class MenuImpl implements Menu {
private int id;
private String details;
MenuImpl(int idC, String detailsC) {
this.id = idC;
this.details = detailsC;
}
public String printMenu() {
return this.details;
}
public int getId() {
return this.id;
}
}
|
Associating interfaces and implementations with the diagram
Complete your SCA assembly file with the interfaces and implementations that you defined.
Open the Restaurant.composite_diagram with the SCA Composite Designer.
You have two ways to fill your SCA assembly file:
- Drag and drop the Java interface files on the services of your diagram and the Java implementation files on the components. The name of these elements are automatically set.

- Use the JavaInterface and the JavaImplementation creation tools from the palette or the contextual menu. For each created element, you must set (in the Properties view):
- the Interface attribute for a Java interface
- the Class attribute for a Java implementation

Now, your SCA assembly diagram looks like:

and, your SCA assembly file looks like:

Right-click on the canvas. The context menu allows you to:
- Validate your diagram
- Hide/show implementation, interface and binding icons

You have finished developing your first SCA application with the SCA Composite Designer. Now you are ready to test the application.