Recall that in the Model-View-Controller architecture views send commands to controllers. For example, when the user selects an item on a menu, a command such as "print" or "save" is sent to a controller. It is the job of the controller to interact with the model in order to execute the command.
The Use Case Controller design pattern maps use cases to controllers. In this project we explore the possibility of formalizing this mapping as a Java-based code generator called R2C.
Alistair Cockburn's Hexagonal architecture is a symmetric reworking of the three-layer architecture. The model is surrounded by a hexagon. We can think of the edges of the hexagon as interfaces to the model. An interface consists of one or more ports. Cockburn's ports can be use case controllers. The hexagon is surrounded by actors. These actors might be real or mock, primary (initiators) or secondary (responders). The arrows can be thought of as adapters (see the Adapter Design Pattern) that translate the actor's messages into something that the port can recognize.
Assume a model is anything that is serializable:
interface Model implements java.io.Serializable { }
Assume an oracle is anything that can answer queries:
interface Oracle {
String ask(Oracle asker, String query)
throws Exception;
}
An actor is an oracle:
interface Actor extends Oracle { }
A use case controller is an oracle that implements an execute operation:
interface UseCaseController extends Oracle {
String execute(Oracle caller, String
cmmd) throws Exception;
}
Assume an e-store has a GetDiscount use case:
Here's the main scenario:
Note that user initiates the use case by calling the execute method. The scenario ends when the discount is returned to the user. In between, several queries are asked and answered. Asking is shown using a send arrow and answering is shown using a returns arrow. Except for calling the execute method, all communication between actors and use cases is done by sending and returning.
XPD is an XML language used by StarUML to store UML projects as files. These files have a ".uml" extension. XPD is discussed here:
http://www.cs.sjsu.edu/faculty/pearce/oom/ood/codeGen/codeGen.htm
Each arrow in the above sequence diagram appears in XPD as a "participating stimuli".
Parsing the .uml file the R2C code generator generates the following use case controller:
class GetDiscount implements UseCaseController {
private Actor aDbase;
private Model model;
public void setADbase(Actor a) { aDbase
= a; }
public void setModel(Model m) { model =
m; }
public String ask(Oracle asker, String
query) throws Exception {
// TO DO: complete this
implementation
if
(query.equals("amount?")) {
return null;
} else {
return null;
}
}
public String execute(Oracle o, String
cmmd) throws Exception {
// TO DO: complete this
implementation
String discount = null;
String amount = o.ask(this,
"amount?");
String rate = dDase.ask(this,
"rate?");
return discount;
}
}
Here are a few adapters we might consider developing:
Here are the implementations:
Implement the R2C generator. Here's a sample model you should be able to generate code from: