1、GRASP原则一: 创建者 Creator
Who should be responsible for creating a new instance of some class
由谁来负责创建某个类的新实例(对象)
1.1 GRASP 原则
GRASP
General Responsibility Assignment Software Patterns
初学对象技术的同学,在编码或绘制交互图和类图时,应该理解并应用 GRASP的内在思想,
以便尽快地掌握这些基本原则,它们是设计OO系统 的基础
GRASP原则可以帮助设计人员理解面向对象设计的本质,并以一种有条理 的、理性的、
可解释的方式应用这些设计原则
The GRASP patterns are a learning aid to help one understand essential object design,
and apply design reasoning in a methodical, rational, explainable ways
GRASP 原则共9条
Creator
Information Expert
Low Coupling
Controller
High Cohesion
Polymorphism
Indirection
Pure Fabrication
Protected Variations
1.3 GRASP rule1: Creator (创建者)
Name: Creator
Problem:
Who should be responsible for creating a new instance of some class?
Solution:
Assign class B the responsibility to create an instance of class A if one of these is true (the more the better) :
1. B “contains” or compositely aggregates A
2. B records A
3. B closely uses A
4. B has the initializing data for A that will be passed to A when it is created (B is an expert with respect to A)
如果有一个以上的选项适用,通常首选聚集或包含A的类 Note: B and A refer to software objects, not domain model objects