C++ 设计模式之装饰者模式/适配器模式/桥接模式(含代码)

装饰者模式动态地给一个对象添加一些额外的职责,就增加功能来说,此模式比生成子类更为灵活

装饰模式又叫包装模式,是通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案。装饰模式就是把要添加的附加功能分别放在单独的类中,并让这个类包含他要装饰的对象。当需要执行时,客户端就可以有选择地 按顺序的使用装饰功能包装对象

适用于:动态的给一个对象添加一些额外的职责,就增加功能来说,此模式比生成子类更为灵活.

#include
using namespace std;
#include"string"
class Car
{
public:
	virtual void show() = 0;
};
class RunCar :public Car
{
public:
	virtual void show()
	{
		cout << "keyipao" << endl;
	}
};
class SwimCarDirector :public Car
{
public:
	SwimCarDirector(Car *car)
	{
		m_car = car;
	}
	void swim()
	{
		cout << " keyi you" << endl;
	}
	virtual void show()
	{
		m_car->show();
		swim();
	}
private:
	Car  *m_car;
};
class FlyCarDirector :public Car
{
public:
	FlyCarDirector(Car *car)
	{
		m_car = car;
	}
	void fly()
	{
		cout << " keyi fei" << endl;
	}
	virtual void show()
	{
		m_car->show();
		fly();
	}
private:
	Car  *m_car;
};
void main()
{
	Car *mycar = NULL;
	mycar = new RunCar;
	mycar->show();


	FlyCarDirector *flycar = new FlyCarDirector(mycar);
	flycar->show();

	SwimCarDirector*swimcar = new SwimCarDirector(mycar);
	swimcar->show();
	system("pause");
	return;
}


适配器模式是将一个类的接口转化成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作

#include
using namespace std;
#include"string"
class Current18v
{
public:
	virtual void useCurrent18v() = 0;
};
class Current220v
{
public:
	void useCurrent220v()
	{
		cout << "220" << endl;
	}
};
class Adapter :public Current18v
{
public:
	Adapter(Current220v *current)//关联
	{
		m_current = current;
	}
	virtual void useCurrent18v()
	{
		cout << "shipei220v" << endl;
		m_current->useCurrent220v();
	}
private:
	Current220v  *m_current;
};
void main()
{
	Current220v *current220v = NULL;
	Adapter  *adapter = NULL;
	current220v = new Current220v;
	adapter = new Adapter(current220v);
	adapter->useCurrent18v();
	delete current220v;
	delete adapter;
	system("pause");
	return;
}

桥接模式是将抽象部分与实际部分分离,是他们都可以独立的变化

#include
using namespace std;
#include"string"
class Engine
{
public:
	virtual void InstallEngine() = 0;

};
class E4400cc:public Engine
{
public:
	virtual void InstallEngine()
	{
		cout << "我是4400" << endl;
    }
};

class E4500cc :public Engine
{
public:
	virtual void InstallEngine()
	{
		cout << "我是4500" << endl;
	}
};
class Car
{
public:
	Car(Engine *engine)
	{
		this->m_engine = engine;

	}
	virtual void installEngine() = 0;
private:
	Engine  *m_engine;
};
class WBM5:public Car
{
public:
	WBM5(Engine *engine) :Car(engine)
	{
		;

	}
	virtual void installEngine()
	{
		cout << "  " << endl;
		m_engine->InstallEngine();
	}
private:

};

void main()
{
	Engine *engine = NULL;
	WBM5   *wbm5 = NULL;
	engine = new Engine4400cc;

	wbm5 = new WBM5(engine);
	wbm5->installEngine();
	delete wbm5;
	delete engine;
	system("pause");
	return;
}
展开阅读全文

页面更新:2024-04-26

标签:适配器   模式   子类   抽象   顺序   客户端   职责   灵活   接口   透明   对象   独立   代码   功能   动态   工作   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top