设计模式C++实现(二)结构型模式

目录

 

桥接模式

功能:

类图:

代码:

适配器模式

功能:

类图:

代码:

装饰者模式

功能:

类图:

代码:

组合模式

功能:

类图:

代码:

享元模式

功能:

类图:

代码:

外观模式

功能:

类图:

代码:

代理模式

功能:

类图:

代码:


桥接模式

功能:

Bridge 是设计模式中比较复杂和难理解的模式之一,也是 OO 开发与设计中经常会用到 的模式之一。使用组合(委托)的方式将抽象和实现彻底地解耦,这样的好处是抽象和实现 可以分别独立地变化,系统的耦合性也得到了很好的降低。

类图:

代码:

#include<iostream>

using namespace std;

class Soft
{
public:
	virtual void run()=0;
};
class SoftA:public Soft
{
public:
	void run()
	{
		cout<<"run softA"<<endl;
	}
};
class SoftB:public Soft
{
public:
	void run()
	{
		cout<<"run softB"<<endl;
	}
};

class PhoneClass
{
protected:
	Soft* soft;
public:
	void installSofr(Soft* s)
	{
		this->soft = s;
	}
	virtual void runSoft()=0;
};
class PhoneA:public PhoneClass
{
public:
	void runSoft()
	{
		cout<<"phonea"<<"->";
		soft->run();
	}
};
class PhoneB:public PhoneClass
{
public:
	void runSoft()
	{
		cout<<"phoneb"<<"->";
		soft->run();
	}
};


int main()
{
	Soft* softa = new SoftA();
	Soft* softb = new SoftB();
	PhoneClass* phonea = new PhoneA();
	PhoneClass* phoneb = new PhoneB();
	phonea->installSofr(softb);
	phonea->runSoft();
	phoneb->installSofr(softa);
	phoneb->runSoft();


	return 0;
}

适配器模式

功能:

我们在应用程序中已经设计好了接口, 与这个第三方提供的接口不一致,为了使得这些接口不兼容的类(不能在一起工作)可以在 一起工作了,Adapter 模式提供了将一个类(第三方库)的接口转化为客户(购买使用者) 希望的接口。

在上面生活中问题的解决方式也就正好对应了 Adapter 模式的两种类别:类模式和对象 模式。

类图:

代码:

以对象模式为例

#include<iostream>

using namespace std;

class Target
{
public:
	virtual void request()
	{
		cout<<"target request"<<endl;
	}
};

class Adaptee
{
public:
	void specificRuquest()
	{
		cout<<"Adaptee::specificRuquest"<<endl;
	}
} ;

class Adapter:public Target
{
private:
	Adaptee* adap;
public:
	Adapter(Adaptee* ad)
	{
		this->adap = ad;
	}
	void request()
	{
		this->adap->specificRuquest();
	}
};


int main()
{
	Adaptee* ad = new Adaptee();
	Target* t = new Adapter(ad);
	t->request();
	return 0;
}

装饰者模式

功能:

OO 设计和开发过程,可能会经常遇到以下的情况:我们需要为一个已经定义好的类 添加新的职责(操作),通常的情况我们会给定义一个新类继承自定义好的类,这样会带来 一个问题(将在本模式的讨论中给出)。通过继承的方式解决这样的情况还带来了系统的复 杂性,因为继承的深度会变得很深。

而 装饰者提供了一种给类增加职责的方法,不是通过继承实现的,而是通过组合。 有关这些内容在讨论中进一步阐述。

用于在原有的基础上进行动态扩展。

类图:

代码:

#include<iostream>

using namespace std;

class Component //对象接口
{
public:
	virtual void operation() = 0;
};
class ComponentSon1:public Component
{
public:
	void operation()
	{
		cout<<"operation Son1"<<endl;
	}
};

class Decorator:public Component
{
protected:
	Component* com;
public:
	Decorator(Component* c)
	{
		this->com = c;
	}
	void operation()
	{
		if(com)
			com->operation();
	}
};
class Decorator1:public Decorator
{
public:
	Decorator1(Component* c):Decorator(c){}
	void operation()
	{
		com->operation();
		this->addBehavior();
	}
	void addBehavior()
	{
		cout<<"Decorator1 addBehavior"<<endl;
	}
};

int main()
{
	Component* com1 = new ComponentSon1();
	com1->operation();
	Decorator* d1 = new Decorator1(com1);
	d1->operation();

	return 0;
}

组合模式

功能:

将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

类图:

代码:

#include<iostream>
#include<vector>
#include<string>

using namespace std;

class Component
{
protected:
	string name;
public:
	Component(string n)
	{
		this->name = n;
	}
	virtual void add(Component* c)=0;
	virtual void remove(Component* c)=0;
	virtual void display()=0;
};

class Composite:public Component
{
public:
	vector<Component*> vCom;
public:
	Composite(string n):Component(n){}
	void add(Component* c)
	{
		vCom.push_back(c);
	}
	void remove(Component* c)
	{
		vector<Component*>::iterator ite = vCom.begin();
		while(ite != vCom.end())
		{
			if(*ite == c)
			{
				vCom.erase(ite);
				break;
			}
			ite++;
		}
	}
	void display()
	{
		cout<<name<<endl;
		for(auto i:vCom)
		{
			i->display();
		}
		
	}
};

class Leaf:public Component
{
public:
	Leaf(string n):Component(n){};
	void add(Component* c)
	{
		cout<<"leaf can not add"<<endl;
	}
	void remove(Component* c)
	{
		cout<<"leaf can not remove"<<endl;
	}
	void display()
	{
		cout<<name<<endl;
	}
};

int main()
{
	Component* root = new Composite("root");
	Component* leafa = new Leaf("LeafA");
	Component* leafb = new Leaf("LeafB");
	root->add(leafa);
	root->add(leafb);
	Component* coma = new Composite("ComA");
	root->add(coma);
	Component* leafaa = new Leaf("LeafAA");
	Component* leafab = new Leaf("LeafAB");
	coma->add(leafaa);
	coma->add(leafab);
	Component* comac = new Composite("ComAC");
	coma->add(comac);
	Component* aca = new Leaf("LeafACA");
	comac->add(aca);
	root->display();
	cout<<endl;
	root->remove(coma);
	root->display();
	return 0;
}

享元模式

功能:

运用共享技术有效的支持大量细粒度的对象。

将 对象的状态分为“外部状态”和“内部状态”,将可以被共享(不会变化)的状态作为内部 状态存储在对象中,而外部对象(变化的)我们可以在适当的时候将 外部对象最为参数传递给对象。

外部状态也可以以参数的形式传递。

类图:

代码:

#include<iostream>
#include<string>
#include<vector>

using namespace std;

class FlyWeight
{
protected:
	string m_str;

public:
	FlyWeight(){}
	FlyWeight(string str):m_str(str){}
	string getObject()
	{
		return this->m_str;
	}
	virtual void operation()=0;
};

class SharedFlyWeight:public FlyWeight
{
public:
	SharedFlyWeight(string str):FlyWeight(str){}
	void operation()
	{
		cout<<"shared:"<<m_str<<endl;
	}
};

class UnShared:public FlyWeight
{
public:
	string unStr;
public:
	UnShared(string str)
	{
		unStr = str;
	}
	void operation()
	{
		cout<<"unshared:"<<unStr<<endl;
	}
};

class FlyWeightFactory
{
private:
	vector<FlyWeight*> vFly;
public:
	FlyWeight* getFlyWeight(string str)
	{
		vector<FlyWeight*>::iterator ite = vFly.begin();
		while(ite != vFly.end())
		{
			if((*ite)->getObject() == str)
			{
				(*ite)->operation();
				return *ite;
			}
			ite++;
		}
		FlyWeight* f = new SharedFlyWeight(str);
		f->operation();
		vFly.push_back(f);
		return f;
	}
	void addUnshared(FlyWeight* unshared)
	{
		unshared->operation();
	}
};

int main()
{
	FlyWeightFactory* fly = new FlyWeightFactory();
	fly->getFlyWeight("175");
	FlyWeight* un1 = new UnShared("whg");
	fly->addUnshared(un1);
	fly->getFlyWeight("175");
	FlyWeight* un2 = new UnShared("whc");
	fly->addUnshared(un2);

	return 0;
}

外观模式

功能:

为子系统中的一组接口提供一个一致的界面,使子系统更加容易使用。

类图:

代码:

#include<iostream>
using namespace std;

class SubSystem1
{
public:
	
	void operation()
	{
		cout<<"SubSystem1"<<endl;
	}
};
class SubSystem2
{
public:
	void operation()
	{
		cout<<"SubSystem2"<<endl;
	}

};

class Facade
{
private:
	SubSystem1* sub1;
	SubSystem2* sub2;
public:
	Facade()
	{
		sub1 = new SubSystem1();
		sub2 = new SubSystem2();
	}
	void operation()
	{
		sub1->operation();
		sub2->operation();
	}
	~Facade()
	{
		delete sub1;
		delete sub1;
	}
};

int main()
{
	Facade* f = new Facade();
	f->operation();
	delete f;

	return 0;
}

代理模式

功能:

为其他对象ti'gong 一种代理以控制对这个对象的访问。

类图:

代码:

#include<iostream>

using namespace std;

class Subject //代理和实体公共接口,这样才能使代理会干实体的活
{
public:
	virtual void request()=0;
	virtual ~Subject(){}
};

class RealSubject:public Subject //实体
{
public:
	void request()
	{
		cout<<"RealSubject"<<endl;
	}
};
class Proxy:public Subject //代理
{
private:
	Subject* m_sub;
public:
	Proxy(Subject* sub)
	{
		m_sub = sub;
	}
	void request()
	{
		m_sub->request();
	}
	~Proxy()
	{
		if(m_sub)
			delete m_sub;
		m_sub = 0;
	}
};

int main()
{
	Subject* sub = new RealSubject();
	Proxy* pro = new Proxy(sub);
	pro->request();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值