装饰模式
实现一个穿衣服的程序:
先看uml图:
因为不是会员,,图没有做完。。Trousers,Shoes,Ornament这几个类和Jacket差不多。在(实现一——————请点击)的基础上改进了一些,将Jacket,Trousers,Shoes,Ornamen这些类抽象化了,最后改成和策略工厂模式相结合,减少客户端那边的代码。
这边是具体代码,可以copy到自己电脑上跑一下看一看,有改进的地方一定要指出来。谢谢。QAQ
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
class Person{
private :
string name;
public:
Person(){};
Person(string name) : name(name){};
virtual void show(){
cout<<"装扮的"<<name<<" ";
}
};
class Clothes : public Person{
protected:
Person* component;
public:
void Decorate(Person* component){
this->component = component;
}
virtual void show(){
if(component != NULL ){
component->show();
}
}
};
//衣服分类 抽象化
class Jacket : public Clothes{
public:
virtual void show() =0;
};
class Trousers : public Clothes{
public:
virtual void show() = 0;
};
class Shoes : public Clothes{
public:
virtual void show() = 0;
};
class Ornament : public Clothes{
public:
virtual void show() = 0;
};
class Tshirt : public Jacket{
public:
virtual void show(){
cout<<"T-shirt ";
Clothes::show();
}
};
class Coat : public Jacket{
public:
virtual void show(){
cout<<"Coat ";
Clothes::show();
}
};
class Suit : public Jacket{
public:
virtual void show(){
cout<<"Suit ";
Clothes::show();
}
};
class DownCoat : public Jacket{
public:
virtual void show(){
cout<<"Down-Coat ";
Clothes::show();
}
};
class Bra : public Jacket{
public:
virtual void show(){
cout<<"Bra ";
Clothes::show();
}
};
class Jeans : public Trousers{
public:
virtual void show(){
cout<<"Jeans ";
Clothes::show();
}
};
class SportPants : public Trousers{
public:
virtual void show(){
cout<<"Sport-Pants ";
Clothes::show();
}
};
class SuitTrousers : public Trousers{
public:
virtual void show(){
cout<<"SuitTrousers ";
Clothes::show();
}
};
class Underpants : public Trousers{
public:
virtual void show(){
cout<<"Underpants ";
Clothes::show();
}
};
class Slipper : public Shoes{
public:
virtual void show(){
cout<<"Slipper ";
Clothes::show();
}
};
class Sneakers : public Shoes{
public:
virtual void show(){
cout<<"Sneakers ";
Clothes::show();
}
};
class LeatherShoes : public Shoes{
public:
virtual void show(){
cout<<"LeatherShoes ";
Clothes::show();
}
};
class Scarf : public Ornament{
public:
virtual void show(){
cout<<"Scarf ";
Clothes::show();
}
};
class Glove : public Ornament{
public:
virtual void show(){
cout<<"Gloves ";
Clothes::show();
}
};
class Cap : public Ornament{
public:
virtual void show(){
cout<<"Cap ";
Clothes::show();
}
};
//穿衣服工厂
class ClothesFactory{
public:
Shoes* shoes;
Trousers* trousers;
Jacket* jacket;
Ornament* ornament;
void GetClothes(string str1,string str2){
if(str1 == "Shoes"){
if(str2 == "Slipper"){
this->shoes = new Slipper;
}
else if(str2 == "Sneakers"){
this->shoes = new Sneakers;
}
else if(str2 == "LeatherShoes"){
this->shoes = new LeatherShoes;
}
}
else if(str1 == "Trousers"){
if(str2 == "SuitTrousers"){
this->trousers = new SuitTrousers;
}
else if(str2 == "SportPants"){
this->trousers = new SportPants;
}
else if(str2 == "Jeans"){
this->trousers = new Jeans;
}
else if(str2 == "Underpants"){
this->trousers = new Underpants;
}
}
else if(str1 == "Jacket"){
if(str2 == "Tshirt"){
this->jacket = new Tshirt;
}
else if(str2 == "Suit"){
this->jacket = new Suit;
}
else if(str2 == "Coat"){
this->jacket = new Coat;
}
else if(str2 == "DownCoat"){
this->jacket = new DownCoat;
}
else if (str2 == "Bra"){
this->jacket = new Bra;
}
}
else if(str1 == "Ornament"){
if(str2 == "Cap"){
this->ornament = new Cap;
}
else if(str2 == "Glove"){
this->ornament = new Glove;
}
else if(str2 == "Scarf"){
this->ornament = new Scarf;
}
}
}
};
int main(){
srand((unsigned)time(NULL));
string str1[5] = {"Shoes","Trousers","Jacket","Ornament"};
string shoes[5] = {"Slipper","Sneakers","LeatherShoes"};
string trousers[5] = {"SuitTrousers","SportPants","Underpants","Jeans"};
string jacket[6] = {"Coat","DownCoat","Tshirt","Suit","Bra"};
string ornament[5] = {"Cap","Glove","Scarf"};
for (int i=1 ;i<=10;i++){
Person* ts = new Person("ts");
cout<<"第"<<i<<"次穿衣服:";
ClothesFactory* CF = new ClothesFactory;
CF->GetClothes(str1[0],shoes[rand()%3]);
CF->GetClothes(str1[1],trousers[rand()%4]);
CF->GetClothes(str1[2],jacket[rand()%5]);
CF->GetClothes(str1[3],ornament[rand()%3]);
CF->ornament->Decorate(ts);
CF->shoes->Decorate(CF->ornament);
CF->trousers->Decorate(CF->shoes);
CF->jacket->Decorate(CF->trousers);
CF->jacket->show();
delete CF;
delete ts;
cout<<"第"<<i<<"次穿衣服结束"<<endl;
}
}