Wednesday, September 2, 2020

4.2 Design Objects with Patterns




Object design is sometimes described as some variation of the following:
After identifying your requirements and creating a domain model, then add methods to the software classes, and define the messaging between the objects to fulfill the requirements.

Experienced object-oriented developers (and other software developers) build up a repertoire of both general principles and idiomatic solutions that guide them in the creation of software. These principles and idioms, if codified in a structured format describing the problem and solution, and given a name, may be called patterns.

For example, here is a sample pattern:

Pattern Name: Information Expert
Solution: Assign a responsibility to the class that has the information needed to fulfill it.
Problem It Solves: What is a basic principle by which to assign responsibilities to objects?

Most simply, a pattern is a named problem/solution pair that can be applied in new context, with advice on how to apply it in novel situations and discussion of its trade-offs.


4.2.1    GRASP
General Responsibility Assignment Software Patterns (or Principles), abbreviated GRASP, consists of guidelines for assigning responsibility to classes and objects in object-oriented design.

·         guides in assigning responsibilities to collaborating objects.

The different patterns and principles used in GRASP are:
1.      Creator
2.      Information expert
3.      Low Coupling
4.      Controller
5.      High Cohesion


Responsibility of GRASP:
Ø  ●Responsibility can be:–accomplished by a single object.–or a group of object collaboratively accomplish a responsibility.
Ø  ●GRASP helps us in deciding which responsibility should be assigned to which object/class.
Ø  ●Identify the objects and responsibilities from the problem domain, and also identify how objects interact with each other.
Ø  ●Define blue print for those objects – i.e. class with methods implementing those responsibilities.

1.         Creator:
●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.

Example for Creator
●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




2.   Information Expert:
●Given an object o, which responsibilities can be assigned to o?
●Expert principle says – assign those responsibilities to o for which o has the information to fulfill that responsibility.
●They have all the information needed to perform operations, or in some cases they collaborate with others to fulfill their responsibilities.

Example for Expert
●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.


3.   Low Coupling
●How strongly the objects are connected to each other?
●Coupling – object depending on other object.
●When depended upon element changes, it affects the dependant also.
●Low Coupling – How can we reduce the impact of change in depended upon elements on dependant elements.
●Prefer low coupling – assign responsibilities so that coupling remain low.
●Minimizes the dependency hence making system maintainable, efficient and code reusable
Two elements are coupled, if
–One element has aggregation/composition association with another element.
–One element implements/extends other element.
here class Rent knows about both VideoStore and Video objects. Rent is depending on both the classes.
Example for low coupling
●VideoStore and Video class are coupled, and Rent is coupled with VideoStore. Thus providing low coupling.


4.         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.

●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).
Benefits
–can reuse this controller class.
–Can use to maintain the state of the use case.
–Can control the sequence of the activities


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

4.       High Cohesion
●How are the operations of any element are functionally related?
●Related responsibilities in to one manageable unit.
●Prefer high cohesion
●Clearly defines the purpose of the element

Benefits
–Easily understandable and maintainable.
–Code reuse
–Low coupling





No comments:

Post a Comment