设计模式复习-桥接模式

博客围绕桥接模式这一设计模式展开,虽未给出具体内容,但桥接模式作为设计模式的一种,在信息技术领域的程序设计中有着重要作用,可用于解决特定的软件设计问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma once
#include "stdafx.h"
#include<set>
#include<string>
#include<iostream>
using namespace std;

/*
	设计模式-桥接模式(Bridge)
	将抽象部分与它的实现部分分离,使他们都可以独立地变化。
*/

class CImplementor {
public:
	virtual void Operation() = 0;
};

class CConcreteImplementorA :public CImplementor {
public:
	void Operation() {
		cout << "Execution method A" << endl;
	}
};

class CConcreteImplementorB :public CImplementor {
public:
	void Operation() {
		cout << "Execution method B" << endl;
	}
};

class CAbstraction {
protected:
	CImplementor * m_pImplementor;
public:
	CAbstraction() {
		m_pImplementor = NULL;
	}
	void SetImplementor(CImplementor *pImplementor) {
		m_pImplementor = pImplementor;
	}
	virtual void Operation() = 0;
};

class CRefinedAbstraction :public CAbstraction {

public:
	void Operation() {
		if (m_pImplementor != NULL) {
			m_pImplementor->Operation();
		}
	}

};



int main() {

	CAbstraction *pAb = new CRefinedAbstraction();
	CConcreteImplementorA *pcA = new CConcreteImplementorA();
	CConcreteImplementorB *pcB = new CConcreteImplementorB();
	pAb->SetImplementor(pcA);
	pAb->Operation();
	pAb->SetImplementor(pcB);
	pAb->Operation();
	
	getchar();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值