Download - Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures
![Page 1: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/1.jpg)
CSE 335: Software Design K. Stirewalt
Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures
Topics:– Elaboration of the expression class hierarchy– New modeling concepts: Aggregation and composition
associations [Blaha and Rumbaugh § 4.4]– Composite object structures– The Composite pattern [Gamma]
![Page 2: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/2.jpg)
CSE 335: Software Design K. Stirewalt
Recall: Economics of software development
Problem: Financing development of software systems– Big software is notoriously expensive over long term– Many software projects run out of money before they ever yield
a product
What the manager can do:– Outsource development to cheaper programmers– “Buy vs. build” – Amortize costs over long term: Design for change– Create assets that have intrinsic value and that pay dividends:
Reusable libraries
![Page 3: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/3.jpg)
CSE 335: Software Design K. Stirewalt
Uses of abstract classes
Defining an abstract “placeholder” that can hold objects of various types– E.g., Shape– Useful for building composite object structures
Factoring common code into an abstract concept
Serving as a program fragment in the design of a family of programs
Definition of role-classes for use in collaboration-based designs
![Page 4: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/4.jpg)
CSE 335: Software Design K. Stirewalt
Uses of abstract classes
Defining an abstract “placeholder” that can hold objects of various types– E.g., Shape– Useful for building composite object structures
Factoring common code into an abstract concept
Serving as a program fragment in the design of a family of programs
Definition of role-classes for use in collaboration-based designs
![Page 5: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/5.jpg)
CSE 335: Software Design K. Stirewalt
Recall: Collaborative Exercise
Design classes for arithmetic expression trees. Each arithmetic operator class should provide operations for retrieving operand expressions. Define at least the following classes:
– Variable– Literal– Negate– Add, Subtract, Multiply, Divide
Hint: You will need to invent some abstract classes
![Page 6: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/6.jpg)
CSE 335: Software Design K. Stirewalt
Classes Expr and Literal
class Expr {
public:
virtual ~Expr() {}
protected:
Expr() {}
};
Note: Constructor is not public!
class Literal : public Expr {
public:
Literal( double d )
: _val(d) {}
double value() const
{ return _val; }
protected:
double _val;
};
![Page 7: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/7.jpg)
CSE 335: Software Design K. Stirewalt
Class BinaryExpr
class BinaryExpr : public Expr { public: const Expr* getLeftOperand() const { return leftOperand; } const Expr* getRightOperand()const { return rightOperand; }
protected: const Expr* leftOperand; const Expr* rightOperand;
BinaryExpr( const Expr* l, const Expr* r ): leftOperand( l ), rightOperand( r ) {}
};
Note: Constructor is not public!
![Page 8: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/8.jpg)
CSE 335: Software Design K. Stirewalt
Example: Classes Add and Multiply
class Add : public BinaryExpr { public: Add( Expr* l, Expr* r ) : BinaryExpr(l,r) {}
};
class Multiply : public BinaryExpr { public: Multiply( Expr* l, Expr* r ) : BinaryExpr(l,r) {}
};
![Page 9: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/9.jpg)
CSE 335: Software Design K. Stirewalt
Exercise
int main(void){ Variable* x = new Variable(“x”); Variable* y = new Variable(“y”); Literal* l1 = new Literal(5.0); Literal* l2 = new Literal(3.9);
Expr* e = new Add(l1, new Multiply(x,l2)); … Expr* e2 = new Divide(e, new Subtract(y,x)); …}
Draw a UML object diagram depicting expressions e and e2
![Page 10: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/10.jpg)
CSE 335: Software Design K. Stirewalt
Exercise
Draw a UML class diagram that illustrates the composite nature of the Expr class hierarchy.
![Page 11: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/11.jpg)
CSE 335: Software Design K. Stirewalt
First attempt at model of the Expr class hierarchy
Expr
BinaryExpr
Add Divide Multiply Subtract
Variable
name : string
Literal
val : double
UnaryExpr
Negate
child
left
right
* *
*
![Page 12: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/12.jpg)
CSE 335: Software Design K. Stirewalt
First attempt at model of the Expr class hierarchy
Expr
BinaryExpr
Add Divide Multiply Subtract
Variable
name : string
Literal
val : double
UnaryExpr
Negate
child
left
right
* *
*
Question: Can you spot any problems with this model?
![Page 13: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/13.jpg)
CSE 335: Software Design K. Stirewalt
Consider the following object models
: Add
: Literal
val = 5.0
: Add
: Subtract
left
right right
right
left left
![Page 14: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/14.jpg)
CSE 335: Software Design K. Stirewalt
Document Paragraph Sentence1..* 1..*
New concept: Aggregation
Special kind of association with additional semantics– Minimally: Transitive and anti-symmetric
A.k.a., the is-part-of or part-whole association– A sentence is part of a paragraph (a paragraph comprises sentences)– A paragraph is part of a document (a document comprises paragraphs)
Two forms:– strong aggregation (a.k.a. composition)– weak aggregation
![Page 15: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/15.jpg)
CSE 335: Software Design K. Stirewalt
Terminology: Parts and assemblies
Instances of classes on the “diamond end” of an aggregation are called assemblies; instances of classes on the non-diamond end are called the parts of an assembly.
E.g.:– aggAB: instances of A are the assemblies, instances of B are the parts– aggBC: instances of B are the assemblies, instances of C are the parts– Parts that are also assemblies give rise to deep aggregation
hierarchies
A B CaggAB aggBC
![Page 16: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/16.jpg)
CSE 335: Software Design K. Stirewalt
Car
Engine
Deep hierarchies: Part-explosion diagrams
GearboxBodyWheel
Door Hood Trunk
1..*
4
![Page 17: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/17.jpg)
CSE 335: Software Design K. Stirewalt
Aggregation Versus Association
Aggregation is a special kind of an association:
Use aggregation when:– association might naturally be called “is-part-of” or “is-
made-of”– some operations on the assembly object automatically
apply to the part objects• E.g., deletion, moving, recomputing, etc
– some values propagate from the assembly to all or some of the parts
– there is an intrinsic asymmetry to the association• i.e., one class is subordinate to the other
![Page 18: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/18.jpg)
CSE 335: Software Design K. Stirewalt
Better model of the Expr hierarchy
Expr
BinaryExpr
Add Divide Multiply Subtract
Variable
name : string
Literal
val : double
UnaryExpr
Negate
child
left
right
*
*
*
![Page 19: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/19.jpg)
CSE 335: Software Design K. Stirewalt
Aggregation vs. composition
Composition is a strong form of aggregation with additional semantic constraints:– A part can belong to at most one assembly– Once a part has been assigned to an assembly, its
lifetime is coincident with that of the assembly
Composition implies ownership of parts by assembly– Deletion of assembly object triggers deletion of
component objects
![Page 20: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/20.jpg)
CSE 335: Software Design K. Stirewalt
Examples of Aggregations
Vehicle VehiclePart1..*
Polygon LineSegment
Vehicle comprises the parts; Parts may be “parts of” vehicles
Where assembly appears, its parts also appear.
Destroying the assembly meansdeleting its parts
3..*0..1
*
![Page 21: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/21.jpg)
CSE 335: Software Design K. Stirewalt
Example with both compositions and associations
Company Division Department
Person
em
plo
ys
*
*
**
![Page 22: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/22.jpg)
CSE 335: Software Design K. Stirewalt
Composite object structures
Expression trees are examples of composite object structures– General notion of an expression, which may be a:
• simple object (e.g., an instance of a Variable or Literal) or• composite object (e.g., instance of an Add, Subtract,
Multiply, or Divide)
Composite structures show up frequently in real object-oriented designs
Composite designs especially useful when the abstract class declares polymorphic operations
![Page 23: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/23.jpg)
CSE 335: Software Design K. Stirewalt
Exercise
Extend Expr hierarchy with polymorphic operation:void print( ostream& )
It should be possible to execute code such as:Expr* l = new Literal(5.0);
Expr* v = new Variable(“x”);
Expr* e = new Add( l, v );
e->print(cout);
l->print(cout);
v->print(cout);
![Page 24: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/24.jpg)
CSE 335: Software Design K. Stirewalt
Composite pattern (idealized)
Client Component
Operation()
Composite
Operation()Add(Component)Remove(Component)GetChild(int) : Component
Leaf
Operation()
children*
*
![Page 25: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/25.jpg)
CSE 335: Software Design K. Stirewalt
Composite pattern (most general)
Client Component
Operation()Add(Component)Remove(Component)GetChild(int) : Component
Composite
Operation()Add(Component)Remove(Component)GetChild(int) : Component
Leaf
Operation()
children
*
*
![Page 26: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/26.jpg)
CSE 335: Software Design K. Stirewalt
Design virtues of composites
Makes client code simple– Clients treat composite structures and individual
objects uniformly– Design principle: Abstraction through use of the
abstract class Component and the identification of abstract operations that apply to many types of objects
Makes it easy to add new kinds of components without modifying any client code– Design principle: Incrementality– Design principle: Anticipation of change
![Page 27: Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures](https://reader035.vdocuments.us/reader035/viewer/2022062517/5681345e550346895d9b43b1/html5/thumbnails/27.jpg)
CSE 335: Software Design K. Stirewalt
Question
Can you come up with another example use of the Composite pattern?