Title: Synthetic OO Design Concepts
1Synthetic 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
2Recall 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
3Uses 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
4Uses 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
5Recall 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
6Classes Expr and Literal
- class Expr
- public
- virtual Expr()
- protected
- Expr()
-
class Literal public Expr public Literal(
double d ) _val(d) double value()
const return _val protected
double _val
Note Constructor is not public!
7Class 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!
8Example 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)
9Exercise
Draw a UML object diagram depicting expressions e
and e2
- 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))
-
10Exercise
- Draw a UML class diagram that illustrates the
composite nature of the Expr class hierarchy.
11First attempt at model of the Expr class hierarchy
left
child
right
Expr
BinaryExpr
Variable
Literal
UnaryExpr
name string
val double
Add
Divide
Multiply
Subtract
Negate
12First attempt at model of the Expr class hierarchy
left
child
right
Expr
BinaryExpr
Variable
Literal
UnaryExpr
name string
val double
Add
Divide
Multiply
Subtract
Negate
Question Can you spot any problems with this
model?
13Consider the following object models
right
right
Add
Add
left
left
left
Literal
Subtract
val 5.0
right
14New 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)
1..
1..
Document
Paragraph
Sentence
- Two forms
- strong aggregation (a.k.a. composition)
- weak aggregation
15Terminology 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
aggAB
aggBC
A
B
C
16Deep hierarchies Part-explosion diagrams
Car
4
Engine
Gearbox
Body
Wheel
1..
Door
Hood
Trunk
17Aggregation 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
18Better model of the Expr hierarchy
left
child
right
Expr
BinaryExpr
Variable
Literal
UnaryExpr
name string
val double
Add
Divide
Multiply
Subtract
Negate
19Aggregation 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
20Examples of Aggregations
1..
Vehicle comprises the parts Parts may be parts
of vehicles
VehiclePart
Vehicle
0..1
3..
LineSegment
Polygon
Where assembly appears, its parts also appear.
Destroying the assembly means deleting its parts
21Example with both compositions and associations
Company
Division
Department
employs
Person
22Composite 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
23Exercise
- 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-gtprint(cout)
- l-gtprint(cout)
- v-gtprint(cout)
24Composite pattern (idealized)
Client
Component
Operation()
children
Composite
Leaf
Operation() Add(Component) Remove(Component) GetCh
ild(int) Component
Operation()
25Composite pattern (most general)
Client
Component
Operation() Add(Component) Remove(Component) GetCh
ild(int) Component
children
Composite
Leaf
Operation() Add(Component) Remove(Component) GetCh
ild(int) Component
Operation()
26Design 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
27Question
- Can you come up with another example use of the
Composite pattern?