C++设计模式之组合、装饰、适配器、桥接、外观模式(例子为主)

组合模式:单个对象和组合对象的使用具有一致性。将对象组合成树形结构以表示“部分–整体”(举例:文件夹增加删除遍历)
首先创建一个文件虚基类(包括名字、地址、增加、删除),用以派生出文件(只有名字)和文件夹(名字、地址、增加、删除),文件夹在构造的时候不仅需要取名,还要创建一个链表来保存子目录的文件或文件夹的地址。采用迭代器,递归的方法,显示某个文件夹下面的所有文件。

/*************************************************************************
	> File Name:   1_组合模式.cpp
	> Author:      ls
	> Mail:        1096475833@qq.com 
	> Created Time: 2019年07月09日 星期二 10时17分30秒
 ************************************************************************/

#include<iostream>
#include<list>
#include<string>
using namespace std;

class File
{
protected:
	string name;
	list<File*> *child;
public:
	virtual string GetName() = 0;
	virtual list<File*>* GetChild() = 0;
	virtual int addFile(File*) = 0;
	virtual int removeFile(File*) = 0;
};

class iFile:public File
{
public:
	iFile(string str) { name = str; }
	string GetName()
	{
		return name;
	}

	list<File*>* GetChild() { return NULL; }
	int addFile(File*p) { return 1; }
	int removeFile(File*p) { return 1;}
};

class Dir:public File
{
public:
	Dir(string str)
	{
		name = str;
		child = new list<File*>;
	}

	string GetName() { return name; }
	list<File*>* GetChild() { return child; }
	int addFile(File*p) 
	{
		child->push_back(p);
		return 1;
	}

	int removeFile(File*p)
	{
		child->remove(p);
		return 1;
	}
};

void show(File*root,int gap)
{
	for(int i=0;i<gap;++i)
	{
		cout<<"----|";
	}

	if(root == NULL)
	{
		return;
	}
	else
	{
		cout<<root->GetName()<<endl;
	}

	list<File*>*l = root->GetChild();

	if(l!=NULL)
	{
		gap++;

		list<File*>::iterator iter = l->begin();
		for(iter;iter!=l->end();++iter)
		{
			show((*iter),gap);
		}
	}
}

int main()
{
	File*dir = new Dir("home");
	File*dir1 = new Dir("lib");
	File*dir2 = new Dir("etc");
	File*dir3 = new Dir("mnt");

	File*file1 = new iFile("a.c");
	File*file2 = new iFile("b.c");
	File*file3 = new iFile("c.c");
	File*file4 = new iFile("d.c");
	File*file5 = new iFile("e.c");

	dir->addFile(dir1);
	dir->addFile(dir2);
	dir1->addFile(file1);
	dir1->addFile(file2);
	dir->addFile(file3);
	dir2->addFile(file4);
	dir2->addFile(file5);
	dir->addFile(dir3);

	show(dir,0);

}

装饰模式:装饰模式动态的给一个对象添加一些额外的职责。就增加功能来说,此模式比生成子类更为灵活。(举例:手机的功能添加)
刚开始的手机只有打电话的功能,因此创建一个phone虚基类,派生出callPhone;接着我再根据phone虚基类派生出photophone,可以进行拍照,拍照当然也需要打电话的功能,因此在构造photophone类的时候,向里面传一个phone类型的参数指针,将指向callphone的指针传入进去,在工作的时候调用传参的work,这么就相当于把打电话和拍照的功能放在了一起。

/*************************************************************************
	> File Name:   2_装饰模式.cpp
	> Author:      ls
	> Mail:        1096475833@qq.com 
	> Created Time: 2019年07月09日 星期二 11时15分40秒
 ************************************************************************/

#include<iostream>
using namespace std;

class Phone
{
public:
	virtual void work() = 0;
};

class CallPhone:public Phone
{
public:
	void work()
	{
		cout<<"---------------------------------------"<<endl;
		cout<<"this is call phone"<<endl;
	}
};

class PhotoPhone:public Phone
{
private:
	Phone*phone;
public:
	PhotoPhone(Phone *p)
	{
		phone = p;
	}
	void work()
	{
		phone->work();
		cout<<"this is photo phone"<<endl;
	}
};

class SmartPhone:public Phone
{
private:
	Phone*phone;
public:
	SmartPhone(Phone *p)
	{
		phone = p;
	}
	void work()
	{
		phone->work();
		cout<<"this is net phone"<<endl;
	}
};


int main()
{
	Phone*p = new CallPhone;
	p->work();

	Phone*p1 = new PhotoPhone(p);
	p1->work();

	Phone*p2 = new SmartPhone(p1);
	p2->work();

	Phone*p3 = new SmartPhone(p);
	p3->work();

	delete p;
	delete p1;
	delete p2;
	delete p3;
	return 0;
}

适配器模式:是将一个类的接口转换成客户希望的另外一个接口。使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。(举例:电源220V与电池5V相对应)

/*************************************************************************
	> File Name:   3_适配器模式.cpp
	> Author:      ls
	> Mail:        1096475833@qq.com 
	> Created Time: 2019年07月09日 星期二 11时52分23秒
 ************************************************************************/

#include<iostream>
using namespace std;

class Voltage
{
public:
	virtual int GetVoltage() = 0;
};

class Voltage_220:public Voltage
{
public:
	int GetVoltage()
	{
		return 220;
	}
};

class Adapter:public Voltage
{
private:
	Voltage *v;
public:
	Adapter(Voltage*v)
	{
		this->v = v;
	}
	int GetVoltage()
	{
		if(v->GetVoltage() == 220)
		{
			return 5;
		}
		else
		{
			return 0;
		}
	}

};

class Phone
{
private:
	Voltage*v;
public:
	Phone(Voltage*v)
	{
		this->v = v;
	}
	void start()
	{
		if(v->GetVoltage()<5)
		{
			cout<<"low Voltage电压过低"<<endl;
		}
		else if(v->GetVoltage()>5)
		{
			cout<<"high Voltage电压过高"<<endl;
		}
		else
		{
			cout<<"正常启动"<<endl;
		}
	}
};

int main()
{
	Voltage *v = new Voltage_220;
	Voltage *t = new Adapter(v);
	Phone *p = new Phone(t);
	p->start();

	return 0;
}

桥接模式:把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。(举例:不同的手机上运行不同的软件)构造两个类,然后将其连接起来构成一个大产品,各部分功能相互独立互不影响

/*************************************************************************
	> File Name:   4_桥接模式.cpp
	> Author:      ls
	> Mail:        1096475833@qq.com 
	> Created Time: 2019年07月09日 星期二 14时04分04秒
 ************************************************************************/

#include<iostream>
using namespace std;

class Phone
{
public:
	virtual void work() = 0;
};

class IPhone:public Phone
{
public:
	void work()
	{
		cout<<"Iphone is running ";
	}
};

class HuaWei:public Phone
{
public:
	void work()
	{
		cout<<"HuaWei is running ";
	}
};

class SoftWare
{
protected:
	Phone *p;
public:
	virtual void work() = 0;
};

class QQ:public SoftWare
{
public:
	QQ(Phone *p)
	{
		this->p = p;
	}

	void work()
	{
		p->work();
		cout<<" QQ"<<endl;
	}
};

class WeChat:public SoftWare
{
public:
	WeChat(Phone*p)
	{
		this->p = p;
	}

	void work()
	{
		p->work();
		cout<<" WeChat"<<endl;
	}
};


int main()
{
	Phone*p = new IPhone;
	SoftWare *s=new QQ(p);
	s->work();

	delete p;
	p = new HuaWei;
	s->work();
	delete s;
	s = new WeChat(p);
	s->work();

	return 0;
}

外观模式:封装多个对象的生命周期,从创建到销毁。 建造者模式:封装单个对象的创建过程(过程) 工厂模式:封装多个对象的创建过程(结果)为子系统中统一一套接口,让子系统更加容易使用

/*************************************************************************
	> File Name:   5_外观模式.cpp
	> Author:      ls
	> Mail:        1096475833@qq.com 
	> Created Time: 2019年07月09日 星期二 14时36分42秒
 ************************************************************************/

#include<iostream>
using namespace std;

class SystemA
{
public:
	SystemA()
	{
		cout<<"create SystemA"<<endl;
	}
	void work()
	{
		cout<<"SystemA is working"<<endl;
	}
	~SystemA()
	{
		cout<<"delete SystemA"<<endl;
	}
};

class SystemB
{
public:
	SystemB()
	{
		cout<<"create SystemB"<<endl;
	}
	void work()
	{
		cout<<"SystemB is working"<<endl;
	}
	~SystemB()
	{
		cout<<"delete SystemB"<<endl;
	}
};

class SystemC
{
public:
	SystemC()
	{
		cout<<"create SystemC"<<endl;
	}
	void work()
	{
		cout<<"SystemC is working"<<endl;
	}
	~SystemC()
	{
		cout<<"delete SystemC"<<endl;
	}
};


class Fac
{
private:
	SystemA *a;
	SystemB *b;
	SystemC *c;
public:
	Fac()
	{
		a = new SystemA;
		b = new SystemB;
		c = new SystemC;
	}

	void work()
	{
		a->work();
		b->work();
		c->work();
	}

	~Fac()
	{
		delete a;
		delete b;
		delete c;
	}

};

int main()
{
	Fac*f = new Fac;
	f->work();
	delete f;


	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值