Lec05 DP Intro & Structural
Lec05 DP Intro & Structural
Lec05 DP Intro & Structural
TCP2201
Object-oriented Analysis and Design
Portions of this file have content that were adapted from [1] Object-Oriented Software Engineering Using UML, Patterns, and Java, [2] OOSE Pratcial software
development using UML and Java, [3] http://kremer.cpsc.ucalgary.ca/patterns/index.html, [4] http://www.tutorialspoint.com/index.htm, [5]
http://www.avajava.com/, [6] https://dzone.com, [7] https://sourcemaking.com/design_patterns/structural_patterns and
[8] http://uosis.mif.vu.lt/~plukas/resources/DPBook/excerpts/patterns.htm
Lecture outcomes
At the end of this lecture you should
Example.
You have code to create a ‘List’ object made with
node objects
Later you realize you need a ‘Stack’ type object
Various methods exist to realize the ‘Stack’ but
which is the best?
Do you create the Stack as a implementation
inheritance? (generalization method where you inherit
from an existing class)
But what would happen if the ‘Stack’ object calls
Remove() instead of Pop()?
What about creating the Stack as a delegation instead?
(composition method)
A Stack would then make use of List node objects but
rewrite the Pop and Push methods to incorporate
Remove and Add
Delegation Inheritance
Pros: Pros:
Flexibility → Any delegate object can be Straightforward to use
Comparison
replaced at run time by another
one (as long as it has the same type)
Static hierarchy at compile time ensures
consistent architecture
Details of the delegate can be hidden Easy to implement (override) new
Low coupling from client to delegate class functionality
Good for creating and maintaining different
Cons: implementations according
Inefficiency: Many objects available, flat to a common interface
hierarchy
Highly parameterized → difficult to Cons:
understand, risk of having inconsistencies in Inheritance exposes (undesired) details of
the architecture the parent class
High coupling between classes
Inheritance hierarchy cannot change at
runtime
Substitution of the subclasses for a super
class in any of the client code possible →
may lead to unwanted conditions
Resolving the debate
Although there still is no guaranteed way to promise a
successful design, there are several tried-and-tested
design patterns (solutions) intended for common problem
scenarios.
A design pattern is a template solution that developers
have refined over time to solve a range of recurring
problems
Design patterns are sufficiently abstract to be reused in
different settings.
Makes software modifiable and extensible to minimize
the cost of future changes
Patterns merely rely on object characteristics such as:
inheritance
delegation
dynamic binding
MODEL-VIEW-CONTROLLER
• Basic pattern
for GUIs
Leaf
Defines the behavior for basic elements in the composition.
Represents building blocks for the composition and implements base
component.
It does not reference other components.
Composite
It consists of leaf elements and implements the operations in base
component.
The composite pattern models tree structures that
represent part-whole hierarchies with arbitrary depth
and width
With the DP, each individual object and their
compositions are treated uniformly
How would it be implemented?
Base component
This is the base from which composites and leaves will inherit
from
Usually base components are abstract classes or interface objects
One (1) method prototype is shown in the example above which
MUST be overridden by subclasses (because it’s an interface)
A leaf implements the base component and these are the building block for
the composites later.
Note how it overrides the method prototypes from the superclass → in this
case the method just prints to screen
Another example
}
• The Graphic class is an example that implements
the composite design pattern with its hierarchy of
drawable objects as both primitives (Line, Oval etc)
and containers (Picture, Image etc)
Composite DP summary
Name Composite
Intent To treat individual objects and multiple, recursively-composed objects
uniformly through a common interface
Applicability Used when
• you want to represent part-whole hierarchies of objects.
• you want clients to be able to ignore the difference between
compositions of objects and individual objects
Solution Component interface specifics services that are shared amongst leaf
and composite objects. A composite has an aggregation type
association with components and implements each service by iterating
each contained component. The leaf objects contain services that do
actual work
Consequences Defines class hierarchies consisting of primitive objects
and composite objects
Makes the client simple
Makes it easy to add new types of components
Can make design overly general
COMMON DESIGN
PATTERNS
(Adapter - Structural)
Adapter DP scenario
• You are developing a program and require
certain functionality but do not possess the
skills/time to code it.
• Luckily, you locate an existing software library
that does the exact thing you require but it
was written in a different language and only
compiled binaries are provided.
• Instead of rewriting or trying to understand
the code, you decide to use the software
library as is