Lecture 9 2023
Lecture 9 2023
PRINCIPLES
Dr. Salome Maro
12th June 2023
RECAP
Collaboration diagrams:
is a graph showing a
number of objects and the
links between them, which
in addition shows the
messages that, are passed
from one object to
another.
OBJECT/CLASS
RESPONSIBILITIES
What responsibility should a certain object do?
So, assign those responsibilities to o for which o has the information to fulfil
that responsibility
INFORMATION EXPERT
Expert leads to designs where a software object does those operations which
are normally done to the real-world (or domain) object it represents; this is
called the “DO it Myself” strategy.
The use of the Expert pattern maintain encapsulation, since objects use their
own information to fulfill responsibilities.
INFORMATION EXPERT –
EXAMPLE
Consider VideoStore and Video in that store.
VideoStore has an aggregation association with Video. I.e, VideoStore is the
container and the Video is the contained object.
Assume we need to get all the videos of a VideoStore.
Since VideoStore knows about all the videos, we can assign this responsibility
of giving all the videos can be assigned to VideoStore class.
VideoStore is the information expert.
INFORMATION EXPERT
EXAMPLE
SuppD o c s
do c ID 1 ...* uplo ad Ve nue
CV *
do c D e s c
D ate U plo ade d ve nue N am e L o c atio n
1
Inte rvie we e lo c atio nN am e
C e rtific a te s 1 ...* 1
Sys U s e r fN am e H as
m N am e 1
fN am e SirN am e
m N am e 1 ...* Inte rvie w
Inte rvie we r G e nde r
SirN am e inte rvie wN o Inte rvie wN o
G e nde r P as s wo rd Inte rvie wD e s rp
Sys Adm in U s e rnam e e m ail 1 c re ate dD ate J o bD e s c riptio n
P as s wo rd pho ne inte rvie wC re ato r 1
I n terv iew er e m ail Tim e jo bID
pho ne 1 de s c riptio n
1
Ans we rM arks Inte rvie wQ ue s t 1
Q ue s tAns we rs 1...*
m arks Q ue s tN o
1 Ans we r 1
D ate C re ate d Q ue s tD e s c *
D ate C re ate d
Q ue s tio nC re at D ate C re ate d
1 Ans we rC re ato r 1 ...* Spe c ializatio n
or Q ue s tio nC re ato r
Q ue s tio nType
Spe c ializatio nID
type ID D e s c riptio n
D e s c riptio n
Inte rvie wG rade
grade
rank
CREATOR
The creation of objects is one of the most common activities in an OO system.
Who creates an Object? Or who should create a new instance of some class?
“Container” object creates “contained” objects.
Decide who can be creator based on the objects’ association and their
interaction.
CREATOR
Pattern Name Creator
Solution Assign class B the responsibility to create an instance of class A if one of these is true:
1. B aggregates A
2. B contains A
3. B records A
4. B closely uses A
5. B has the initializing data for A
Thus, B is an expert with respect to creating A objects
Problem What should be responsible for creating a new instance of some class?
CREATOR EXAMPLE
Consider VideoStore and Video in that store.
VideoStore has an aggregation association with Video. I.e, VideoStore is the container, and the
Video is the contained object.
So, we can instantiate video object in VideoStore class
CREATOR EXAMPLE
LOW COUPLING
Coupling is a measure of how strongly one class is connected to, has knowledge of, or relies
on other classes.
A class with low (or weak) coupling is not dependent on too many other classes.
A class with high (or strong) coupling relies upon many other classes.
LOW COUPLING
m a ke P a ym e nt 1:c re a te ()
The second design is preferable :P O S T p :P aym e n t
because it does not need an extra
2:a ddP a ym e nt(p)
link formed between POST and :S ale
Payment.
:P aym e nt
LOW COUPLING
Classes with high (or strong) coupling are undesirable; they suffer
from the following problems:
Changes in related classes force local changes, i.e. changes in this class.
Changes in this class force the changes in many other related classes.
Harder to understand in isolation.
Harder to reuse as its reuse requires the use of the related class.
COHESION –
ge tFlo o r()
EXAMPLE m o ve Lift()
se tTrip()
e m e r g e nc yP r o c e dur e ()
r ai se Al ar m ()
update L e d()
o pe nD o o r s ()
c l o se D o o r s ()
re se t()
startU p()
shutD o wn()
di spl ayL o g ()
HIGH COHESION – EXAMPLE
Note that a class should represent Alarm
one “thing” from the real would. c a n ra ise rais e ()
L ift c a n w rite to
Key separate abstractions from m o ve ()
liftController are: update L e d()
c a n w rite to
Log
An Alarm ge tFlo o r()
dis play()
Lift Door c le ar()
Doors o pe n()
Logs c lo s e ()
HIGH COHESION
The benefits from the use of the High Cohesion Pattern include:
Clarity and ease of comprehension of the design is increased.
Maintenance and enhancements are simplified.
Low coupling is often supported.
Supports reuse.
CONTROLLER
Deals with how to delegate the request from the UI layer objects to domain layer objects.
When a request comes from UI layer object, Controller pattern helps us in determining what is
that first object that receive the message from the UI layer objects.
This object is called controller object which receives request from UI layer object and then
controls/coordinates with other object of the domain layer to fulfill the request.
It delegates the work to other class and coordinates the overall activity.
CONTROLLER
We can make an object as Controller, if
– Object represents the overall system (facade controller)
– Object represent a use case, handling a sequence of operations (session
controller).
CONTROLLER
Introduce a new class, and make it sit between the actor and the business
classes.
The name of this controller is usually called <name>Handler.
Handler reads the commands from the user and then decides which classes the
messages should be directed to.
The handler is the only class that will be allowed to read and display.
CONTROLLER EXAMPLE
BLOATED CONTROLLERS
Controller class is called bloated, if
The class is overloaded with too many responsibilities.
Solution – Add more controllers
Benefits:
High cohesion,
low coupling and can reuse this class
PURE FABRICATION –
EXAMPLE
Classes in a domain model of a banking application:
Account,
Branch,
Cash,
Check,
Transaction, etc.
The domain classes need to store information about the customers.
PURE FABRICATION –
EXAMPLE
The domain classes need to store information about the customers.
Option 1: delegate data storage responsibility to domain classes.
This option will reduce the cohesiveness of the domain classes (more than
one responsibility).
Option 2: Introduce another class which does not represent any domain
concept.
Introduce a class called, PersistenceProvider.
This class does not represent any domain entity.
The purpose of this class is to handle data storage functions.
Therefore PersistenceProvider is a pure fabrication.
PURE FABRICATION EXAMPLE
In e-commerce systems we often have need to convert one currency to another.
Sometimes it is hard to say where this behavior should be placed so the best
option is to create new class called currencyConverter whose only job is to
make conversions
INDIRECTION
How can we avoid a direct coupling between two or more elements.
Indirection introduces an intermediate unit to communicate between the other
units, so that the other units are not directly coupled.
Benefits: low coupling
Even if the first sketches of a model are done using a whiteboard (drawing the
models manually), the work of maintaining, synchronizing, and providing
consistency in a number of diagrams is almost impossible without a tool.
WISH LIST FOR MODELLING
TOOLS
Wish list for features of a modelling tool:
Drawing
Repository
Navigation
Provide multi-user support
Generate code
Reverse engineer
Integrate with other tools
Cover models of all abstraction levels
Interchange models