• ## GRASP原则

千次阅读 2012-01-03 11:49:41
来源:http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29

GRASP (object-oriented design)

General Responsibility Assignment Software Patterns (or  Principles), abbreviated GRASP, consists of guidelines for assigning responsibility to classes and objects inobject-oriented design.
The different patterns and principles used in GRASP are: Information Expert, Creator, Controller, LowCoupling, High  Cohesion,  Polymorphism, Pure Fabrication, Indirection, Protected Variations. All these patterns answer somesoftware problem, and in almost every case these problems are common to almost everysoftware development project. These techniques have not been invented to create new ways of working but to better document and standardize old, tried-and-testedprogramming principles in object oriented design.
It has been said that "the critical design tool for software development is a mind well educated in design principles. It is not theUML or any other technology".[1] Thus, GRASP is really a mental toolset, a learning aid to help in the design of object oriented software.

Contents
[hide]

1Patterns
Patterns
Creator

Factory pattern

Creation of objects is one of the most common activities in an object-oriented system. Which class is responsible for creating objects is a fundamental property of the relationship between objects of particular classes. Simply "Creator pattern is responsible for creating an object of class".
In general, a class B should be responsible for creating instances of class A if one, or preferably more, of the following apply:
Instances of B contains or compositely aggregates instances of AInstances of B record instances of AInstances of B closely use instances of AInstances of B have the initializing information for instances of A and pass it on creation.
Information Expert

Information hiding

Information Expert is a principle used to determine where to delegate responsibilities. These responsibilities include methods, computed fields and so on.
Using the principle of Information Expert a general approach to assigning responsibilities is to look at a given responsibility, determine the information needed to fulfill it, and then determine where that information is stored.
Information Expert will lead to placing the responsibility on the class with the most information required to fulfill it.[2]
Controller

Model–view–controller

The Controller pattern assigns the responsibility of dealing with system events to a non-UI class that represent the overall system or ause case scenario. A Controller object is a non-user interface object responsible for receiving or handling a system event.
A use case controller should be used to deal with all system events of a use case, and may be used for more than one use case (for instance, for use casesCreate User and Delete User, one can have one UserController, instead of two separate use case controllers).
It is defined as the first object beyond the UI layer that receives and coordinates ("controls") a system operation. The controller should delegate to other objects the work that needs to be done; it coordinates or controls the activity. It should not do much work itself. The GRASP Controller can be thought of as being a part of the Application/Service layer[3] (assuming that the application has made an explicit distinction between the Application/Service layer and the  Domain layer) in an object-oriented system with  common layers.
Low Coupling

Main article:
loose coupling

Low Coupling is an evaluative pattern, which dictates how to assign responsibilities to support:
low dependency between classes;low impact in a class of changes in other classes;high reuse potential;
High Cohesion

Main article:
cohesion (computer science)

High Cohesion is an evaluative pattern that attempts to keep objects appropriately focused, manageable and understandable. High cohesion is generally used in support of Low Coupling. High cohesion means that the responsibilities of a given element are strongly related and highly focused. Breaking programs into classes and subsystems is an example of activities that increase the cohesive properties of a system. Alternatively, low cohesion is a situation in which a given element has too many unrelated responsibilities. Elements with low cohesion often suffer from being hard to comprehend, hard to reuse, hard to maintain and adverse to change.[4]
Polymorphism

Main article:
polymorphism in object-oriented programming

According to Polymorphism, responsibility of defining the variation of behaviors based on type is assigned to the types for which this variation happens. This is achieved usingpolymorphic operations.
Pure Fabrication

service (systems architecture)

A Pure Fabrication is a class that does not represent a concept in the problem domain, specially made up to achieve low coupling, high cohesion, and the reuse potential thereof derived (when a solution presented by theInformation Expert pattern does not). This kind of class is called "Service" inDomain-driven design.
Indirection

delegation pattern

The Indirection pattern supports low coupling (and reuse potential) between two elements by assigning the responsibility of mediation between them to an intermediate object. An example of this is the introduction of a controller component for mediation between data (model) and its representation (view) in the  Model-view-controller pattern.
Protected Variations

Main article:
delegation pattern

The Protected Variations pattern protects elements from the variations on other elements (objects, systems, subsystems) by wrapping the focus of instability with aninterface and using  polymorphism to create various implementations of this interface.

展开全文
• GRASP模式，OO原则和设计模式的基础，原创不易，请珍惜下载
• 1、GRASP原则一: 创建者 Creator　 Who should be responsible for creating a new instance of some class 　由谁来负责创建某个类的新实例（对象）1.1 GRASP 原则 　 GRASP 　 General Responsibility...

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 Variations1.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

转载于:https://www.cnblogs.com/mayZhou/p/10550011.html
展开全文
• GRASP原则六: 多态 Polymorphism　 How to handle alternative behaviors based on type 如何处理依据类型不同而有 不同行为的一类需求 ？ 　 比如，开餐馆 　 苏州人喜欢甜、四川人喜欢麻、湖南人喜欢辣...

GRASP原则六: 多态 Polymorphism　　 How to handle alternative behaviors based on type 如何处理依据类型不同而有 不同行为的一类需求 ？
 比如，开餐馆
 苏州人喜欢甜、四川人喜欢麻、湖南人喜欢辣，咋处理 ？1.1 9条GRASP原则
 Information Expert
 responsibilities should be assigned to objects that contain relevant information
 Creator
 the creator of an object is usually an object that contains, or aggregates it
 High Cohesion
 responsibilities of a certain class must be highly related
 Low Coupling
 interdependency between classes should remain low
 Controller
 class which handles external system events
 Polymorphism 多态原则
 Indirection 间接原则
 Pure Fabrication 纯虚构原则
 Protected Variations 隔离变化1.2 Iteration 2 More Requirement
 第一次迭代结束时，完成了
 当前软件功能测试：单元测试、用户可接受测试、负载测试、可使用性测试等
 必须有客户加入，并给出反馈 Customers engaged in and feedback
 把基线稳定下来，发布内部版本 stabilized baseline internal release
 第二次迭代时，要考虑加入新的功能
 需求变化、业务规则细化、考虑更多的用例
 制定本次迭代的计划活动等
 Ex, Monopoly game
 When a player lands on the Go square, the player receives $200  When a player lands on the Go-To-Jail square, they move to the Jail square  When a player lands on the Income-Tax square, the player pays the minimum of$200 or 10%
of their worth1.2 Iteration 2 More Requirement
 例如POS系统，第二次迭代时增加另外的需求
 支持多种第三方服务的接口 Support for variations in third-party external services
 计算税费、信用卡授权认证等
 复杂的定价机制 Complex pricing rules
 GUI窗口在信息发生变化时得到更新 GUI window updates when information changes
 这些功能点，可能属于前轮迭代同样的用例，但更多的是讨论非功能性需求
 这些需求对领域模型的影响较小
 同样一项功能，原来一种处理方法就可以，现在需要适应多种处理方法，设 计方案该如何支持？
 比如付费：现金、储蓄卡、信用卡、支付宝、微信1.3 GRASP rule6: Polymorphism(多态)
 Name: Polymorphism(多态)
 Problem:
 如何处理依据类型不同而有不同行为的一类需求？
How to handle alternative behaviors based on type?
How to create pluggable software components?
 Solution:
 使用多态操作为依据类型变化的行为 进行职责分配
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
 Corollary(推论):
 不要去测试对象的类型或者条件逻辑，并以此选择相应的行为
Do not test for the type of an object and use conditional logic to perform varying alternatives
based on type
 即，不要使用条件逻辑，而是为不同的类定义相同名字的方法
That is, don’t use conditional logic, but assign the same name to services (methods) in
different classes
 不同的类实现了相同的接口、或者有一个共同的父类（继承）The different classes usually
implement a common interface or are related in an implementation hierarchy with a
common superclass

转载于:https://www.cnblogs.com/mayZhou/p/10550045.html
展开全文
• GRASP原则九: 隔离变化 　 Protected Variations 　 需求一定会变化的！如何做到以系统的局部变化为代价就可以应对这一点？4.1 GRASP rule9: Protected Variations 　 Name: Protected Variations 隔离...

GRASP原则九: 隔离变化
 Protected Variations
 需求一定会变化的！如何做到以系统的局部变化为代价就可以应对这一点？4.1 GRASP rule9: Protected Variations
 Name: Protected Variations 隔离变化
 Problem: 如何设计对象、系统和子系统，使得这些成分里面的变化因素、 不稳定因素不会对系统的其余
部分造成意想不到的影响？
How to design objects, systems and subsystems so that the variations and instability in these
elements does not have an undesirable impact on other elements?
 Solution: 标识出能够预计的变化点或者不稳定点，职责分配的时候创建一 个稳定的接口，把它们与
系统的其余部分隔离开来
Identify points of predicted variation or instability; assign responsibilities so that you create
a stable interface around them
 “隔离可能的变化”是一个设计原则，它鼓励使用一个稳定的接口来封装可以 预知的变更点。以下技巧都使
用了PV
 数据封装 、多态 、数据-驱动设计， 服务查询，配置文件、接口
 ….等都是这种机制的不同实现　　　　 隔离变化是一种高度抽象的原则，指导了设计和编程过程的大多数机制、模式programming4.3 Protected Variations: discussion
 需要注意的两种可能的变化点
 变化点: 在当前系统或者当前需求中已经存在了
Variation point: variations in the existing current system or requirements
 演化点: 推测的类型变化可能发生在今后，但在当前的需求中 不存在
Evolution point: speculative types of variation that may arise in the future, but which are not
present in the existing requirements

转载于:https://www.cnblogs.com/mayZhou/p/10550050.html
展开全文
• GRASP原则八: 间接 Indirection　 若两个对象直接连接，导致耦合太紧，如何解决？3.1 GRASP rule8: Indirection 间接 　 Name: Indirection 　 Problem: 把职责分配到哪里可以避免两个或者多个对象之间的 ...
• GRASP原则七: 纯虚构 Pure Fabrication　 如果依据信息专家原则获得的解决方案不合适，既不想违反低耦合、高内聚，也不想违 反其他的原则， 　该如何把职责分配给对象？ 　 左右为难….2.1 GRASP rule7: Pure...
• 2、GRASP原则二：信息专家 Information Expert　 What is a general principle of assigning responsibility to objects? 　为一个对象 分配职责的一般原则是什么？ 　 “鸡鸣狗盗” 战国时候，齐国的孟尝君...
• 4、GRASP原则四：控制器 Controller　 What first object beyond the UI layer receives and co-ordinates (controls) a system operation？ 　在领域层，由谁负责首先接收并协调来自UI层的系统操作？4.1 Mini ...
• GRASP原则五：高内聚 High Cohesion　 How to keep objects focused, understandable and manageable, and as a side effect support Low Coupling? 　如何使对象功能专注、可理解、可管理，同时又支持 低耦合？...
• 3、GRASP原则三: 低耦合 Low Coupling　 How to support low dependency, low change impact and increased reuse? 　 如何保证设计方案支持低的依赖性、低的变化影响度、增加可重用性？3.2 耦合的定义...
• GRASP（General Responsibility Assignment Software Pattern）是通用职责软件分配模式。...GRASP提出了九大原则，下面笔者将对这九大设计原则做一一阐述。 高内聚、低耦合（High Cohesion、Low C...
• GRASP原则：全称翻译为通用职责分配软件模式。称其为设计原则而不是设计模式，它站在面对对象设计的角度，告诉我们怎样设计空间中的类与分配它们的行为职责，以及明确类之间的相互关系等。它包含9个基本原则：创建...
• 要学习设计模式，有些基础知识是我们必须要先知道的，设计模式是关于...这时就需要比23种设计模式更重要更经典的GRASP模式登场了。 GRASP （General Responsibility Assignment Software Patterns） 中文名称为“通...
• 要学习设计模式，有些...GRASP（General Responsibility Assignment Software Patterns），中文名称为“通用职责分配软件模式”，GRASP一共包括9种模式，如何决定一个系统有多少对象，每个对象都包括什么职责，GRASP
• 重要的几个GRASP原则：1.控制器模式 2.创建者模式 （原则）3。信息专家模式（原则） 4. 高内聚 低耦合 这里所说的模式并不是java中针对具体的事件的设计模式 主成功场景的几个操作：1.makemewsale 2.enteritem 3....
• GRASP(General responsibility assignment software Principle)设计原则是设计模式的基础,在GOF的23中设计模式中处处可以体现其中的一个或多个设计原则，所以在掌握设计模式之前需要对GRASP原则有一定的了解，本节我...
• 简述GRASP设计原则   GRASP（General Responsibility Assignment Software Pattern）是通用职责软件分配模式。GRASP的核心是自己干自己能干的事，自己只干自己的 事，也就是职责的分配和实现高内聚。用来解决...
• GRASP(General Responsibility Assignment Software Pattern)通用职责软件分配模式基于面向对象, 用于逻辑表示, 是实现代码架构的分配方案. 遵循原则是为了 让代码更加高效、灵活一共有八大原则--高内聚低耦合 ...

...