GRASP (object-oriented design)

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

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

  • 5.1 connection High Cohesion / Low Coupling

Information Expert

Give the information to experts, the responsibility, ie the class which has the necessary information to carry out the responsibility. For example, a class code, the method has the berechneFläche (), which is calculated from the private attribute the radius surface. Negative example: A class berechneFläche that has a method that takes a geometric form and calculates their area. In contrast to the real world in which a circle does not do anything, must be in the object-oriented world, the object have all the methods that define the actions that can be done with him. Also known as " Do it Myself" strategy (Peter Coad ). Expert information is the basic principle of object-oriented design and can also be referred to as encapsulation of data. The consistent use leads to low coupling (low coupling ) and high cohesion (high cohesion ).

Creator

The production principle determines who should create an instance of a class ( object). New objects of class A should be produced by B if:

  • B is an aggregation of A
  • B contains A objects
  • A captured B - objects
  • B has the initialization data for A ( ie, B is an expert with respect to production of A )

Controller

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

There are two possibilities here. The use of use case controllers or facade controllers. In use case controllers all events of a use case are covered in a class. Mini use cases can also be treated in a controller, for example, the creation and deletion of a user. It is only important that the cohesion of the controller is as large as possible. The facade controller is used in message handling systems since arriving here all system events in one place. Here, a single controller ( Message Handler ) is defined to intercept all events. To this end, working with the Command Pattern.

Low Coupling

Low coupling is one of the main objectives of good design. Coupling referred to herein, the degree of the dependence of an element ( such as a class ) of the environment (for example out of the other classes). The main advantages are the following:

  • Because changes pull easy adaptability in a class, no changes in other classes according to
  • Comprehensibility of the class, because the context does not need to be considered
  • Good testability
  • High reusability

Forms of coupling

Using the example of the class X to class Y:

  • X is a direct or indirect subclass of Y
  • X implements the interface of Y
  • X has attribute or reference of type Y (aggregation / association )
  • X has method that Y referenced ( dependence)

High Cohesion

High cohesion is especially important in order to limit the complexity of complete systems by organizing classes well manageable. Cohesion is a measure of the internal cohesion of a class. This means it measures how closely the work methods and attributes of a class.

A negative example would be, for example, a class that offers methods from two completely different areas. Such classes are usually to locate quickly by completely meaningless names and many methods / lines of code.

Connection High Cohesion / Low Coupling

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

Polymorphism

Polymorphism can be used to control the behavior depends on the change type. Thus, many case distinctions can be avoided. Better known is the pattern as Strategy ( GoF ).

Pure Fabrication

A Pure Fabrication ( pure fiction ), represents a class that does not exist in the problem domain. It provides a method available, for she is not an expert. Normally, a Pure Fabrication is used to encapsulate an algorithm that does not fit any domain class. It can be used for example to separate technology knowledge of domain knowledge. It implements pure conduct and thus has no state. Should not be used too frequently, otherwise exist at the end only encapsulate the individual methods classes.

Indirection

Indirection ( detour ) can be used to achieve low coupling. This is achieved by an intermediary between the client and the server is installed. It makes sense when a server object is constantly changing. A disadvantage of the performance is reduced. An example of this pattern, the introduction of the controller component that between the data model ( Model) and its presentation ( View) in the Model-View- Controller architecture pattern is taught.

Protected Variations

Interfaces should always hide various concrete implementation. So you use polymorphism and delegation to switch between implementations. This allows the rest of the system to be protected from the effects of a change in implementation.

277697
de