Tải bản đầy đủ (.pdf) (10 trang)

Enterprise Java and UML 2nd Edition PHẦN 5 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (289.1 KB, 10 trang )

import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.FormPrimitives.*;
import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.Layout.*;
import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.ContentElements.*;
import com.wiley.compBooks.EJwithUML.TimeCardWorkflow.*;
import com.wiley.compBooks.EJwithUML.TimeCardDomain.*;
import com.wiley.compBooks.EJwithUML.TimecardProducers.*;
import com.wiley.compBooks.EJwithUML.Dtos.*;
import com.wiley.compBooks.EJwithUML.Base.EjbUtil.*;
import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*;
/**
* The RecordTimeServlet uses the TimecardWorkflow and
* HtmlProduction packages to create the formatted HTML
* to build a charge code selection grid.
*/
public class SelectChargeCodeServlet extends BasicTimecardServlet
{
private RecordTimeWorkflowHome rtwhome = null;
/** Overrides method from HttpServlet. doGet is called by the Æ
servlet engine. */
public void doGet(HttpServletRequest request, HttpServletResponse Æ
response) throws ServletException, IOException
{
doPost(request, response);
}
/** Overrides method from HttpServlet. doPost is called by the Æ
servlet engine. */
public void doPost(HttpServletRequest request, HttpServletResponse Æ
response) throws ServletException, IOException
{
try
{


HttpSession session = request.getSession();
String username = (String)
session.getAttribute(TimecardKey.USERNAME.getKeyText());
PagePrimitive page = new TimecardPageProducer(“Select a Æ
Charge Code”);
ParagraphPrimitive page_title = new ParagraphPrimitive Æ
(TimecardStyle.IMPORTANT_TEXT, “Select a charge code from the list below”);
page.addToBody(page_title);
Iterator clients = getClients(username);
TablePrimitive table = new TablePrimitive();
table.setStyle(TimecardStyle.CHARGE_CODE_SELECTION_TABLE);
table.setStyleForCells(TimecardStyle.CHARGE_CODE_SELECTION_CELLS);
page.addToBody(table);
int row_ctr = 0;
40 SelectChargeCodeServletjava
267783 WS07.qxd 5/5/03 9:18 AM Page 40
table.setPrimitiveAt(row_ctr, 0, new Æ
SpanPrimitive(TimecardStyle.IMPORTANT_TEXT, “Client”));
table.setPrimitiveAt(row_ctr, 1, new Æ
SpanPrimitive(TimecardStyle.IMPORTANT_TEXT, “Project”));
table.setPrimitiveAt(row_ctr, 2, new Æ
SpanPrimitive(TimecardStyle.IMPORTANT_TEXT, “Code”));
table.setPrimitiveAt(row_ctr, 3, new Æ
SpanPrimitive(TimecardStyle.IMPORTANT_TEXT, “Action”));
row_ctr++;
while (clients.hasNext())
{
ClientDTO client = (ClientDTO) clients.next();
Iterator projects = client.getProjects();
while (projects.hasNext())

{
ProjectDTO project = (ProjectDTO) projects.next();
Iterator codes = project.getChargeCodes();
while (codes.hasNext())
{
String code = (String) codes.next();
String url = “/Timecard/RecordTimeServlet?” Æ
+TimecardKey.CLIENT.getKeyText()+”=” +client.getName()+
“&” +TimecardKey.PROJECT.getKeyText()+”=” Æ
+project.getName()+ “&”+ TimecardKey.CODE.getKeyText()+”=” +code;
TextPrimitive client_text = new Æ
TextPrimitive(client.getName());
TextPrimitive project_text = new Æ
TextPrimitive(project.getName());
TextPrimitive code_text = new TextPrimitive(code);
TextPrimitive link_text = new TextPrimitive(“Add”);
LinkPrimitive add_link = new LinkPrimitive(url);
add_link.addText(link_text);
table.setPrimitiveAt(row_ctr, 0, client_text);
table.setPrimitiveAt(row_ctr, 1, project_text);
table.setPrimitiveAt(row_ctr, 2, code_text);
table.setPrimitiveAt(row_ctr, 3, add_link);
row_ctr++;
}
}
}
// write page to response
StringBuffer buffer = new StringBuffer();
page.buildContent(buffer);
response.getWriter().println(buffer.toString());

response.getWriter().flush();
SelectChargeCodeServletjava 41
267783 WS07.qxd 5/5/03 9:18 AM Page 41
response.getWriter().close();
}
catch (Exception e)
{
throw new ServletException(e);
}
}
private Iterator getClients(String username) throws
ApplicationException, RemoteException, NamingException, CreateException
{
if (rtwhome == null)
{
// Get workflow and timecard
Context initial = new InitialContext();
Object objref = initial.lookup(EjbReferenceNames. Æ
RECORD_TIME_WORKFLOW_HOME);
rtwhome =
(RecordTimeWorkflowHome)PortableRemoteObject.narrow(objref, Æ
RecordTimeWorkflowHome.class);
}
RecordTimeWorkflow rtwf = rtwhome.create(username);
Collection clients = rtwf.getAllClients();
return clients.iterator();
}
}
42 SelectChargeCodeServletjava
267783 WS07.qxd 5/5/03 9:18 AM Page 42

43
The following glossary shows how several important object-oriented concepts are
shown in the UML and how they can be implemented in Java. Each section describes a
concept, provides a sample UML diagram that uses the concept, implements the UML
in Java, and offers some guidance on the proper use of the concept.
Generalization
One or more subclasses may share the attributes and behavior that are defined for the
base class. There are two ways to describe this relationship in proper object-oriented
terminology. First, a class inherits all of the attributes and behaviors from a superclass
or base class. From the opposite perspective, the superclass is a generalization of the
attributes and behaviors that are common to all of its subclasses. In UML, the relationship
is described as a generalization and is denoted by a solid line with a hollow arrow
pointing to the base class.
Visual Glossary
267783 WS08.qxd 5/5/03 9:19 AM Page 43
Figure VG.1 Generalization example.
UML Example
Consider a brief example of generalization. Vehicle is a generalization of both Car and
Truck. The two subclasses, Truck and Car, inherit all of the attributes and behavior
from the base class. Figure VG.1 shows the two subclasses, with generalization arrows
pointing to the base class. In this case, there is no default go behavior for vehicles, so
the base class must be abstract. Each concrete subclass of Vehicle must provide an
implementation of the go method. Each concrete subclass may accept the default
behavior for startEngine, stopEngine, and isEngineOn. If the default implementation is
inappropriate, the subclass may override the default implementation by providing its
own implementation.
In UML, rendering the abstract class name in italics indicates that the class is
abstract. Showing a method in both the base class and in the subclass indicates that the
subclass overrides that method.
Java Example

The following Java files show how the UML model in Figure VG.1 can be implemented
in Java.
Vehicle.java
Vehicle.java is the abstract base class in Figure VG.1. This is reflected in the source
code, as the class and the go method are both abstract. The other methods have imple-
mentations, but are not final, so they may be overridden by subclasses.
/**
* The Vehicle class contains the data and behavior that
Vehicle
+ go() : String
+ startEngine() : void
+ stopEngine() : void
+ isEngineOn() : boolean
Truck
+ go() : String
Car
+ go() : String
44 Visual Glossary
267783 WS08.qxd 5/5/03 9:19 AM Page 44
* is common to all Vehicles.
*/
public abstract class Vehicle
{
private boolean engineStarted;
/** Answers the noise made when the Vehicle goes.
Must be overridden by all concrete implementations
of Vehicle. */
public abstract String go();
/** Starts engine. */
public void startEngine()

{
this.engineStarted = true;
}
/** Stops engine. */
public void stopEngine()
{
this.engineStarted = false;
}
/** Answers true if engine is started. */
public boolean isEngineOn()
{
return this.engineStarted;
}
}
Car.java
Car.java is a subclass of Vehicle, which in Java is indicated by the reserved word
“extends.” Car overrides the go method and uses the base class implementation of the
isEngineOn method.
/**
* The Car class inherits from Vehicle and overrides the go method.
* It encapsulates all data and behavior that is specific to Cars.
*/
public class Car extends Vehicle
{
/** Answers the noise made by the Car when it goes. */
public String go()
{
if (this.isEngineOn())
{
return “Vroom”;

Visual Glossary 45
267783 WS08.qxd 5/5/03 9:19 AM Page 45
}
else
{
return “ ”;
}
}
}
Truck.java
Truck.java is a subclass of Vehicle, which in Java is indicated by the reserved word
“extends.” Truck overrides the go method and uses the base class implementation of
the isEngineOn method. Truck is almost identical to Car, with a different implementation
of the go method.
/**
* The Truck class inherits from Vehicle and overrides the go method.
* It encapsulates all data and behavior that is specific to Trucks.
*/
public class Truck extends Vehicle
{
/** Answers the noise made by the Truck when it goes. */
public String go()
{
if (this.isEngineOn())
{
return “Rumble”;
}
else
{
return “ ”;

}
}
}
Guidelines
It is extremely important for the generalization relationship to be an accurate descrip-
tion of the underlying reality that you are modeling. Each subclass must really be a
refinement of the superclass. Do not subclass a class just to get useful behavior or
attributes. Doing so makes the system significantly more difficult to understand, and
may result in strange errors as the system evolves.
46 Visual Glossary
267783 WS08.qxd 5/5/03 9:19 AM Page 46
Realization
A class realizes an interface by implementing each method that is defined in the inter-
face. By realizing the interface, the class is promising to make the interface real. In
UML, the realization relationship is denoted by a dashed line, with a hollow arrow
pointing to the interface.
UML Example
Continuing the earlier example, some Vehicles can carry cargo, some cannot. Also,
some classes that are not “normal” vehicles may also carry cargo. So, rather than intro-
ducing a separate subclass for all cargo-carrying vehicles, we introduce an interface,
ICargoTransport. Our design allows any class to realize the ICargoTransport by
providing an implementation for the loadCargo method. Figure VG.2 shows Truck
realizing the ICargoTransport interface, while Car does not.
Java Example
The following Java files show how the UML model in Figure VG.2 can be implemented
in Java. Only the files that have changed from the generalization example are shown.
Figure VG.2 Realization example.
Vehicle
+ go() : String
+ startEngine() : void

+ stopEngine() : void
+ isEngineOn() : boolean
Truck
+ go() : String
+ loadCargo() : void
<<Interface>>
ICargoTransport
+ loadCargo() : void
Car
+ go() : String
Visual Glossary 47
267783 WS08.qxd 5/5/03 9:19 AM Page 47
Truck.java
Truck.java is a subclass of Vehicle, which in Java is indicated by the reserved word
“extends.” Truck overrides the go method and uses the base class implementation of
the isEngineOn method. Truck also realizes the ICargoTransport interface, as indicated
by the “implements” reserved word in the class definition.
/**
* The Truck class inherits from Vehicle, and overrides the go
* method. It encapsulates all data and behavior that is specific to
* Trucks.
*/
public class Truck extends Vehicle implements ICargoTransport
{
/** Answers the noise made by the Truck when it goes. */
public String go()
{
if (this.isEngineOn())
{
return “Rumble”;

}
else
{
return “ ”;
}
}
/** Adds cargo to this Truck.*/
public void loadCargo()
{
}
}
ICargoTransport.java
ICargoTransport.java simply defines the name and signature for the loadCargo
method. As an interface, it is precluded from providing an implementation.
/**
* The ICargoTransport interface defines the methods
* that must be implemented by all classes that
* transport cargo.
*/
public interface ICargoTransport
{
48 Visual Glossary
267783 WS08.qxd 5/5/03 9:19 AM Page 48
/** Loads cargo for transport. */
public void loadCargo();
}
Guidelines
All of the methods in an interface must combine to describe a coherent responsibility.
Association
An association is a long-term relationship between objects. In an association, an object

keeps a reference to another object, and can call the object’s methods as it needs them.
Real life is replete with association relationships. Consider a person with his or her
own automobile. As long as he or she remembers where it is parked, the car will let the
person in to drive to his or her destination. In the UML, a solid line between the two
classes represents an association.
In some cases, an object may instantiate another object and keep a reference to it for
future use. An object may also receive an object as a parameter to a configuration
method and keep a reference to the object.
UML Example
Consider an association relationship in which each Person object knows about zero or
more Vehicle objects. Figure VG.3 shows this relationship in a class diagram. The rela-
tionship is read as “every Person object is associated with zero or more Vehicle
objects,” and “every Vehicle object is associated with one or more Person objects.” It
may help to think of this as a “knows-about-a” relationship, as in “each Person object
knows about some Vehicle objects.”
Figure VG.3 Association example.
Vehicle
+ go() : String
+ startEngine() : void
+ stopEngine() : void
+ isEngineOn() : boolean
Truck
+ go() : String
+ loadCargo() : void
<<Interface>>
ICargoTransport
+ loadCargo() : void
Car
+ go() : String
Person

+ addVehicle(v : Vehicle) : void
1 * 0 *
Visual Glossary 49
267783 WS08.qxd 5/5/03 9:19 AM Page 49

×