GRASP design patterns

    Read the description of other patterns.

    GRASP (General Responsibility Assignment Software Patterns) - design patterns used to solve common tasks for assigning responsibilities to classes and objects.

    There are nine known GRAPS templates, originally described in Craig Larman's book, Using UML and Design Patterns. Unlike the patterns familiar to the reader from the Gang of Four, GRAPS patterns do not have a pronounced structure, a clear scope and a specific problem to be solved, but only represent generalized approaches / recommendations / principles used in designing the system design.

    Consider the characteristics of the main GRASP templates.

    Subject area


    For a more detailed understanding by the reader of GRASP templates, examples of their use from the subject area of ​​sea freight will be described.

    Navigator + shipping company is the largest provider of sea freight services. The head office of the company consists of three departments: a customer service department, a flight dispatching department, and a flight picking department. Managers from the customer service department draw up contracts for the delivery of goods from point A to point B. Dispatchers from the flight dispatch department monitor the position of the vessels. Administrators from the flight picking department form cargo flights based on agreements with customers.

    Information Expert


    The information expert template is the basic and at the same time the most obvious of the nine. The information expert describes the fundamental principles for assigning responsibilities to classes and objects. According to the description, an information expert (an object endowed with some responsibilities) is an object that has the maximum information necessary to fulfill the assigned duties.

    As an example, we can consider the calculation of the total amount of cargo and its value both on a particular flight, and summarized for all flights of the company.


    Using the template, the information expert increases the connectivity of the modules and does not contradict the encapsulation property.

    Low Coupling and High Cohesion


    Perhaps in any literature on object-oriented design, these two concepts are found. It is believed that any designed system should satisfy the principles of low connectivity and high meshing of modules. Compliance with these templates makes it easy to modify and maintain the program code and also increases the degree of its reuse.

    Consider the concept of a measure of connectedness of modules and a measure of linking of a module. A measure of module connectivity is determined by the amount of information that one module has about the nature of another. In turn, the measure of engagement of a module is determined by the degree of focus on its responsibilities.

    It is worth noting that there are methodologies according to which the measures of connectivity and engagement can be evaluated on a scale of 1 to 10 for a particular case. However, they are not considered within the framework of this article.

    An example of a good system design is the GNU Binutils Utility Suite for Linux. In which, each utility (if viewed as a module) performs only minimal duties (high gearing) and knows almost nothing about the nature of other utilities (low connectivity), and therefore can be easily replaced with an analogue in some use case.

    Resistant to Changes (Protected Variantions)


    The problem of modifying the system is most relevant in conditions of dynamically changing requirements. Often, it is possible to distinguish the so-called instability points of the system that will most often be subject to change / modification. Then, the essence of the template resistant to changes is to eliminate instability points by defining them as interfaces and implementing various behaviors for them.

    For example, consider the situation of expanding the navigator + services sprektra of services. We assume that the shipping company began to engage in passenger traffic. To do this, we implement the stability point of the system - the transported entity (Entity).


    Controller


    The controller is responsible for processing input system events, delegating responsibilities for their processing to competent classes. In general, a controller implements one or more use cases .

    The concept of an external controller (Front Controller) is known, which represents the entire system as a whole (aggregates all the functionality of the system in one class).

    An example of a controller is the Administrator class, which implements the use of the system by the administrator from the flight configuration department.


    Using controllers allows you to separate the logic from the presentation, thereby increasing the possibility of code reuse.

    Polymorphism (Polymorphism)


    The polymorphism pattern allows you to handle alternative behaviors based on type. In this case, alternative implementations are reduced to a generalized interface.

    Consider the integration of the system with external components of the calculation of tariffs for cargo transportation. The LocalTarificator and WorldWideTarificator classes are adapters to the corresponding external components.


    The use of the polymorphism pattern makes it possible to easily modify the system in the future.

    Pure Fabrication


    There is the concept of a programming model for a subject area, according to which, each entity from the subject field corresponds to one or more classes of the software environment. In this case, the responsibilities of the interaction of entities, as a rule, are imposed on themselves. This approach has an obvious drawback - the high connectivity of the system modules. The pure invention template allows you to solve this problem by introducing an additional class (not reflecting the real nature of the subject area) into the program environment and providing it with the required responsibilities.

    For example, the obligation to store customer information for a shipping company can be assigned to the fictitious class ClientsSaver.


    The use of the net invention template allows the system design to comply with the principles of low connectivity and high engagement.

    Redirection (Indirection)


    The redirection pattern implements low connectivity between classes by assigning responsibilities for their interaction to an additional object - an intermediary.

    An example of this template is the ClientsSaver class (see Pure invention), which is an intermediate layer between client entities and the storage in which they will be stored. In addition, the controller from the MVC triad is an intermediary between the data and their presentation.

    Also popular now: