装饰模式

单一职责原则:

单一职责原则,就一个类而言,应该仅有一个引起它变化的原因。如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个职责的变化可能会消弱或者抑制这个类完成其他职责的能力。这种耦合会导致脆弱的设计,当变化发生时,设计会遭受到意想不到的破坏。

软件设计真正要做的许多内容,就是发现职责并把那些职责相互分离。如果你能够想到多于一个的动机去改变一个类,那么这个类就具有多于一个的职责,就应该考虑类的职责分离。

 

开放-封闭原则:

是说软件实体(类、模块、函数等等)应该可以扩展,但是不可修改。这个原则其实 是有2个特征,一个是说“对于扩展是开放的(Open for extension)”,另一个是说“对于更改是封闭的(Closed for modification)”。怎样的设计才能面对需求的改变却可以保持相对稳定,从而使得系统可以在第一个版本以后不断推出新的版本呢?

无论模块是多么的“封闭”,都会存在一些无法对之封闭的变化。既然不可能完全封 闭,设计人员必须对于他设计的模块应该对哪种变化封闭做出选择。他必须先猜测出最有可能发生的变化种类,然后构造抽象来隔离那些变化。我们很难预先猜测, 但是我们却可以在发生小变化时,就及早去想办法应对发生更大变化的可能。也就是说,等到变化发生时立即采取行动。

在我们最初编写代码时,假设变化不会发生时,我们就创建抽象来隔离以后发生的同类变化。面对需求,对程序的改动是通过增加新代码进行的,而不是更改现有的代码。这就是“开发-封闭原则”的精神所在。

当然并不是什么时候应对变化都是容易的。我们希望的是在开发工作展开不久就知道可能发生的变化。查明可能发生的变化所等待的时间越长,要创建正确的抽象就越困难。

开放-封闭原则是面向对象设计的核心所在。遵循这个原则可以带来面向对象技术所 声称的巨大好处,也就是可维护性、可扩展性、可复用、灵活性好。开发人员应该仅对程序中呈现出频繁变化的那些部分做出抽象,然而,对于应用程序中的每个部 分都刻意地进行抽象同样不是一个好主意。拒绝不成熟的抽象和抽象本身一样重要。

 

依赖倒转原则

就是抽象不应该依赖细节,细节应该依赖于抽象。针对接口编程,不要对实现编程。比如:主板、CPU、内存、硬盘都是针对接口设计的,如果针对实现来设计,内存就要对应到具体的某个品牌的主板,那就会出现换内存需要把主板也换了的尴尬。

A.高层模块不应该依赖低层模块。两个都应该依赖抽象。

B.抽象不应该依赖细节。细节应该依赖抽象。

依赖倒转其实可以说是面向对象设计的标志,用哪些语言来编写程序不重要,如果编写时考虑的都是如何针对抽象编程而不是针对细节编程,即程序中所有的依赖关系都是终止于抽象类或者接口,那就是面向对象的设计,反之那就是过程化的设计了。

 

里氏代换原则

一个软件实体如果使用的是一个父类的话,那么一定适用于其子类,而且它察觉不出 父类对象和子类对象的区别。也就是说,在软件里面,把父类都替换成它的子类,程序的行为没有变化,简单地说,子类型必须能够替换掉它们的父类型。只有当子 类可以替换掉父类,软件单位的功能不受到影响时,父类才能真正被复用,而子类也能够在父类的基础上增加新的行为。由于子类型的可替换性才使得使用父类类型 的模块在无需修改的情况下就可以扩展。

 

 

装饰模式(Decorator)

动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。装饰模式是为已有功能动态地添加更多功能的一种方式。

装饰模式什么时候用呢?

当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了 原有类的核心职责或主要行为。但这种做法的问题在于,他们在主类中加入了新的字段,新的方法和新的逻辑,从而增加了主类的复杂度。而这些新加入的东西仅仅 是为了满足一些只在某种特定情况下才会执行的特殊行为的需要。而装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类 包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。

装饰模式的优点:把类中的装饰功能从类中搬移去除,这样可以简化原有的类。这样做更大的好处就是有效地把类的核心职责和装饰功能区分开了。而且可以去除相关类中复杂的装饰逻辑。

工程结构:

(1)Component.h 抽象类

(2)ConcreteComponent.h 具体待装饰对象类

(3)Decorator.h 抽象装饰类

(4)ConcreteDecoratorA.h 具体装饰类A

(5)ConcreteDecoratorB.h 具体装饰类B

(6)DecoratorApplication.cpp 客户端运用类

(1)Component.h 抽象类

  1. /************************************************************************/  
  2. /* 抽象基类 */  
  3. /************************************************************************/  
  4. #ifndef _COMPONENT_H_  
  5. #define _COMPONENT_H_  
  6.   
  7. #include <string>  
  8. using namespace std;  
  9.   
  10. class Component  
  11. {  
  12. public:  
  13.     virtual void Operation() = 0;  
  14. };  
  15.   
  16. #endif  _COMPONENT_H_  

(2)ConcreteComponent.h 具体待装饰对象类

  1. /************************************************************************/  
  2. /* 具体对象Ted的操作类 */  
  3. /************************************************************************/  
  4. #ifndef _CONCRETE_COMPONENT_H_  
  5. #define _CONCRETE_COMPONENT_H_  
  6.   
  7. #include "Component.h"  
  8. #include <iostream>  
  9. using namespace std;  
  10.   
  11. class ConcreteComponent : public Component  
  12. {  
  13. public:  
  14.     void Operation()  
  15.     {  
  16.         cout << "具体对象Ted的操作" << endl;  
  17.     };  
  18. };  
  19.   
  20. #endif  _CONCRETE_COMPONENT_H_  
  

(3)Decorator.h 抽象装饰类

  1. /************************************************************************/  
  2. /* 抽象的装饰类,继承了Component */  
  3. /************************************************************************/  
  4. #ifndef _DECORATOR_H_  
  5. #define _DECORATOR_H_  
  6.   
  7. #include "Component.h"  
  8.   
  9. class Decorator : public Component  
  10. {  
  11. public:  
  12.     // 设置Component  
  13.     void SetComponent(Component* pComponent)  
  14.     {  
  15.         m_pComponent = pComponent;  
  16.     };  
  17.   
  18.     // 重写Operation,实际执行的是Component的Operation  
  19.     void Operation()  
  20.     {  
  21.         if (NULL != m_pComponent)  
  22.         {  
  23.             m_pComponent->Operation();  
  24.         }  
  25.     };  
  26.   
  27. protected:  
  28.     Component*   m_pComponent;  
  29. };  
  30.   
  31. #endif  _DECORATOR_H_  
  

(4)ConcreteDecoratorA.h 具体装饰类A

 

  1. /************************************************************************/  
  2. /* 具体装饰类A */  
  3. /************************************************************************/  
  4. #ifndef _CONCRETE_DECORATOR_A_H_  
  5. #define _CONCRETE_DECORATOR_A_H_  
  6.   
  7. #include "Decorator.h"  
  8.   
  9. class ConcreteDecoratorA : public Decorator  
  10. {  
  11. public:  
  12.     void Operation()  
  13.     {          
  14.         // 首先运行原Component的Operation,再执行本类的功能,如m_strAddedState,相当于对原Component进行了装饰  
  15.         Decorator::Operation();  
  16.         m_strAddedState = "第A种装饰手法";  
  17.         cout << "具体第A种装饰操作" << endl;                  
  18.     };  
  19.   
  20. private:  
  21.     // 本类独有功能,以区别于其他的具体装饰类,比如:ConcreteDecoratorB  
  22.     string m_strAddedState;  
  23. };  
  24.   
  25. #endif  _CONCRETE_DECORATOR_A_H_  

(5)ConcreteDecoratorB.h 具体装饰类B

 

  1. /************************************************************************/  
  2. /* 具体装饰类B */  
  3. /************************************************************************/  
  4. #ifndef _CONCRETE_DECORATOR_B_H_  
  5. #define _CONCRETE_DECORATOR_B_H_  
  6.   
  7. #include "Decorator.h"  
  8.   
  9. class ConcreteDecoratorB : public Decorator  
  10. {  
  11. public:  
  12.     void Operation()  
  13.     {          
  14.         // 首先运行原Component的Operation,再执行本类的功能,如AddedBehavior,相当于对原Component进行了装饰  
  15.         Decorator::Operation();          
  16.         AddedBehavior();  
  17.         cout << "具体第B种装饰操作" << endl;                  
  18.     };  
  19.   
  20. private:  
  21.     // 本类中独有的方法,以区别于其他具体的装饰类,如:ConcreteDecoratorA  
  22.     void AddedBehavior()  
  23.     {  
  24.         cout << "第B种装饰手法" << endl;  
  25.     };  
  26. };  
  27.   
  28. #endif  _CONCRETE_DECORATOR_B_H_  

(6)DecoratorApplication.cpp 客户端运用类

  1. // DecoratorApplication.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "ConcreteComponent.h"  
  6. #include "ConcreteDecoratorA.h"  
  7. #include "ConcreteDecoratorB.h"  
  8.   
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     ConcreteComponent* pConcreteCmpnt = NULL;  
  12.     pConcreteCmpnt = new ConcreteComponent();  
  13.   
  14.     ConcreteDecoratorA* pConcreteDecA = NULL;  
  15.     pConcreteDecA = new ConcreteDecoratorA();  
  16.   
  17.     ConcreteDecoratorB* pConcreteDecB = NULL;  
  18.     pConcreteDecB = new ConcreteDecoratorB();  
  19.   
  20.     // 装饰的方法是:首先用ConcreteComponent实例化对象指针pConcreteCmpnt,  
  21.     // 然后用ConcreteDecoratorA的实例化对象指针pConcreteDecA来包装pConcreteCmpnt,  
  22.     // 在用ConcreteDecoratorB的对象指针pConcreteDecB包装pConcreteDecA,最终执行  
  23.     // pConcreteDecB的Operation  
  24.     if (NULL != pConcreteDecA)  
  25.     {  
  26.         pConcreteDecA->SetComponent(pConcreteCmpnt);  
  27.         if (NULL != pConcreteDecB)  
  28.         {  
  29.             pConcreteDecB->SetComponent(pConcreteDecA);  
  30.             pConcreteDecB->Operation();  
  31.         }  
  32.     }  
  33.   
  34.     int iPause = 0;  
  35.     cin >> iPause;  
  36.   
  37.     return 0;  
  38. }  

装饰模式是利用SetComponent来对对象进行包装的。这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。

如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是 ConcreteComponent的一个子类。同样道理,如果只有一个ConcreteDecorator类,那么就没有必要建立一个单独的 Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。

 新版的装饰模式

工程结构:

(1)Person.h 待装饰的人类

(2)Decorator.h 装饰类

(3)Hats.h 具体装饰类

(4)Nikes.h 具体装饰类

(5)Trousers.h 具体装饰类

(6)TShirts.h 具体装饰类

(7)PersonDecorator.cpp 客户端运用类

(1)Person.h 待装饰的人类

  1. /************************************************************************/  
  2. /* 实现类似ConcreteComponent类的功能 */  
  3. /************************************************************************/  
  4. #ifndef _PERSON_H_  
  5. #define _PERSON_H_  
  6.   
  7. #include <string>  
  8. #include <iostream>  
  9. using namespace std;  
  10.   
  11. class CPerson  
  12. {  
  13. public:  
  14.     CPerson(){};  
  15.     CPerson(string strName)  
  16.     {  
  17.         m_strName = strName;  
  18.     }  
  19.   
  20.     virtual void Show()  
  21.     {  
  22.         cout << "装饰的" << m_strName << endl;  
  23.     };  
  24.   
  25. private:  
  26.     string m_strName;  
  27. };  
  28.   
  29. #endif  _PERSON_H_  

(2)Decorator.h 装饰类

  1. /************************************************************************/  
  2. /* 实现Decorator类功能 */  
  3. /************************************************************************/  
  4. #ifndef _DECORATOR_H_  
  5. #define _DECORATOR_H_  
  6.   
  7. #include "Person.h"  
  8.   
  9. class CDecorator : public CPerson  
  10. {  
  11. public:  
  12.     // 装扮  
  13.     void AddDecorator(CPerson* pPerson)  
  14.     {  
  15.         m_pPerson = pPerson;  
  16.     };  
  17.   
  18.     void Show()  
  19.     {  
  20.         if (NULL != m_pPerson)  
  21.         {  
  22.             m_pPerson->Show();  
  23.         }  
  24.     };  
  25.   
  26. protected:  
  27.     CPerson* m_pPerson;  
  28. };  
  29.   
  30. #endif  _DECORATOR_H_  

(3)Hats.h 具体装饰类

  1. #ifndef _HATS_H_  
  2. #define _HATS_H_  
  3.   
  4. #include "Decorator.h"  
  5.   
  6. class CHats : public CDecorator  
  7. {  
  8. public:  
  9.     void Show()  
  10.     {  
  11.         cout << "帽子 ";  
  12.         CDecorator::Show();  
  13.     }  
  14. };  
  15.   
  16. #endif  _HATS_H_  

(4)Nikes.h 具体装饰类

  1. #ifndef _NIKES_H_  
  2. #define _NIKES_H_  
  3.   
  4. #include "Decorator.h"  
  5.   
  6. class CNikes : public CDecorator  
  7. {  
  8. public:  
  9.     void Show()  
  10.     {  
  11.         cout << "Nike鞋 ";  
  12.         CDecorator::Show();  
  13.     }  
  14. };  
  15.   
  16. #endif  _NIKES_H_  

(5)Trousers.h 具体装饰类

  1. #ifndef _TROUSERS_H_  
  2. #define _TROUSERS_H_  
  3.   
  4. #include "Decorator.h"  
  5.   
  6. class CTrousers : public CDecorator  
  7. {  
  8. public:  
  9.     void Show()  
  10.     {  
  11.         cout << "裤子 ";  
  12.         CDecorator::Show();  
  13.     }  
  14. };  
  15.   
  16. #endif  _TROUSERS_H_  

(6)TShirts.h 具体装饰类

  1. /************************************************************************/  
  2. /* 具体的装饰类 */  
  3. /************************************************************************/  
  4. #ifndef _TSHIRTS_H_  
  5. #define _TSHIRTS_H_  
  6.   
  7. #include "Decorator.h"  
  8.   
  9. class CTShirts : public CDecorator  
  10. {  
  11. public:  
  12.     void Show()  
  13.     {  
  14.         cout << "T-恤 ";  
  15.         CDecorator::Show();  
  16.     }  
  17. };  
  18.   
  19. #endif  _TSHIRTS_H_  

(7)PersonDecorator.cpp 客户端运用类

  1. // PersonDecorator.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "Person.h"  
  6. #include "TShirts.h"  
  7. #include "Trousers.h"  
  8. #include "Hats.h"  
  9. #include "Nikes.h"  
  10.   
  11. int _tmain(int argc, _TCHAR* argv[])  
  12. {  
  13.     CPerson* pSky = NULL;  
  14.     pSky = new CPerson("Sky");  
  15.     cout << "第一种装扮:" << endl;  
  16.   
  17.     CHats* pHat = NULL;  
  18.     pHat = new CHats();  
  19.   
  20.     CTShirts* pTShirt = NULL;  
  21.     pTShirt = new CTShirts();  
  22.   
  23.     CTrousers* pTrouser = NULL;  
  24.     pTrouser = new CTrousers();  
  25.   
  26.     CNikes* pNikes = NULL;  
  27.     pNikes = new CNikes();  
  28.   
  29.     // 装饰过程  
  30.     if (NULL != pHat && NULL != pTShirt && NULL != pTrouser && NULL != pNikes)  
  31.     {  
  32.         pHat->AddDecorator(pSky);  
  33.         pTShirt->AddDecorator(pHat);  
  34.         pTrouser->AddDecorator(pTShirt);  
  35.         pNikes->AddDecorator(pTrouser);  
  36.         pNikes->Show();  
  37.     }  
  38.   
  39.     int iPause = 0;  
  40.     cin >> iPause;  
  41.   
  42.     return 0;  
  43. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值