Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OBJECT DESIGN
Object Design:
In object design generally we consider following things:
1. Inputs to Object Design
2. Activities of Object Design
3. Outputs of Object Design
1
Prepared By: RC
In RDD, we think of software objects as having responsibilities-an abstraction of what they do.
The UML defines a responsibility as "a contract or obligation of classifier." Responsibilities
are related to the obligations or behavior of an object in terms of its role. Basically, these
responsibilities are of the following two types: doing and knowing
Doing responsibilities of an object include:
GRASP:
GRASP stands for General Responsibility Assignment Software Patterns. The name was
chosen to suggest the importance of grasping these principles to successfully design
object-oriented software.
Guides in assigning responsibilities to collaborating objects.
There are 9 GRASP patterns:
2
Prepared By: RC
1. Creator
2. Information Expert
3. Low Coupling
4. Controller
5. High Cohesion
6. Indirection
7. Polymorphism
8. Protected Variations
9. Pure Fabrication
Creator
The creation of objects is one of the most common activities in an object-oriented system.
Consequently, it is useful to have a general principle for the assignment of creation
responsibilities. Assigned well, the design can support low coupling, increased clarity,
encapsulation and reusability.
Problem: Who should be responsible for creating a new instance of some class?
Solution: Assign class B the responsibility to create an instance of class A if one
of these is true.
B contains or compositely aggregates A
B records A
B closely uses A
B has the initializing data for A that will be passed to A when it is
created. Thus B is an Expert with respect to creating A.
For 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
3
Prepared By: RC
Fig: Partial Domain model of video store
Since a VideoStore contains (in fact, aggregations) many Video objects, the Creator pattern
suggest that VideoStore is a good candidate to have the responsibility of creating Video
instances. This leads to design of object interaction as shown in figure below.
Information Export
A design model may define hundreds or thousands of software classes, and an application may
require hundreds or thousands of responsibilities to be fulfilled. During object design when the
interactions between objects are defined, we make choices about the assignment of
responsibilities to software classes. If we've chosen well, systems tend to be easier to
understand, maintain, and extend and our choices afford more opportunity to reuse components
in future applications.
By Information Expert, we should look for that class of objects that has the information needed
to determine the total.
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 fulfill the responsibility.
For Example:
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.
4
Prepared By: RC
Figure: Example for Information Expert
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. These elements include classes, subsystems, systems, and so on.
A class with high (or strong) coupling relies on many other classes. Such classes may be
undesirable. Some suffer from 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.
5
Prepared By: RC
Two elements are coupled, if
One element has aggregation/composition association with another element.
One element implements/extends other element.
Problem: How to support low dependency, low changes impact, and increased
reuse?
Solution: Assign a responsibility so that coupling remains low. Use this principle
to evaluate alternatives.
For example:
6
Prepared By: RC
VideoStore and Video class are coupled, and Rent is coupled with VideoStore. Thus providing
low coupling.
Controller
A controller is the first object beyond the UI layer that is responsible for receiving or handling
a system operation message. It 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, which 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.
Benefits
Can reuse this controller class.
Can use to maintain the state of the use case.
Can control the sequence of the activities
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 following
choices:
Object represents the overall system (facade controller)
Object represent a use case, handling a sequence of operations
(session controller).
For example:
Figure below shows the system with controller.
7
Prepared By: RC
Bloated Controllers
Controller class is called bloated, if
The class is overloaded with too many responsibilities.
Solution – Add more controllers
Controller class also performing many tasks instead of delegating to other class.
Solution – controller class has to delegate things to others.
High Cohesion
In terms of object design, cohesion (or more specifically, functional cohesion) is a measure of
how strongly related and focused the responsibilities of an element. An element with highly
related responsibilities that does not do a tremendous amount of work has high cohesion.
These elements include classes, subsystems and so on.
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
constantly affected by change.
Benefits
Easily understandable and maintainable.
Code reuse
Low coupling
For example:
8
Prepared By: RC
fig: Low cohesion
Here the student performs both display student detail as well as insert student. When we try to
do too many jobs with single object it may create poor coupling.
9
Prepared By: RC
Polymorphism
● Polymorphism guides us in deciding which object is responsible for handling those
varying elements.
● Benefits: handling new variations will become easy.
Pure Fabrication
● Fabricated class/ artificial class – assign set of related responsibilities that doesn't
represent any domain object.
● Provides a highly cohesive set of activities.
● Behavioral decomposed – implements some algorithm.
● Examples: Adapter, Strategy
● Benefits: High cohesion, low coupling and can reuse this class.
Problem: What object should have the responsibility, when we don’t want to
violate High Cohesion and Low Coupling or other goals but solutions
offered by Expert are not appropriate?
Solution: Assign a highly cohesive set of responsibilities to an artificial or
10
Prepared By: RC
convenience class that does not represent a problem domain concept--
something made up, to support high cohesion, low coupling and reuse.
Example
● Suppose we Shape class, if we must store the shape data in a database.
● If we put this responsibility in Shape class, there will be many database related operations
thus making Shape incohesive.
● So, create a fabricated class DBStore which is responsible to perform all database
operations.
● Similarly logInterface which is responsible for logging information is also a good
example for Pure Fabrication.
Indirection
● Indirection introduces an intermediate unit to communicate between the other units, so
that the other units are not directly coupled.
● Benefits: low coupling
● Example: Adapter, Facade, Observer
Problem: Where to assign a responsibility, to avoid direct coupling between two
(or more) things? How to de-couple objects so that low coupling is
supported and reuse potential remains higher?
Solution: Assign the responsibility to an intermediate object to mediate between
other components or services so that they are not directly coupled.
11
Prepared By: RC
Protected Variation
How to avoid impact of variations of some elements on the other elements.
It provides a well-defined interface so that the there will be no effect on other units.
Provides flexibility and protection from variations.
Provides more structured design.
Example: polymorphism, data encapsulation, interfaces
Problem: How to design objects, subsystems, and systems so that the variations or
instability in these elements does not have an undesirable impact on
other elements?
Solution: Identify points of predicted variation or instability; assign
responsibilities to create a stable interface around them.
12
Prepared By: RC
Fig: Attribute Visibility
Parameter Visibility:
Parameter visibility from A to B exits when B is passed as a parameter to a method of A. It is
a relatively temporary visibility because it persists only within the scope of the method. After
the attribute visibility it is the second most common form of visibility in object-oriented
systems.
For example, when the makeLineItem message is sent to a Sale instance, a ProductDescription
instance is passed as a parameter. Within the scope of the makeLineItem method, the Sale has
parameter visibility to a ProductDescription. Figure below shows this.
13
Prepared By: RC
For example, the second variation (assigning the returning object to a local variable) can be
found in the enterItem method of class Register as shown in figure below.
Adapter:
Polymorphism pattern, and its solution, is more specifically an example of the GoF Adapter
pattern.
Name: Adopter
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.
For example:
A solution is to add a level of indirection with objects that adapt the varying external interfaces
to a consistent interface used within the application. The solution is illustrated in Figure
14
Prepared By: RC
Factory
A common alternative in this case is to apply the Factory (or Concrete Factory) pattern, in
which a Pure Fabrication "factory" object is defined to create objects.
Factory objects have several advantages:
Separate the responsibility of complex creation into cohesive helper objects.
Hide potentially complex creation logic.
Allow introduction of performance-enhancing memory management strategies, such as
object caching or recycling.
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.
15
Prepared By: RC
Singleton
Name: Singleton
Problem: Exactly one instance of a class is allowed—it is a "singleton." Objects
need a global and single point of access.
Solution: Define a static method of the class that returns the singleton.
16
Prepared By: RC
Observer/Publish-Subscribe/Delegation Event Model
17
Prepared By: RC
Fig: Observer pattern
18
Prepared By: RC