zoukankan      html  css  js  c++  java
  • 设计模式:bridge模式

    目的:将“类的功能层次结构”和“类的实现层次结构”分类

    类的功能层次:通过类的继承添加功能(添加普通函数)

    类的实现层次:通过类的继承实现虚函数

    理解:和适配器模式中的桥接方法相同

    例子:

    class DisplayImpl
    {
    public:
    	virtual void open() = 0;
    	virtual void print() = 0;
    	virtual void close() = 0;
    };
    
     class StringDisplayImpl: public DisplayImpl //实现性扩展
     {
     public:
    	void open()
    	{
    		cout << "open()" << endl;
    	}
    	
    	void print()
    	{
    		cout << "print()" << endl;
    	}
    	
    	void close()
    	{
    		cout << "close()" << endl;
    	}
     };
    
    class Display
    {
    	DisplayImpl* pImpl;   //桥接的核心代码
    public:
    	Display(DisplayImpl* pImpl)
    	{
    		this->pImpl = pImpl;
    	}
    	
    	void print()
    	{
    		pImpl->open();
    		pImpl->print();
    		pImpl->close();
    	}
    };
    
    class MultiDisplay: public Display //功能性扩展
    {
    public:
    	MultiDisplay(DisplayImpl* pImpl): Display(pImpl)
    	{}
    	
    	void multiPrint()
    	{
    		for(int i=0; i<5; i++)
    		{
    			print();
    		}
    	}
    };
    
    int main() 
    {
    	Display* d = new Display(new StringDisplayImpl());
    	d->print();
    	
    	cout << endl;
    	
    	MultiDisplay* md = new MultiDisplay(new StringDisplayImpl());
    	md->multiPrint();
    	
    	return 0;
    }
    
  • 相关阅读:
    一个奇怪的网页bug 竟然是局域网DNS搞的鬼
    繁体系统下如何快速将简体安装文件乱码恢复正常?
    Ubuntu16.04LTS国内快速源
    bitnami redmine版本由2.3.1升级至3.2.2过程
    Ubuntu1404安装gogs过程
    AJAX
    jQuery 事件解释
    安装phpMyadmi报错
    总结二
    总结
  • 原文地址:https://www.cnblogs.com/chusiyong/p/11433290.html
Copyright © 2011-2022 走看看