Ooad Unit IV
Ooad Unit IV
GRASP: Designing objects with responsibilities – Creator – Information expert – Low Coupling
– High Cohesion – Controller Design Patterns – creational – factory method – structural –
Bridge – Adapter – behavioural – Strategy – observer –Applying GoF design patterns – Mapping
design to code.
DESIGN PATTERNS
CREATOR
Creation of objects is one of the most common activities in an object oriented system. Which
class is responsible for creating objects is a fundamental property of relationship between objects
of particular classes.
Problem
Solution:
Assign class B the responsibility to create an instance of a class A if one of these is true,
INFORMATION EXPERT
Problem
LOW COUPLING
Coupling is a measure of how strongly one element is connected to, has knowledge of, or relies
on other elements. An element with low (or weak) coupling is not dependent on too many other
elements.
A class with high (or strong) coupling relies on many other classes. Such classes may be
undesirable; some suffer from the following problems,
Forced local changes because of changes in related classes.
Harder to understand in isolation.
Harder to reuse because its use requires the additional presence of the classes on which it
is dependent.
Problem
How to support low dependency, low change impact, and increased reuse?
Solution:
Assign a responsibility so that coupling remains low.
Eg: Partial Class domain
Assume that a Payment instance is to be created and associated with the Sale. Since a 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.
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
Delicate; constantly affected by change.
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 addPayrnent message to the Sale, passing along the new Payment as a parameter.
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:
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.
During design, a controller class is assigned the responsibility for system operation.
The system Operations identified during system behaviour analysis are assigned to one or more
controller classes, such as Register,
Bloated Controller
Poorly designed, a controller class will have low cohesion. unfocused and handling
too many areas of responsibility; this is called a bloated controller.
Add more controllers-a system does not have to have only one. For example, consider an
application with many system events, such as an airline reservation system.
Design the controller so that it primarily delegates the fulfillment of each system
operation responsibility on to other objects.
DESIGN PATTERNS
Design patterns are termed as reusable solutions for commonly occurring problems in
software designs.
Design Patterns are descriptions of communicating objects and classes customized to
solve a general design problem in a particular context.
Design Patterns identifies the participating classes and instances, their roles and
collaborations, and the distribution of responsibilities.
1. Creational Pattern
Factory Method
2. Structural Patterns
Bridge
Adapter
3. Behavioral Patterns
Strategy
Observer
CREATIONAL PATTERN
Creational design patterns provide a way to create objects while hiding the creation logic, rather
than instantiating objects directly using new operator. This gives program more flexibility in
deciding which objects need to be created for a given use case.
FACTORY METHOD
Name: Factory
Problem: Who should be responsible for creating objects when there are special considerations, such
as complex creation logic, a desire to separate the creation responsibilities for better cohesion, and so
forth? Solution: Create a Pure Fabrication object called a Factory that handles the creation.
In the below diagram, In ServicesFactory, the logic to decide which class to create is resolved by
reading in class name from an external source and then dynamically loading the class. This is termed
as Partial Data Driven Design.
STRUCTURAL PATTERNS
Structural patterns are concerned with how classes and objects are composed to form larger
structures. These patterns describe ways to compose objects to realize new functionality.
BRIDGE PATTERN
Bridge is used, when we need to decouple an abstraction from its implementation so that
the two can vary independently. This type of design pattern comes under structural
pattern as this pattern decouples implementation class and abstract class by providing a
bridge structure between them.
This pattern involves an interface which acts as a bridge which makes the functionality
of concrete classes independent from interface implementer classes. Both types of
classes can be altered structurally without affecting each other.
Problem
How to resolve incompatible interfaces, or provide a stable interface to similar
components with different interfaces?
Solution:
Convert the original interface of a component into another interface, through an
intermediate adapter object.
Behavioral Patterns are concerned with communication between objects. These patterns use
inheritance to distribute behavior between classes.
STRATEGY PATTERN
Problem
How to design for varying, but related, algorithms or policies? How to design for
the ability to change these algorithms or policies?
Solution:
Define each algorithm/policy/strategy in a separate class, with a common interface.
Example:
In this example we are going to create a Strategy interface defining an action and concrete
strategy classes implementing the Strategy interface. Context is a class which uses a
Strategy.
StrategyPatternDemo, a demo class, will use Context and strategy objects to demonstrate
change in Context behaviour based on strategy it deploys or uses.
Problem
Different kinds of subscriber objects are interested in state changes or events of a
publisher object,and want to react in their own unique way when the publisher generates an
event. Moreover,the publisher wants to maintain low coupling to the subscribers. What to
do?
Solution:
Define a “subscriber” or “listener” interface. Subscribers implement this interface.
The publisher can dynamically register subscribers who are interested in an event and
notify them when an event occurs.