Bridge(桥接)模式

一、桥接模式简介(Brief Introduction)

      桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化。

二、类说明

Abstraction:抽象部分的接口,通常在这个对象中,要维护一个实现部分的对象引用,抽象对象里面的方法,需要调用实现的部分的对象来完成。这个对象中的方法,通常都是和具体的业务相关的方法
RefinedAbstraction:扩展抽象部分的接口,通常在这些对象中,定义跟实际业务相关的方法,这些方法的实现通常会使用Abstraction中定义的方法,也可能需要调用实现部分的对象来完成
Implementor:定义实现部分的接口,这个接口不用和Abstraction中的方法一致,通常是由Implementor接口提供基本的操作。而Abstraction中定义的是基于这些基本操作的业务方法,也就是说Abstraction定义了基于这些基本操作的较高层次的操作
ConcreteImplementor:真正实现Implementor接口的对象

三、
1、Abstraction

/**
**Abstraction将client的请求转发给它的Implementor对象。
**Abstraction通常是对更基本的实现的封装,在其上添加一些业务相关的东西,如makeDefaultRoom
**/
class Abstraction
{
public:
	Abstraction(int type):_type(type)
	{
	}

   Implementor *getImpl(int type)
   {
	   if (_impl == 0)
	   {
		   _impl = ImplFactory::getInstance()->getImplementor(_type);
	   }

	   return _impl;
   }

    void makeDefaultRoom()
   {
	   std::cout << "default room" <<std::endl;
	   Implementor *impl = getImpl(_type);
	   if (impl != 0)
	   {
		      impl->makeDoor();
	          impl->makeWindow();
	   }
	
   }
  
protected:
	 void setRoomSize(int size)
    {
	   std::cout << "set room size to:" << size << std::endl;
    }

private:
	Implementor *_impl;
	int _type;
   
};


2、RefinedAbstraction

/**
**扩充由Abstraction定义的接口。
**/
class RefinedAbstraction:public Abstraction
{
 
public:
	/**
	*一、初始化列表和构造函数体内部来初始化成员变量的区别是:
	*(1)初始化列表被认为是对变量的初始化
	*(2)构造函数体内被认为是对变量的赋值
	*而有些变量是不能赋值的,比如const常量,没有初始化的引用;故这两种类型的变量只能在初始化列表中进行初始化。
	*二、初始化列表中的初始化顺序就是定义成员变量的顺序,而不是在初始化列表中书写的顺序。
	**/
	RefinedAbstraction(int type):Abstraction(type){}

    void makeDefaultRoom()
	{
		makeDefaultRoom();
		setRoomSize(100);
	}
};


3、Implementor

/**
**定义实现类的接口,该接口不一定要与Abstraction的接口完全一致;事实上这两个
接口可以完全不同。一般来讲, Implementor接口仅提供基本操作,而Abstraction则
定义了基于这些基本操作的较高层次的操作。
**/
class Implementor
{
public:
	virtual void makeDoor() = 0;
	virtual void makeWindow() = 0;
};


4、ConcretImplementor

class ConcreImplementorA:public Implementor
{
	virtual void makeDoor()
	{
		std::cout << "make door with tone" << std::endl;
	}
	virtual void makeWindow() 
	{
		std::cout << "make window with tone" << std::endl;
	}
};

 

class ConcreImplementorB:public Implementor
{
	virtual void makeDoor()
	{
		std::cout << "make door with tree" << std::endl;
	}
	virtual void makeWindow()
	{
		std::cout << "make window with tree" << std::endl;
	}
};

 

class ImplFactory
{
public:
    static ImplFactory *getInstance()
	{
		if (_instance == 0)
		{
			_instance = new ImplFactory();
		}

		return _instance;
	}

	Implementor *getImplementor(int type) 
	{
		if (type ==  0)
		{
			return new ConcreImplementorA();
		}
        return new ConcreImplementorB();
	}
		
private:
	static ImplFactory *_instance;
};


 5、Client

int main()
{
	Abstraction *abs = new RefinedAbstraction(1);
	abs->makeDefaultRoom();
 
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值