The Single Responsibility Principle

The Single Responsibility Principle

Robert C. Martin (Uncle Bob)

ONE OF THE MOST FOUNDATIONAL PRINCIPLES OF GOOD DESIGN IS:

Gather together those things that change for the same reason, and separate those things that change for different reasons.

This principle is often known as the single responsibility principle, or SRP. In short, it says that a subsystem, module, class, or even a function, should not have more than one reason to change. The classic example is a class that has methods that deal with business rules, reports, and databases:

    public class Employee {
        public Money calculatePay() ...
        public String reportHours() ...
        public void save() ...
}

Some programmers might think that putting these three functions together in the same class is perfectly appropriate. After all, classes are supposed to be collections of functions that operate on common variables. However, the problem is that the three functions change for entirely different reasons. The calculatePay function will change whenever the business rules for calculating pay do. The reportHours function will change whenever someone wants a dif- ferent format for the report. The save function will change whenever the DBAs change the database schema. These three reasons to change combine to make Employee very volatile. It will change for any of those reasons. More importantly, any classes that depend upon Employee will be affected by those changes.

Good system design means that we separate the system into components that can be independently deployed. Independent deployment means that if we change one component, we do not have to redeploy any of the others. However, if Employee is used heavily by many other classes in other components, then every change to Employee is likely to cause the other components to be redeployed,

??152 97 Things Every Programmer Should Know

?

???????????????thus negating a major benefit of component design (or SOA, if you prefer the trendier name). The following simple partitioning resolves the issues:

    public class Employee {
        public Money calculatePay() ...
    }
    public class EmployeeReporter {
        public String reportHours(Employee e) ...
    }
    public class EmployeeRepository {
        public void save(Employee e) ...
}

Each class can be placed in a component of its own. Or rather, all the reporting classes can go into the reporting component. All the database-related classes can go into the repository component. And all the business rules can go into the business rule component.

The astute reader will see that there are still dependencies in the above solution. That Employee is still depended upon by the other classes. So if Employee is modi- fied, the other classes will likely have to be recompiled and redeployed. Thus, Employee cannot be modified and then independently deployed. However, the other classes can be modified and independently deployed. No modification of one of them can force any of the others to be recompiled or redeployed. Even Employee could be independently deployed through a careful use of the depen- dency inversion principle (DIP), but that’s a topic for a different book.*

Careful application of the SRP, separating things that change for different reasons, is one of the keys to creating designs that have an independently deployable component structure.

时间: 2024-10-12 14:40:33

The Single Responsibility Principle的相关文章

C#设计模式系列:单一职责原则(Single Responsibility Principle)

1.单一职责原则的核心思想 一个类应该有且只有一个变化的原因. 2.为什么要引入单一职责原则 单一职责原则将不同的职责分离到单独的类,每一个职责都是一个变化的中心.当需求变化时,这个变化将通过更改职责相关的类来体现.如果一个类拥有多于一个的职责,则这些职责就耦合到在了一起,那么就会有多于一个原因来导致这个类的变化.对于某一职责的更改可能会损害类满足其他耦合职责的能力.这样职责的耦合会导致设计的脆弱,以至于当职责发生更改时产生无法预期的破坏. 3.单一职责原则的优点 1>.可以降低类的复杂度,一个

(1)Single Responsibility Principle【单一职责原则】

单一职责原则 SRP:Single responsibility principle [概述]单一职责原则又称单一功能原则,面向对象五个基本原则(SOLID)之一.它规定一个类应该只有一个发生变化的原因.该原则由罗伯特·C·马丁(Robert C. Martin)于<敏捷软件开发:原则.模式和实践>一书中给出的.马丁表示此原则是基于汤姆·狄马克(Tom DeMarco)和Meilir Page-Jones的著作中的内聚性原则发展出的. 所谓职责是指类变化的原因.如果一个类有多于一个的动机被改变

设计模式 之 单一职责原则 (Single Responsibility Principle)

Motivation 动机 In this context, a responsibility is considered to be one reason to change. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one respon

单一职责原则(SRP:Single responsibility principle)

问题: 一个类,只有一个引起它变化的原因.应该只有一个职责.每一个职责都是变化的一个轴线,如果一个类有一个以上的职责,这些职责就耦合在了一起.这会导致脆弱的设计.当一个职责发生变化时,可能会影响其它的职责.另外,多个职责耦合在一起,会影响复用性.例如:要实现逻辑和界面的分离. 好处: 类的复杂性降低. 类的复用性变高. 可读性.可维护性高.

单一职责原则(Single Responsibility Principle SRP)

对于单一职责原则,其核心思想为: 一个类,最好只做一件事,只有一个引起它的变化. 单一职责原则可以看做是低耦合.高内聚在面向对象原则上的引申,将职责定义为引起变化的原因,以提高内聚性来减少引起变化的原因. 职责过多,可能引起它变化的原因就越多,这将导致职责依赖,相互之间就产生影响,从而大大损伤其内聚性和耦合度. 通常意义下的单一职责,就是指只有一种单一功能,不要为类实现过多的功能点,以保证实体只有一个引起它变化的原因.

六大设计原则——单一职责原则【Single Responsibility Principle】

声明:本文内容是从网络书籍整理而来,并非原创. 用户管理的例子 先看一张用户管理的类图: 再看一眼上面的图,思考:这样合理吗? 这个接口是一个很糟糕的设计! 用户的属性和行为竟然混合在一起!!! 正确的做法是把用户的信息抽取成一个业务对象(Bussiness Object,简称 BO),把行为抽取成另外一个接口中,我们把这个类图重新画一下: 这样划分成了两个接口,IUserBO 负责用户的属性,IUserBiz 负责用户的行为,因为是面向的接口编程,所有当产生了这个 UserInfo 对象之后,

面向对象设计原则:单一职责原则(The Single Responsibility Principle)

热爱生活.享受娱乐.专注技术,欢迎关注微信公众号QGer,我们一起见证成长! 什么是单一职责原则? - 官方解释:一个类应该只有一种改变的原因 - 通俗解释:一个类被修改.拓展的时候,应该只能因为一种职责(功能)的扩展,而不应该有第二种职责导致类的修改,一个也不能有另一种职责存在. 为什么遵循单一职责原则? 降低类的复杂度,一个类负责一种职责,逻辑上也变得直观简单. 使代码变得灵活,提高系统的维护性. 变更的风险降低,有需求就会有变更,单一职责原则可降低变更的影响面. 保持松耦合,提供内聚. 如

里氏替换原则(Liskov Substitution Principle)

开放封闭原则(Open Closed Principle)是构建可维护性和可重用性代码的基础.它强调设计良好的代码可以不通过修改而扩展,新的功能通过添加新的代码来实现,而不需要更改已有的可工作的代码.抽象(Abstraction)和多态(Polymorphism)是实现这一原则的主要机制,而继承(Inheritance)则是实现抽象和多态的主要方法. 那么是什么设计规则在保证对继承的使用呢?优秀的继承层级设计都有哪些特征呢?是什么在诱使我们构建了不符合开放封闭原则的层级结构呢?这些就是本篇文章将

designed principle

Review Of designed Pattern principle OutLine: Explanation in principles of designed pattern and useful designed pattern's samples, As we known, there are six principles at designing program, no matter what kind of object-oriented programing language.