1.裝飾模式(Decorator):動态地給一個對象添加一些額外的職責,就增加功能來說,裝飾模式比生成子類更加靈活。
2.Component是定義一個對象接口,可以給這些對象動态地添加職責。ConcreteComponent是定義了一個具體的對象,也可以給這個對象添加一些職責。Decorator:裝飾抽象類,繼承了Component從外類來擴充Component類的功能,但對于Component來說,是無需知道Decorator的存在的.至于ConcreteDecorator就是具體的裝飾對象,起到給Component添加職責的功能。
3.裝飾模式是利用SetComponent來對對象進行包裝的。這樣每個裝飾對象的實作就和如何使用這個對象分離開了,每個裝飾對象隻關心自己的功能,不需要關心如何被添加到對象鍊當中。
4.裝飾模式是為己有功能動态地添加更多功能的一種方式。當系統需要新功能的時候,是向舊的類中添加新的代碼。這些新加的代碼通常裝飾了原有類的核心職責或主要行為。
5.裝飾模式可以讓你全副武裝到牙齒,也可以讓你隻挂一絲到内褲。
6.裝飾模式的優點是:把類中的裝飾功能從類中搬移去除,這樣可以簡化原有的類。最大的好處是有效地把類的核心職責和裝飾功能區分開了。而且可以去除相關類中重複的裝飾邏輯。
7.保證裝飾類之間彼此獨立,這樣他們就可以以任意的順序進行組合了。
小菜的第一款裝扮
代碼:
#pragma once
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Person
{
public:
Person(string name)
{
m_Name=name;
};
void WearTShirts(void)
{
cout << "大T恤" << endl;
};
void WearBigTrouser(void)
{
cout << "垮褲" << endl;
};
void WearSneakers(void)
{
cout << "破球鞋" << endl;
};
void WearSuit(void)
{
cout << "西裝" << endl;
};
void WearTie(void)
{
cout << "領帶" << endl;
};
void WearLeatherShoes(void)
{
cout << "皮鞋" << endl;
};
void Show(void)
{
cout << m_Name << "裝扮的"<< endl;
};
private:
string m_Name;
};
void main()
{
Person* person=new Person("小菜");
person->Show();
person->WearBigTrouser();
person->WearTShirts();
person->WearSneakers();
cout<<endl;
person->Show();
person->WearSuit();
person->WearLeatherShoes();
person->WearTie();
}
運作結果:
使用繼承和抽象
代碼實作:
#pragma once
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Person
{
public:
Person(string name)
{
m_Name=name;
};
void Show(void)
{
cout << m_Name << "裝扮的"<< endl;
};
private:
string m_Name;
};
//服飾抽象類
class Finery
{
public:
virtual void Show(void) = 0;
};
class TShirts :public Finery
{
public:
void Show(void)
{
cout << "大T恤" << endl;
};
};
class BigTrouser :public Finery
{
public:
void Show(void)
{
cout << "垮褲" << endl;
};
};
class Sneakers :public Finery
{
public:
void Show(void)
{
cout << "破球鞋" << endl;
};
};
class Suit :public Finery
{
public:
void Show(void)
{
cout << "西裝" << endl;
};
};
class Tie :public Finery
{
public:
void Show(void)
{
cout << "領帶" << endl;
};
};
class LeatherShoe :public Finery
{
public:
void Show(void)
{
cout << "皮鞋" << endl;
};
};
void main()
{
Person* person=new Person("小菜");
Finery* dtx=new TShirts();
Finery* kk = new BigTrouser();
Finery* pqx = new Sneakers();
person->Show();
dtx->Show();
kk->Show();
pqx->Show();
cout<<endl;
}
實驗結果:
裝飾設計模式的UML
小菜的裝飾模式裝扮
代碼實作:
#pragma once
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Person
{
public:
Person(string name)
{
m_Name=name;
};
void Show(void)
{
cout << "裝扮的"<< m_Name << endl;
};
Person(){};
private:
string m_Name;
};
//服飾類
class Finery : public Person
{
public:
void Decorator(Person* comp)
{
this->m_pComponent=comp;
};
void Show(void)
{
if (m_pComponent!=NULL)
{
m_pComponent->Show();
}
}
protected:
Person* m_pComponent;
};
class TShirts :public Finery
{
public:
void Show(void)
{
cout << "大T恤" << endl;
Finery::Show();
};
};
class BigTrouser :public Finery
{
public:
void Show(void)
{
cout << "垮褲" << endl;
Finery::Show();
};
};
class Sneakers :public Finery
{
public:
void Show(void)
{
cout << "破球鞋" << endl;
Finery::Show();
};
};
class Suit :public Finery
{
public:
void Show(void)
{
cout << "西裝" << endl;
Finery::Show();
};
};
class Tie :public Finery
{
public:
void Show(void)
{
cout << "領帶" << endl;
Finery::Show();
};
};
class LeatherShoe :public Finery
{
public:
void Show(void)
{
cout << "皮鞋" << endl;
Finery::Show();
};
};
void main()
{
Person* person=new Person("小菜");
Sneakers* pqx = new Sneakers();
BigTrouser* kk = new BigTrouser();
TShirts* dtx = new TShirts();
pqx->Decorator(person);
kk->Decorator(pqx);
dtx->Decorator(kk);
dtx->Show();
}