Upload
maj
View
27
Download
0
Embed Size (px)
DESCRIPTION
Art for Chapter 7, Object Design. Problem. System. Custom objects. Application objects. Solution objects. Requirements gap. Object design gap. Off-the-shelf components. System design gap. Machine. - PowerPoint PPT Presentation
Citation preview
Con
quer
ing
Com
plex
and
Cha
ngin
g S
yste
ms
Ob
ject
-Ori
ente
d S
oftw
are
En
gin
eeri
ng
Art forChapter 7,Object Design
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 2
Figure 7-1. Object design closes the gap between application objects identified during requirements and off-the-shelf components selected during system design (stylized UML class diagram).
Problem
Machine
System design gap
Object design gap
Requirements gap
System
Application objects
Solution objects
Custom objects
Off-the-shelf components
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 3
Figure 7-2. Activities of object design (UML activity diagram).
Identifying components
Adjusting components
Specifying constraints
Collapsing classes
Specifying types &
Identifying missingattributes & operations
Specifying visibility
Specification
signatures
Optimization
Specifying exceptions
Restructuring
Realizingassociations
Revisitinginheritance
Optimizing accesspaths
Caching complexcomputations
Delaying complexcomputations
Component selection
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 4
Figure 7-3. Declarations for the Hashtable class (UML class model and Java excerpts).
Hashtable
+put(key:Object,entry:Object)+get(key:Object):Object+remove(key:Object)+containsKey(key:Object):boolean+size():int
-numElements:int
class Hashtable {private int numElements;
/* Constructors omitted */public void put (Object key, Object entry) {…};public Object get(Object key) {…};public void remove(Object key) {…};public boolean containsKey(Object key) {…};public int size() {…};
/* Other methods omitted */}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 5
Figure 7-4. Method declarations for the Hashtable class annotated with preconditions, postconditions, and invariants (Java, constraints in the iContract syntax [iContract]).
class Hashtable {
/* The number of elements in the Hashtable is nonnegative at all times. * @inv numElements >= 0 */private int numElements;
/* The put operation assumes that the specified key is not used. * After the put operation is completed, the specified key can be used * to recover the entry with the get(key) operation: * @pre !containsKey(key) * @post containsKey(key) * @post get(key) == entry */public void put (Object key, Object entry) {…};
/* The get operation assumes that the specified key corresponds to an * entry in the Hashtable. * @pre containsKey(key) */public Object get(Object key) {…};
/* The remove operation assumes that the specified key exists in the * Hashtable. * @pre containsKey(key) * @post !containsKey(key) */public void remove(Object key) {…};
/* Other methods omitted */}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 6
Figure 7-5. Examples of invariants, preconditions, and postconditions in OCL (UML class diagram).
<<invariant>>self.numElements >= 0
HashTable
put(key,entry:Object)get(key):Objectremove(key:Object)containsKey(key:Object):booleansize():int
numElements:int
<<postcondition>>!containsKey(key)
<<postcondition>>get(key) == entry
<<precondition>>containsKey(key)
<<precondition>>!containsKey(key)
<<precondition>>containsKey(key)
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 7
Figure 7-6. Map with political boundaries and emission sources (JEWEL, mock-up).
Session
Layer fader
New
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 8
Figure 7-7. Object model for the GIS of JEWEL (UML class diagram).
* *
**
* *
**
* *
* *
PoliticalLayerWaterLayerRoadLayer
Highway SecondaryRoad
River Lake
State County
PolyLine Polygon
Layer
label
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 9
Figure 7-9. Subsystem description for the GIS of JEWEL.
JEWEL GIS
Purpose• store and maintain the geographical information for JEWEL
Service• creation and deletion of geographical elements (roads, rivers, lakes, and boundaries)• organization of elements into layers• zooming (selection of points given a level of detail)• clipping (selection of points given a bounding box)
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 10
Figure 7-10. Subsystem decomposition of JEWEL (UML class diagram).
Visualization
Simulation
EmissionModeling
GIS
Storage
Displays geographical and emissions data to
the user.
Manages simulations and
results.
Engine for emission simulations.
Maintains persistent data, including GIS and emissions data.
Manages GIS information for
Visualization and EmissionsModeling.
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 11
Figure 7-11. A sequence diagram for the zoomIn() operation (UML sequence diagram). This sequence diagram leads to the identification of a new class, LayerElement. Because we are focusing on the GIS, we treat the Visualization subsystem as a single object.
:EndUser
zoomIn(x,y)computeBoundingBox(x,y)
*getOutline(r,d)*getOutline(r,d)
points
points
:Layer:Visualization :LayerElement
Newly identifiedclass
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 12
Figure 7-12. Adding operations to the object model of the JEWEL GIS to realize zooming and clipping (UML class diagram).
Layer
getOutline(bbox, detail)
LayerElement
label
Political LayerWater LayerRoad Layer
label
Highway Secondary road
River Lake
State County
LayerElement(polyline)LayerElement(polygon)getOutline(bbox, detail)
elements*
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 13
Figure 7-13. A naive point selection algorithm for the GIS. The left column represents a road crossing and two neighboring counties. The right column shows that road crossings and neighboring counties may be displayed incorrectly when points are not selected carefully.
High detail Low detail
Tw
o cr
ossi
ngro
ads
Tw
o ne
ighb
orin
gco
untie
s
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 14
Figure 7-14. Additional attributes and methods for the Point class to support intelligent point selection and zooming (UML class diagram).
Point
x, y
Point(x, y)includeInLevel(level)excludeFromLevel(level)
notInDetailLevels
* *
PolyLine
addPoint(point)getPoints(bbox)
Polygon
addPoint(point)getPoints(bbox)
inDetailLevels
* *
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 15
Figure 7-15. Adding type information to the object model of the GIS (UML class diagram). Only selected classes shown for brevity.
Layer
LayerElement
+label:String
+label:String
Point
+Point(x, y:double)+includeInLevel(level:double)+excludeFromLevel(level:double)
+LayerElement(polyline:PolyLine)+getOutline(bbox:Rectangle2D,
detail:double):Enumeration
+Layer(label:String)
detail:double):Enumeration+getOutline(bbox:Rectangle2D,
*
points
*
elements
-x, y:double
-notInDetailLevels:Set-inDetailLevels:Set
PolyLine
+label:String+PolyLine()+getPoints():Enumeration
1
1
*
1
polyline
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 16
Figure 7-16. Examples of preconditions and exceptions for the Layer class of the JEWEL GIS.
Layer
+Layer(label:String)
detail:double):Enumeration
+label:String
+getOutline(bbox:Rectangle2D,
<<precondition>>bbox.width > 0 and
<<precondition>>detail > 0
bbox.height > 0
<<exception>>ZeroDetail
<<exception>>ZeroBoundingBox
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 17
Figure 7-17. JFC components for the JEWEL Visualization subsystem (UML object diagram). Associations denote the containment hierarchy used for ordering the painting of components. We use stereotypes to distinguish JEWEL classes from classes provided by JFC.
<<JFC>>:JFrame
<<JFC>>:JPanel
<<JFC>>:JScrollPane
<<JFC>>:JToolbar
:MapArea
paintContents()
<<JFC>>zoomIn:JButton
<<JFC>>zoomOut:JButtondetail
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 18
Figure 7-18. Declaration for drawPolyline() and drawPolygon() operations [JFC, 1999].
// from java.awt packageclass Graphics {
//...
void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {…};
void drawPolygon(int[] xPoints, int[] yPoints, int nPoints){…};
}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 19
Figure 7-19. An example of dynamic site with WebObjects (UML component diagram).
WebBrowser
RelationalDatabase
StaticHTML
WOAdaptorWebServer
WoRequest Template
WebObjectsApplication
WORequest
EOF
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 20
Figure 7-20. WebObject’s State Management Classes. The HTTP protocol is inherently stateless. The State Management Classes allow to maintain information between individual requests.
WOSession WOComponent DynamicElement
WOApplication
WORequest
WOAdaptor
*
*
**
WebServer WOSessionStore
*
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 21
Figure 7-21. Realization of a unidirectional, one-to-one association (UML class diagram; arrow denotes the transformation of the object model).
MapAreaZoomInAction11
MapAreaZoomInAction
targetMap:MapArea
Object design model before transformation
Object design model after transformation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 22
Figure 7-22. Realization of a bidirectional one-to-one association (UML class diagram and Java excerpts; arrow denotes the transformation of the object design model).
MapAreaZoomInAction11
MapAreaZoomInAction
-targetMap:MapArea -zoomIn:ZoomInAction+getZoomInAction()+setZoomInAction(action)
+getTargetMap()+setTargetMap(map)
Object design model before transformation
Object design model after transformation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 23
Figure 7-22 (continued from previous slide). Realization of a bidirectional one-to-one association (UML class diagram and Java excerpts; arrow denotes the transformation of the object design model).
class MapArea extends JPanel {private ZoomInAction zoomIn;/* Other methods omitted */void setZoomInAction (action:ZoomInAction) {
if (zoomIn != action) {zoomIn = action;zoomIn.setTargetMap(this);
}}
}class ZoomInAction extends AbstractAction {
private MapArea targetMap;/* Other methods omitted */void setTargetMap(map:MapArea) {
if (targetMap != map) {targetMap = map;targetMap.setZoomInAction(this);
}}
}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 24
Figure 7-23. Realization of a bidirectional, one-to-many association (UML class diagram; arrow denotes the transformation of the object design model).
Layer LayerElement1 *
Layer LayerElement
-containedIn:Layer-layerElements:Set+elements()+addElement(le)
+getLayer()+setLayer(l)
+removeElement(le)
Object design model before transformation
Object design model after transformation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 25
Figure 7-24. Realization of a bidirectional, many-to-many association (UML class diagram; arrow denotes the transformation of the object design model).
Polyline Point* *
Polyline
-points:Vector+elements()+addPoint(p)+removePoint(p)
Object design model before transformation
Object design model after transformation
Point
-polylines:Vector+elements()+addPolyline(l)+removePolyline(l)
{ordered}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 26
Figure 7-25. Transformation of an association class into an object and two binary associations (UML class diagram; arrow denotes the transformation of the object design model). Once the model contains only binary associations, each association is realized by using reference attributes and collections of references.
SimulationRun
dateauthorCPUtimegetOutline()
EmissionSource SimulationResult* 1
EmissionSource SimulationResult* 1
Object design model before transformation
Object design model after transformation
SimulationRun
1 1
dateauthorCPUtimegetOutline()
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 27
Figure 7-26. Realization of a bidirectional qualified association (UML class diagram; arrow denotes the transformation of the object design model).
Scenario
-runs:Hashtable+elements()+addRun(simname,sr:SimulationRun)+removeRun(simname,sr:SimulationRun)
SimulationRun
-scenarios:Vector+elements()+addScenario(s:Scenario)+removeScenario(s:Scenario)
simname 0..1*Object design model before transformation
Object design model after transformation
Scenario
SimulationRun
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 28
Figure 7-27. An example of code reuse with inheritance (UML class diagram).
AbstractButton
JButton JMenuItem JToggleButton
JRadioButton JCheckBox
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 29
Figure 7-28. AbstractFactory design pattern (UML class diagram, dependencies represent <<call>> relationships). This design pattern uses inheritance to support different look and feels (e.g., Motif and Macintosh). If a new specialization is added, the client does not need to be changed.
AbstractFactory
AbstractWindow
createWindow()createButton()
MotifWindow MacWindow
MacFactory
createWindow()createButton()
MotifFactory
createWindow()createButton()
AbstractButton
MotifButton MacButton
Client
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 30
Figure 7-29. An example of implementation inheritance. The left column depicts a questionable implementation of MySet using implementation inheritance. The right column depicts an improved implementation using delegation. (UML class diagram and Java).
Hashtable
MySet
insert(element)contains(element):boolean
put(key,element)get(key):ObjectcontainsKey(key):booleancontainsValue(element):boolean
Object design model before transformation Object design model after transformation
Hashtable
MySet
insert(element)contains(element):boolean
put(key,element)get(key):ObjectcontainsKey(key):booleancontainsValue(element):boolean
table 1
1
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 31
Figure 7-29 (continued from previous slide). An example of implementation inheritance. The left column depicts a questionable implementation of MySet using implementation inheritance. The right column depicts an improved implementation using delegation. (UML class diagram and Java).
/* Implementation of MySet using inheritance*/class MySet extends Hashtable { /* Constructor omitted */ MySet() { }
void insert(Object element) { if (this.get(element) == null){ this.put(element, this); } } boolean contains(Object element){ return (this.get(element)!=null); } /* Other methods omitted */}
/* Implementation of MySet using delegation*/class MySet { Hashtable table; MySet() { table = Hashtable(); } void insert(Object element) { if (table.get(element)==null){ table.put(element,this); } } boolean contains(Object element) { return (table.get(element) != null); } /* Other methods omitted */}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 32
Figure 7-30. Alternative representations of a unique identifier for a Person (UML class diagrams).
PersonSocialSecurity
ID:String
Person
SSN:String
Object design model before transformation
Object design model after transformation
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 33
Figure 7-31. Delaying expensive computations using a Proxy pattern (UML class diagram).
Object design model before transformation
Image
filename:String
width()height()paint()
data:byte[]
Object design model after transformation
Image
filename:String
width()height()paint()
RealImage
width()height()paint()
data:byte[]
ImageProxy
filename:Stringwidth()height()paint()
image
1 0..1
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 34
Figure 7-32. Embedded ODD approach. Class stubs are generated from the object design model. The object design model is then documented as tagged comments in the source code. The initial object design model is abandoned and the ODD is generated from the source code instead using a tool such as Javadoc (UML activity diagram, continued on next slide).
RADDocumentanalysis
Analysis
Analysis model
Object design
Initial objectdesign model
System design
Design goalsSubsystemdecomposition
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 35
Figure 7-32 (continued from previous slide). Embedded ODD approach. Class stubs are generated from the object design model. The object design model is then documented as tagged comments in the source code. The initial object design model is abandoned and the ODD is generated from the source code instead using a tool such as Javadoc (UML activity diagram).
Object design
Initial objectdesign model
Generate class stubs
Initial classstubs
ODDDocumentobject design
Implementation
Commented code
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 36
Figure 7-33. Interface description of the Layer class using Javadoc tagged comments (Java excerpts).
/* The class Layer is a container of LayerElements, each representing a * polygon or a polyline. For example, JEWEL typically has a road layer, a * water layer, a political layer, and an emissions layer. * @author John Smith * @version 0.1 * @see LayerElement * @see Point */class Layer { /* Member variables, constructors, and other methods omitted */ Enumeration elements() {…};
/* The getOutline operation returns an enumeration of points representing * the layer elements at a specified detail level. The operation only * returns points contained within the rectangle bbox. * @param box The clipping rectangle in universal coordinates * @param detail Detail level (big numbers mean more detail) * @return A enumeration of points in universal coordinates. * @throws ZeroDetail * @throws ZeroBoundingBox * @pre detail > 0.0 and bbox.width > 0.0 and bbox.height > 0.0 * @post forall LayerElement le in this.elements() | * forall Point p in le.points() | * result.contains(p) */ Enumeration getOutline(Rectangle2D bbox, double detail) {…};
/* Other methods omitted */}
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 37
Figure 7-34. DetailTable is a global object tracking which Points have been included or excluded from a specified detailLevel. This is an alternative to the inDetailLevels and notInDetailLevels sets depicted in Figure 7-14.
Point
x, y
detailLevelDetailTable
detailLevel* *
0..1
0..1
includesPoint
excludesPoint