[review]Design Pattern:Bridge

Bridge

The book told me that this pattern is dedicated to decouple the abstraction from its implemetation so they can vary by themself without affecting each others.

To be honest, I don't know which one I should call as abstraction and which one as implemetation. I call them two structures designed for different functions. They focus on different situations, but they should work together to get the things done.

There are so many ways for us available to composite two objects and to let them work together. A very frequent way is to use the inheritance mechanism.

But the inheritance  has drawbacks with less flexible in some situations. The inherited class depends on its father class so much. It is hard to modify, to extend.

We can use another way to improve this situation with the new machenism called as Composition and  Aggregation. Which makes a "Has" structure compared with the inheritance mechanism with the "is-a" structure.

With the new one, the different parts can work independently but also can work together.

Software design is so flexible that there is no a hard way to let us know to choose which one. it is full of trade-offs based on our experiences and the special situations.

Sometimes the inheritance is good but sometimes the composition or the aggregation is better.

The Bridge Pattern is the very structure which uses the Composition structure to design the system. If you draw the UML of this pattern, you can find dramatically that the structure is like a very bridge, which bidges the two different parts(the two ones the book called as Abstraction and Implementation) and helps them communicate with each other and cooperates with each other.

A small example here to show this pattern:

View Code
namespace Bridge
{
public abstract class Implementor
{
public abstract void Operation();
}

public class ConcreteImplementorA : Implementor
{
public override void Operation()
{
Console.WriteLine("Operation A");
}
}

public class ConcreteImplementorB : Implementor
{
public override void Operation()
{
Console.WriteLine("Operation B");
}
}

public class Abstraction
{
protected Implementor implementor;

public void SetImplementor(Implementor implementor)
{
this.implementor = implementor;
}

public virtual void Operation()
{
implementor.Operation();
}
}

public class RefinedAbstraction : Abstraction
{
public override void Operation()
{
implementor.Operation();
}

}
class Client
{
static void Main(string[] args)
{
Abstraction ab = new RefinedAbstraction();

ab.SetImplementor(new ConcreteImplementorA());
ab.Operation();

ab.SetImplementor(new ConcreteImplementorB());
ab.Operation();
}
}
}



转载于:https://www.cnblogs.com/sanjia/archive/2011/12/08/2281338.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值