General Responsibility Assignment Software Patterns or Principles (GRASP)

来源:互联网 发布:共享单车盈利模式 知乎 编辑:程序博客网 时间:2024/06/05 19:41

       Understanding responsibilities is key to good object-oriented design.

                                                                                                  ---MartinFowler

 

General Responsibility Assignment Software Patterns or Principles (GRASP)

Pattern/Principle

Description

Information 

Expert 

   A general principle of object design and responsibility assignment?

   Assign a responsibility to the information expert the class that has the information necessary to fulfill the responsibility.

Creator

  Who creates? (Note that Factory is a common alternate solution.)

  Assign class B the responsibility to create an instance of class A if one of these is true:

1. B contains A

4. B records A

2. B aggregates A

5. B closely uses A

3. B has the initializing data for A

Controller

  What first object beyond the UI layer receives and coordinates ("controls") a system operation?

  Assign the responsibility to an object representing one of these choices:

  1. Represents the overall "system," a "root object," a device that the software is running within, or a major subsystem (these are all variations of a facade controller).
  2. Represents a use case scenario within which the system operation occurs (a use-case or session controller)

Low Coupling (evaluative)

  How to reduce the impact of change?

  Assign responsibilities so that (unnecessary) coupling remains low. Use this principle to evaluate alternatives.

High Cohesion (evaluative)

  How to keep objects focused, understandable, and manageable, and as a side-effect, support Low Coupling?

  Assign responsibilities so that cohesion remains high. Use this to evaluate alternatives.

Polymorphism

  Who is responsible when behavior varies by type?

  When related alternatives or behaviors vary by type (class), assign responsibility for the behavior using polymorphic operations to the types for which     the behavior varies.

Pure Fabrication

  Who is responsible when you are desperate, and do not want to violate high cohesion and low coupling?

  Assign a highly cohesive set of responsibilities to an artificial or convenience "behavior" class that does not represent a problem domain concept something made up, in order to support high cohesion, low coupling, and reuse.

Indirection

  How to assign responsibilities to avoid direct coupling?

  Assign the responsibility to an intermediate object to mediate between other components or services, so that they are not directly coupled.

Protected Variations

  How to assign responsibilities to objects, subsystems, and systems so that the variations or instability in these elements do not have an undesirable   impact on other elements?

  Identify points of predicted variation or instability; assign responsibilities to create a stable "interface" around them.