DEV Community

ltv2563
ltv2563

Posted on

Everything About GRASP OO Design

1, What is GRAP design?
GRAP stands for General Responsibility Assignment Software Patterns or (Principles), which includes rules for assigning responsibilty to Objects and classes in object-oriented design (OO design).

GRAP There are 9 principles as follows:

Creator.
Information Expert.
Low Coupling.
Controller.
High Cohesion.
Indirection.
Polymorphism.
Protected Variations.
Pure Fabrication.
All of these guidelines will fix a number of software issues and are common to virtually every software development project.

GRAP will help you decide which function to include in the class / object, which will help you identify the domain when the system has a problem, ...

2, Rules
Who creates an Object? Or who should create a new instance of some class?
The principle says you need to determine where the object should be declared and where its instances should be raised.

Example: We have 2 classes Book and BookStore. BookStore is a store that contains all the books that the book needs, so it should be an instance of BookStore.

Information Expert.
Information expert (also expert or the expert principle) is a principle used to determine where to delegate responsibilities

This principle is used to determine where a function is declared.

For example, to retrieve information of all the Books in the library, we call this action a getAllBooks () method, based on the Information Expert rule, we should put the getAllBooks () method in the BookStore class. .

Low Coupling.
Coupling is a measure of how strongly one element is connected to, has knowledge of, or relies on other elements. Low coupling is an evaluative pattern that dictates how to assign responsibilities to support.

This principle says we need to reduce our dependencies so that our application is more reusable, less affected by any required changes.

Controller.
The controller pattern assigns the responsibility of dealing with system events to a non-UI class that represents the overall system or a use case scenario. A controller object is a non-user interface object responsible for receiving or handling a system event.

The controller rule says we will assign the responsibility of handling system events to a class, this class is used to receive and handle events on the system. This is the controller in MVC.

High cohesion.
High cohesion is an evaluative pattern that attempts to keep objects appropriately focused, manageable and understandable

This principle says we need to keep our audiences focused in an appropriate, manageable and understandable way. This principle was born to support the low coupling principle.

Polymorphism.
This principle is polymorphism in OOP - object-oriented.

Pure Fabrication.
A pure fabrication is a class that does not represent a concept in the problem domain, specially made up to achieve low coupling, high cohesion, and the reuse potential acquired derived (when a solution presented by the information expert pattern does not).

A pure fabrication is a class that does not represent a problem domain, especially created to make low coupling, high cohesion.

For example: Adapter classes.

Indirection
The indirection pattern supports low coupling (and reuse potential) between two elements by assigning the responsibility of mediation between them to an intermediate object.

This principle states that to reduce low coupling between objects, we can create an intermediate object to handle that.

Protected Variation
The protected variations pattern protects elements from the variations on other elements (objects, systems, subsystems) by wrapping the focus of instability with an interface and using polymorphism to create various implementations of this interface.

This principle says we need to protect components from variations on another component. That is packaging polymorphism in object orientation

Top comments (0)