Design Pattern - Structural Patterns - Bridge Pattern

50 篇文章 0 订阅
37 篇文章 0 订阅

2007

Section 2, Chapter 4


Bridge Pattern


Concept

In the Bridge pattern we allow the instance variable to be not a concrete type but an abstract type, thus giving us a variance on which class we wish to adapt, or bridge.


Use

In case of inexact matches. Inexact matches occur when two class types are similar but not enough so for the compiler to recognize.

We have a class that has a single method and inherits a base.

We have an instance where our implementor is another class type that we do not want to inherit.


Design

  • We desire a class's implementation, but we do not wish to inherit it;
  • We inherit the bridge from the expected base type;
  • We override the methods, properties, and events;
  • We encapsulate the methods, properties, and events of the desired implementation class in those overrides;
  • The instance of the internal abstract implementation is interchangeable because of its abstract status.


  • Abstraction: the base for the bridge that holds the abstracted instance variable for the class to be adapted.
  • Refined Abstraction: the concrete implementation of the abstracted bridge, where we define the methods, properties, and events for which we wish to provide a bridge.
  • Abstract Implementor: the abstraction interface for the class for which we wish to bridge into the new type.
  • Concrete Implementor: the concrete interface for the class for which we wish to bridge into the new type.




Illustration

Suppose we have the code in place as below:


class HttpRequest
{
	public override void ProcessRequest(string request)
	{
		.......
	}
}
class ISAPIRequest
{
	public override void ProcessRequest(string request)
	{
		.......
	}
}

class RequestHandler : Request
{
	public override void Process(string request)
	{
		//some implementation code
	}
}

RequestHandler class has a method similar to the implementation classes (HttpRequest & ISAPIRequest) but named differently, the method's internal functionality is similar.


What we wish to do is to make the RequestHandler class use our implementor handler types, without inheritance from these types.





abstract class RequestImplementor
{
	public abstract void ProcessRequest(string request);
}


class HttpRequest : RequestImplementor
{
	public override void ProcessRequest(string request)
	{
		......
	}
}

class ISAPIRequest : RequestImplementor
{
	public override void ProcessRequest(string request)
	{
		......
	}
}


abstract class Request
{
	private RequestImplementor _implementor;	// a private instance variable for the abstract implementor
	
	public void SetImplementor(RequestImplementor implementor)	// a way to set the actual concrete implementation of our abstract implementor to the instance variable
	{
		_implementor = implementor;
	}
	
	protected RequestImplementor Implementor
	{
		get{return _implementor;}
	}
	
	public abstract void Process(string request);	// will be used to allow the inherited instances of Request to have a common method
}


class RequestHandler : Request
{
	public override void Process(string request)
	{
		Implementor.ProcessRequest(request);
	}
}


Request request = new RequestHandler();

request.SetImplementor(new HttpRequest());
request.Process("This is a HTTP request stream..");

request.SetImplementor(new ISAPIRequest());
request.Process("This is a ISAPI request stream..");



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值