zoukankan      html  css  js  c++  java
  • 设计模式复习-桥接模式

    #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;
    }

  • 相关阅读:
    ES6 Promise 对象及解决回调地狱问题
    ES6 Iterator迭代器和for...of循环
    ES6 Reflect反射
    ES6 Proxy代理
    ES6 Map对象与Set对象
    端口隔离的应用场景与配置
    交换机级联,堆叠,集群技术介绍
    OSPF虚连接简单配置
    小结ospf基本配置的三个参数
    静态路由配置的3个参数
  • 原文地址:https://www.cnblogs.com/csnd/p/12061909.html
Copyright © 2011-2022 走看看