Unit 2
Unit 2
Sale
Date time
1
1..* Contains
Product spec
Sale Line item
Quantity Description
* Described by 1
Creating a Sales line item
Register sale
Sale
lineitem
Problem
What is a general principle of assigning responsibilities to
objects?
Solution:
Assign a responsibility to the information expert-the class
that has the information
necessary to full fill the responsibility.
• What information is needed to determine the grand total? A
Sale instance contains these; therefore, by the guideline of
Information Expert, Sale is a suitable class of object for this
responsibility.
• The Sales Line ltem knows its quantity and its associated
Product Specification; therefore, by Expert, Sales Line ltem
should determine the subtotal; it is the information expert.
t:=gettotal() 1*:st=getsubtotal()
Sale SaleLineitem
sale Salelineitem
p:=getprice()
Product
specification
sale
Register sale
1.1Create()
payment
HIGH COHESION
Cohesion
Cohesion is a measure of how strongly related and focused
the responsibilities of an element
are. An element with highly related responsibilities, and
which does not do a tremendous amount of work, has high
cohesion. These elements include classes, subsystems, and
so on.
Problem
How to keep objects focused, understandable, and
manageable, and as a side effect, support Low Coupling?
Solution:
Assign a responsibility so that cohesion remains high.
A class with low cohesion does many unrelated things, or
does too much work. Such classes are undesirable; they
suffer from the following problems:
✓ Hard to comprehend
✓ Hard to reuse
✓ Hard to maintain
Example
Assume that a Payment instance is to be created and associate
it with the Sale. What class should be responsible for this?
Since Register records a Payment in the real-world domain,
the Creator pattern suggests Register as a candidate for
creating the Payment. The Register instance could then send
an addPayment message to the Sale, passing along the new
Payment as a parameter.
Register Creates Payment
Register
sale
makepayment() Create()
payment
addpayment()
CONTROLLER
A Controller is the first object beyond the UI layer that is
responsible for receiving or handling a system operation
message.
Problem
What first object beyond the UI layer receives and
coordinates(controls) a system operation?
Solution:
Assign the responsibility to a class representing one of the
following choices,
✓ Represents the overall system, “a root object”, a device that
the software is running within, or a major subsystem.
✓ Represents a use case scenario within which the system
event occurs.
Example: NextGen POS application
System
Endsale()
Enteritem()
makeNewsale()
Makepayment()
Controller Class
System
Register
Endsale()
Endsale()
Enteritem()
Enteritem()
Makepayment()
Makepayment()
makeNewreturn()
makeNewreturn()
Enterreturnitem()
Enterreturnitem()
MakeReservationController
ManagescheduleHandler
ManagesfaresHandler
Circle Triangle
Get area() Get area()
• Pattern name
• Problem
• Solution
• Consequences
Pattern Name
•A handle used to describe:
• a design problem,
• its solutions and
• its consequences
• Enhances communication
Problem
Factory Method:
method in a derived class creates associations
Abstract Factory:
Factory for building related objects
Builder:
Factory for building complex objects incrementally
Prototype:
Factory for cloning new instances from a prototype
Singleton:
Factory for a singular (sole) instance
Structural Patterns
Adapter:
Translator adapts a server interface for a client
Bridge:
Abstraction for binding one of many implementations
Composite:
Structure for building recursive aggregations
Decorator:
Decorator extends an object transparently
Facade:
simplifies the interface for a subsystem
Flyweight:
many fine-grained objects shared efficiently.
Proxy:
one object approximates another
Behavioral Patterns
Chain of Responsibility
request delegated to the responsible service provider
Command:
request is first-class object
Iterator:
Aggregate elements are accessed sequentially
Interpreter:
language interpreter for a small grammar
Mediator:
coordinates interactions between its associates
Memento:
snapshot captures and restores object states privately
Observer:
dependents update automatically when subject changes
State:
object whose behavior depends on its state
Strategy:
Abstraction for selecting one of many algorithms
Template Method:
algorithm with some steps supplied by a derived class
Visitor:
operations applied to elements of a heterogeneous object
structure
Benefits of Design Patterns