【设计模式六大原则1】单一职责原则(Single Responsibility Principle)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【设计模式六大原则1】单一职责原则(Single Responsibility Principle),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含31422字,纯文字阅读大概需要45分钟。
内容图文
http://blog.csdn.net/zhengzhb/article/category/926691/1
图片素材来源,java学习手册
ps.内容为自己整理
定义: 不要存在多于一个导致类变更的原因。通俗的说,即一个类只负责一项职责。
问题由来: 类 T 负责两个不同的职责:职责 P1 ,职责 P2 。当由于职责 P1 需求发生改变而需要修改类 T 时,有可能会导致原本运行正常的职责 P2 功能发生故障。
解决方案: 遵循单一职责原则。分别建立两个类 T1 、 T2 ,使 T1 完成职责 P1 功能, T2 完成职责 P2 功能。这样,当修改类 T1 时,不会使职责 P2 发生故障风险;同理,当修改 T2 时,也不会使职责 P1 发生故障风险。
说到单一职责原则,很多人都会不屑一顾。因为它太简单了。稍有经验的程序员即使从来没有读过设计模式、从来没有听说过单一职责原则,在设计软件时也会自觉的遵守这一重要原则,因为这是常识。在软件编程中,谁也不希望因为修改了一个功能导致其他的功能发生故障。而避免出现这一问题的方法便是遵循单一职责原则。 虽然单一职责原则如此简单,并且被认为是常识,但是即便是经验丰富的程序员写出的程序,也会有违背这一原则的代码存在。为什么会出现这种现象呢?因为有职责扩散。所谓职责扩散,就是因为某种原因,职责 P 被分化为粒度更细的职责 P1 和 P2 。
比如:类 T 只负责一个职责 P ,这样设计是符合单一职责原则的。后来由于某种原因,也许是需求变更了,也许是程序的设计者境界提高了,需要将职责 P 细分为粒度更细的职责 P1 , P2 ,这时如果要使程序遵循单一职责原则,需要将类 T 也分解为两个类 T1 和 T2 ,分别负责 P1 、 P2 两个职责。但是在程序已经写好的情况下,这样做简直太费时间了。所以,简单的修改类 T ,用它来负责两个职责是一个比较不错的选择,虽然这样做有悖于单一职责原则。
举例说明,用一个类描述动物呼吸这个场景:
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> breathe(String animal){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> System.<SPAN style=‘color: rgb(0, 0, 192); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">out<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">.println(animal+<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"呼吸空气<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Client{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">static<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> main(String[] args){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal animal = <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">new<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal();
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"牛<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"羊<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"猪<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
运行结果:
牛呼吸空气
羊呼吸空气
猪呼吸空气
程序上线后,发现问题了,并不是所有的动物都呼吸空气的,比如鱼就是呼吸水的。修改时如果遵循单一职责原则,需要将 Animal 类细分为陆生动物类 Terrestrial ,水生动物 Aquatic ,代码如下:
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Terrestrial{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> breathe(String animal){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> System.<SPAN style=‘color: rgb(0, 0, 192); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">out<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">.println(animal+<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"呼吸空气<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Aquatic{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> breathe(String animal){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> System.<SPAN style=‘color: rgb(0, 0, 192); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">out<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">.println(animal+<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"呼吸水<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Client{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">static<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> main(String[] args){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Terrestrial terrestrial = <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">new<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Terrestrial();
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> terrestrial.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"牛<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> terrestrial.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"羊<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> terrestrial.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"猪<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Aquatic aquatic = <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">new<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Aquatic();
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> aquatic.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"鱼<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
运行结果:
牛呼吸空气
羊呼吸空气
猪呼吸空气
鱼呼吸水
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">
我们会发现如果这样修改花销是很大的,除了将原来的类分解之外,还需要修改客户端。而直接修改类 <SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">Animal来达成目的虽然违背了单一职责原则,但花销却小的多,代码如下:
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> breathe(String animal){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">if<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"鱼<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">.equals(animal)){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> System.<SPAN style=‘color: rgb(0, 0, 192); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">out<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">.println(animal+<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"呼吸水<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">else<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> System.<SPAN style=‘color: rgb(0, 0, 192); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">out<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">.println(animal+<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"呼吸空气<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Client{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">static<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> main(String[] args){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal animal = <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">new<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal();
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"牛<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"羊<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"猪<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"鱼<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
可以看到,这种修改方式要简单的多。但是却存在着隐患:有一天需要将鱼分为呼吸淡水的鱼和呼吸海水的鱼,则又需要修改 <SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">Animal类的<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">breathe方法,而对原有代码的修改会对调用“猪”“牛”“羊”等相关功能带来风险,也许某一天你会发现程序运行的结果变为“牛呼吸水”了。这种修改方式直接在代码级别上违背了单一职责原则,虽然修改起来最简单,但隐患却是最大的。还有一种修改方式:
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> breathe(String animal){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> System.<SPAN style=‘color: rgb(0, 0, 192); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">out<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">.println(animal+<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"呼吸空气<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> breathe2(String animal){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> System.<SPAN style=‘color: rgb(0, 0, 192); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">out<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">.println(animal+<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"呼吸水<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">
<SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">class<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Client{
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">public<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">static<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">void<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> main(String[] args){
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal animal = <SPAN style=‘color: rgb(127, 0, 85); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">new<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> Animal();
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"牛<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"羊<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"猪<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> animal.breathe2(<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"鱼<SPAN style=‘color: rgb(42, 0, 255); font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">"<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">);
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> }
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US">}
可以看到,这种修改方式没有改动原来的方法,而是在类中新加了一个方法,这样虽然也违背了单一职责原则,但在方法级别上却是符合单一职责原则的,因为它并没有动原来方法的代码。这三种方式各有优缺点,那么在实际编程中,采用哪一中呢?其实这真的比较难说,需要根据实际情况来确定。我的原则是:只有逻辑足够简单,才可以在代码级别上违反单一职责原则;只有类中方法数量足够少,才可以在方法级别上违反单一职责原则;
<SPAN style=‘font-family: "Courier New"; font-size: 10pt; background-color: inherit;‘ lang="EN-US"> 例如本文所举的这个例子,它太简单了,它只有一个方法,所以,无论是在代码级别上违反单一职责原则,还是在方法级别上违反,都不会造成太大的影响。实际应用中的类都要复杂的多,一旦发生职责扩散而需要修改类时,除非这个类本身非常简单,否则还是遵循单一职责原则的好。
遵循单一职责原的优点有:
l<SPAN style=‘font-family: "Times New Roman"; font-size: 7pt; background-color: inherit;‘> 可以降低类的复杂度,一个类只负责一项职责,其逻辑肯定要比负责多项职责简单的多;
l<SPAN style=‘font-family: "Times New Roman"; font-size: 7pt; background-color: inherit;‘> 提高类的可读性,提高系统的可维护性;
l<SPAN style=‘font-family: "Times New Roman"; font-size: 7pt; background-color: inherit;‘> 变更引起的风险降低,变更是必然的,如果单一职责原则遵守的好,当修改一个功能时,可以显著降低对其他功能的影响。
需要说明的一点是单一职责原则不只是面向对象编程思想所特有的,只要是模块化的程序设计,都需要遵循这一重要原则。
原文:http://www.cnblogs.com/codemylife/p/3653401.html
内容总结
以上是互联网集市为您收集整理的【设计模式六大原则1】单一职责原则(Single Responsibility Principle)全部内容,希望文章能够帮你解决【设计模式六大原则1】单一职责原则(Single Responsibility Principle)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。