OOSD Module 5
OOSD Module 5
CSE431
Dr. Bramah Hazela
ASET
Module V
Descriptors/Topics
• GRASP: Designing objects with
responsibilities ,Creator , Information expert
• Low Coupling –Controller – High Cohesion,
Designing for visibility
• Applying GoF design patterns – adapter,
singleton, factory and observer patterns.
• UML state diagrams and modeling – Operation
contracts- Mapping design to code
• UML deployment and component diagrams
3
ASET
*GRASP: Designing
Objects with
Responsibilities
• Creator
• Information Expert
• Low Coupling
• Controller
• High Cohesion
Creator pattern ASET
Name: Creator
Problem: Who creates an instance of A?
Solution: Assign class B the responsibility
to create an instance of class A if one of
these is true (the more the better):
• B contains or aggregates A (in a collection)
• B records A
• B closely uses A
• B has the initializing data for A
Who creates the Squares?
ASET
But:
• Information expert may contradict patterns of
Low Coupling and High Cohesion
• Remember separation of concerns principle
for large sub-systems
• I.e., keep “business” or application logic
in one place, user interface in other place,
database access in another place, etc.
Low Coupling Pattern ASET
• Name: Controller
(see Model-View-Controller architecture)
• Problem: Who should be responsible for UI
events?
• Solution: Assign responsibility for receiving or
handling a system event in one of two ways:
– Represent the overall system (façade pattern)
– Represent a use case scenario within which the
system event occurs (a session controller)
Who is the controller of playGame operation?
ASET
• Understandability, maintainability
• Complements Low Coupling
But:
• Avoid grouping of responsibilities or code into one
class or component to simplify maintenance by
one person. Why?
• Sometimes desirable to create less cohesive server
objects that provide an interface for many
operations, due to performance needs associated
with remote objects and remote communication
Summary ASET
• Name
– A meaningful pattern identifier
• Problem description
• Solution description
– Not a concrete design but a template for a
design solution that can be instantiated in
different ways
• Consequences
– The results and trade-offs of applying the
pattern
The Observer pattern ASET
• Name
– Observer
• Description
– Separates the display of object state from the object
itself
• Problem description
– Used when multiple displays of state are needed
• Solution description
– See slide with UML description
• Consequences
– Optimisations to enhance display performance are
impractical
The Observer pattern ASET
Subject Observer
ConcreteSubject ConcreteObserver
36
Not a Design Pattern ASET
• Linked Lists
• Hash Tables
• Domain Specific designs for entire
application or subsystem
• A code solution ready for copy-and-
paste
• Programming language features
• The same every time you see them 37
Design Patterns Classification ASET
40
Design Patterns Classification
ASET
Purpose
41
How Design Patterns Solve Design Problems
ASET
42
How Design Patterns Solve Design Problems
ASET
44
Patterns and Frameworks ASET
• Goals
– Learn how to exploit and gain experience
of others
– Examine the benefits of design pattern
– Learn one specific pattern: Strategy
pattern
47
Simple Simulation of Duck behavior
ASET
Duck
quack()
swim()
display()
// other duck methods
MallardDuck RedheadDuck
48
Simple Simulation of Duck behavior
ASET
Duck
quack()
swim()
display()
fly()
// other duck methods
MallardDuck RedheadDuck
50
Tradeoffs in use of inheritance and maintenance
ASET
Duck
One could override the
quack() fly method to the appropriate
swim() thing – just as the quack
display()
method below.
fly()
// other duck methods
MallardDuck
RedheadDuck
RubberDuck
display()
// looks like mallard display()
// looks like redhead quack()
//overridden to squeak
display()
// looks like rubberduck
fly()
// override to do nothing 51
Example complicated: add a wooden decoy ducks to the mix
ASET
DecoyDuck
Inheritance is not always the right
answer. Every new class that inherits
quack(){ unwanted behavior needs to be
// override to do nothing overridden.
}
display()
How about using interfaces instead?
// display decoy duck
fly (){
//override to do nothing
}
52
Using Interfaces in Our Example
ASET
Interfaces
Flyable Quackable Duck
54
Pros & Cons ASET
57
ASET
58
Design Principle ASET
<<interface>> <<interface>>
FlyBehavior QuackBehavior
fly() quack()
Quack MuteQuack
quack(){ quack(){
// implements duck // do nothing –
FlyWithWings FlyNoWay quacking Can’t quack
} }
fly(){ fly(){
// implements duck // do nothing – Squeak
flying Can’t fly
} } quack(){
// implements duck
Key now is that Duck class will delegate its squeak
flying and quacking behavior instead of } 60
implementing these itself
In the Duck simulation context…
ASET
Duck
61
Duck simulation recast using the new
approach ASET
<<interface>>
FlyBehavior
Duck
fly()
FlyBehavior: flyBehavior
QuackBehavior: quackBehavior FlyWithWings FlyNoWay
fly() fly()
performQuack() // implements duck // do nothing –
performFly() flying Can’t fly
setFlyBehavior()
<<interface>>
setQuackBehavior()
QuackBehavior
swim()
display() quack()
Quack Squeak
quack() quack()
MallardDuck RedHeadDuck RubberDuck DecoyDuck // implements duck // implements
quacking squeak
display() display() display() display()
Mutequack
quack()
// do nothing
62
Design Principle ASET
63
The strategy pattern ASET
64
ASET
UML
State Machine Diagrams
State Machine Diagrams ASET
• A state machine diagram models the • The door can be in one of three states:
behavior of a single object, specifying "Opened", "Closed" or "Locked". It can
the sequence of events that an object respond to the events Open, Close,
goes through during its lifetime in Lock and Unlock. Notice that not all
response to events. As an example, events are valid in all states; for
the following state machine diagram example, if a door is opened, you
shows the states that a door goes cannot lock it until you close it. Also
through during its lifetime. notice that a state transition can have
a guard condition attached: if the door
is Opened, it can only respond to the
Close event if the condition
• doorWay->isEmpty is fulfilled. The
syntax and conventions used in state
machine diagrams will be discussed in
full in the following sections.
State Machine Diagrams ASET
• Initial and Final States - The initial state is denoted by a filled black circle
and may be labeled with a name. The final state is denoted by a circle with
a dot inside and may also be labeled with a name.
State Machine Diagrams ASET
• Transitions - Transitions from one state to the next are denoted by lines
with arrowheads. A transition may have a trigger, a guard and an effect, as
below.
• Self-Transitions - A state can have a transition that returns to itself, as in
the following diagram. This is most useful when an effect is associated
with the transition.
Alternative way to
show the same
information
•The ∞ symbol indicates that details of the Check PIN
sub-machine are shown in a separate diagram.
State Machine Diagrams ASET