0% found this document useful (0 votes)
60 views8 pages

GRASP

The document discusses GRASP (General Responsibility Assignment Software Patterns), which provides guidelines for assigning responsibilities in object-oriented design, including principles like Creator, Information Expert, Low Coupling, High Cohesion, and Controller. Each principle addresses specific design challenges, such as determining object creation responsibilities, delegating responsibilities based on information ownership, and maintaining low coupling and high cohesion for better maintainability and reusability. The document also includes examples and benefits associated with each principle to illustrate their application in software design.

Uploaded by

vedaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views8 pages

GRASP

The document discusses GRASP (General Responsibility Assignment Software Patterns), which provides guidelines for assigning responsibilities in object-oriented design, including principles like Creator, Information Expert, Low Coupling, High Cohesion, and Controller. Each principle addresses specific design challenges, such as determining object creation responsibilities, delegating responsibilities based on information ownership, and maintaining low coupling and high cohesion for better maintainability and reusability. The document also includes examples and benefits associated with each principle to illustrate their application in software design.

Uploaded by

vedaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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

Explain in detail about GRASP design patterns.

GRASP

 General Responsibility Assignment Software Patterns (or Principles) consists of guidelines


for assigning responsibility to classes and objects in object- oriented design.
 Pattern is a named description of a problem and solution that can be applied to new contexts;
ideally a pattern advises us on how to apply its solution in varying circumstances.

The different patterns and principles used in GRASP are

 Creator
 Information expert
 Low Coupling
 High Cohesion
 Controller.
1.Creator:
Creation of objects is one of the most common activities in an object-oriented system.

Problem
Who should create an instance of a new class?

Solution
If B is a creator of A objects then atleast one of the following must be true.
• B contains A
• B compositely aggregates A
• B records A
• B closely uses A

Example
In the POS application, who should be responsible for creating a SalesLineltem instance? Consider the
partial domain model of SaleLineItem. Here Sale takes the responsibility of creating SalesLineItem
instance. Since sale contains many SalesLineItem objects.
The common task of creator is to assign responsibilities related to the creation of objects.
All common relationships between classes are
 Composite aggregates part
 Container contains content
 Recorder records
Benefits
 Lower maintenance due to low coupling
 Higher opportunities for reuse.

2.Information expert
Information Expert is a principle used to determine where to delegate responsibilities. These
responsibilities include methods, computed fields and so on.

Problem
What is a general principle of assigning responsibilities to objects?
Solution
Assign the responsibilities to information expert.

Example
In the NextGEN POS application, some class needs to know the grand total of a sale. Consider the partial
Domain Model.

Software class sale is added with the responsibility of getting total with the method getTotal
To determine lineitem sub total we need,
SalesLineitem.quantity’
ProductDescription.price
therefore, by Expert, SalesLineltem should determine the subtotal; it is the information expert. In terms of
an interaction diagram, this means that the Sale needs to send getSubtotal messages to each of the
SalesLineltems and sum the results.

After knowing and answering subtotal, a SalesLineItem has to know product price. ProductDescription is
an information expert for answering price.

Finally, three design classes, assigned with three responsibilities to find sales total.
3.Low Coupling

Coupling
Coupling is the measure of how strongly one element is connected to the other elements.

Types of coupling
There are 2 types of coupling
1) Low coupling or weak coupling
2) High coupling or strong coupling.

Low coupling
An element if does not depend on too many other elements like classes, subsystems and so on.

High coupling
A class with high coupling relies on many other classes.
The problems of high coupling are
 Forced local changes
 Harder to understand
 Harder to reuse

Problem How to support low dependency, low change impact, and increased reuse?

Solution Assign a responsibility so that coupling remains low.

Example

Consider the following partial class diagram from a NextGen case study:

Design 1:
Suggested by creator
1) Register instance send addpayment message to sale, passing newPayment as a parameter.
2) Register class couples with payment class and creates payment.
Design 2:
Suggested by low coupling
1) Here sale does the creation of payment.
2) It does not increase coupling and hence preferred.

Benefits
 Not affected by changes in other components
 Simple to understand in isolation
 Convenient to reuse

4.High Cohesion
Cohesion is a measure of how strongly related and focused responsibilities of an element are.

Problem How to keep classes focused, understandable and manageable?


Solution Assign a responsibility so that cohesion remains high.

An element has high cohesion if it,


• Does not do tremendous work
• Has high responsibilities

Difficulties of low cohesion are


• Hard to comprehend
• Hard to reuse
• Hard to maintain

Example
Create a payment instance and associate it with sale
Design 1
 Register records a payment in real world
 Then it sends addpayment message to sale, passing newpayment as a parameter.

Design 2
In the second design, payment creation is the responsibility for sale.
• It is highly desirable because it supports
◦ High cohesion and
◦ Low coupling

Scenarios of varying degrees of functional cohesion


(1) Very low cohesion
A class is solely responsible for many things in different functional areas.
(2) Low cohesion
A class has sole responsibility for a complex task in one functional area.
(3) High cohesion
A class has moderate responsibilities in one functional area and collaborates with others.
(4) Moderate cohesion
A class has light weight. It is responsible for different areas logically related to the class concept .

Benefits of high cohesion are


1) Clarity
2) Ease of comprehension
3) Reuse of fine-grained, highly related functionality.
5.Controller

A controller is defined as the first object beyond the user interface (UI) layer that is responsible for the
receiving or handling a system operation message.

Problem
what first object beyond the UI layer that receives and coordinates a system operation?

Solution
Assign the responsibility of controller to an object representing one of the following choices
 Represents the overall system, device, or subsystem.
 Represents a use case scenario within which the system event occurs.

Example
NextGen application contains several system operations.

During the design the responsibility of system operations is done by the controller.
The controller are
1) Register POS system – Represents overall system.
2) Process Sale handler – Represents receiver/handler of system events.

You might also like