GRASP

from Wikipedia, the free encyclopedia

GRASP ( General Responsibility Assignment Software Patterns ) describes a set of design patterns with which the responsibility of certain classes of object-oriented systems is determined.

So they generally describe which classes and objects should be responsible for what. All of these rules have long been known, they were simply systematically described by Craig Larman. This facilitates communication between software developers and makes it easier for beginners to develop an awareness of good or bad code.

Information Expert

Gives the information expert the responsibility, i. H. the class that has the necessary information to assume responsibility. Example: A class Circle that has the method calculateArea () that calculates the area from the private attribute Radius . Negative example: A class calculateArea that has a method that takes a geometric shape and calculates its area. In contrast to the real world, in which a circle does nothing, in the object-oriented world the object must have all the methods that define actions that can be performed with it.

Also known as the “Do it Myself” strategy (Peter Coad). Information Expert is the basic principle of object-oriented design and can also be referred to as the encapsulation of data. The consistent use leads to loose coupling and high cohesion.

Creator

The producer principle defines who should create an instance of a class (object). New objects of class B should be created by A if:

  • A is an aggregation of B.
  • A contains B objects
  • A B objects captured
  • A B objects with strong coupling used
  • A has the initialization data for B (i.e. A is an expert on creating B)

Controller

The controller (control unit) contains the domain knowledge and defines who processes the system events intended for a non-user interface class.

There are two options here. The use of use case controllers or facade controllers. With use case controllers, all events of a use case are handled in one class. Mini use cases can also be handled in a controller, for example creating and deleting a user. It is only important that the cohesion of the controller is as great as possible. The facade controller is used in message handling systems, since all system events occur here in one place. A single controller (MessageHandler) is defined here, which intercepts all events. The command pattern is used for this.

Low coupling

Low coupling is one of the main goals of good design. Coupling here denotes the degree of dependence of an element (e.g. a class) on the environment (e.g. on other classes). The main advantages are as follows:

  • easy adaptability, since changes in one class do not result in changes in other classes
  • Comprehensibility of the class as the context does not have to be considered
  • good testability
  • high reusability

Forms of coupling

Using the example from class X to class Y:

  • X is a direct or indirect subclass of Y
  • X implements Y's interface
  • X has an attribute or reference of type Y (aggregation / association)
  • X has method that references Y (dependency)

High cohesion

A high level of cohesion is particularly important in order to limit the complexity of overall systems by organizing classes in a clearly structured manner. Cohesion is a measure of the internal cohesion of a class. That is, it measures how closely the methods and attributes of a class work together.

A negative example would be e.g. B. a class that offers methods from two completely different areas. Such classes can usually be located quickly using completely meaningless names and many methods / lines of code.

Relationship between high cohesion and low coupling

High cohesion within the software units in a system tends to lead to less coupling between the software units involved.

Polymorphism

Polymorphism can be used to change behavior depending on the type. Many distinctions between cases can thus be avoided. The pattern is better known as Strategy ( GoF ).

Pure fabrication

A Pure Fabrication ( pure fiction ), represents a class that does not exist as in the problem domain. It provides a method that it is not expert at. Usually pure fabrication is used to encapsulate an algorithm that does not fit into any domain class. It can be used, for example, to separate technology knowledge from domain knowledge. It implements pure behavior and therefore has no state. Should not be used too often, otherwise at the end there will only be classes that encapsulate individual methods.

Indirection

Indirection ( detour ) can be used to achieve low coupling. It is achieved by installing an intermediary between client and server. Useful when a server object is constantly changing. The disadvantage is the reduced performance. An example of this pattern is the introduction of the controller component, which mediates between the data model (model) and its presentation (view) in the model-view controller - architecture pattern .

Protected variations

Interfaces should always hide different concrete implementations. So you use polymorphism and delegation to switch between implementations. This can protect the rest of the system from the effects of a change in implementation.

literature

  • Mark Grand: Patterns in Java . 2nd Edition. John Wiley & Sons, 1999, ISBN 0-471-25841-5 .
  • Craig Larman: Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development . 3. Edition. Market and Technology, 2005, ISBN 3-8272-6898-2 .