大话设计模式桥接模式c++实现

桥接模式

其他二十三种设计模式

#include<iostream>

using namespace std;

//桥接模式:将抽象部分与实现部分分离,使它们可以独立变化
//实现部分类
class Implementor {
    
    
public:
	virtual void Operation() = 0;
};

//具体实现类
class ConcreteImplementorA :public Implementor {
    
    
public:
	virtual void Operation() {
    
    
		cout << "具体实现A方法" << endl;
	}
};

class ConcreteImplementorB :public Implementor {
    
    
public:
	virtual void Operation() {
    
    
		cout << "具体实现B方法" << endl;
	}
};

//抽象部分类
class Abstraction {
    
    
public:
	void SetImplementor(Implementor* _implementor) {
    
    
		this->implementor = _implementor;
	}
	virtual void Operation() = 0;

protected:
	Implementor* implementor;
};

//具体抽象类
class RefinefAbstraction :public Abstraction {
    
    
public:
	virtual void Operation() {
    
    
		implementor->Operation();
	}
};

void test1() {
    
    
	Abstraction* ab = new RefinefAbstraction;
	
	ab->SetImplementor(new ConcreteImplementorA());
	ab->Operation();

	ab->SetImplementor(new ConcreteImplementorB());
	ab->Operation();

	delete ab;
}

int main() {
    
    

	test1();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wode_0828/article/details/114162081
今日推荐