设计模式-装饰模式(实现二)

装饰模式

实现一个穿衣服的程序:


先看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; 
	}	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值