Bridge模式定义 :将抽象和行为划分开来,各自独立,但能动态的结合。
任何事物对象都有抽象和行为之分,例如人,人是一种抽象,人分男人和女人等;人有行为,行为也有各种具体表现,所以,“人”与“人的行为”两个概念也反映了抽象和行为之分。
在面向对象设计的基本概念中,对象这个概念实际是由属性和行为两个部分组成的,属性我们可以认为是一种静止的,是一种抽象,一般情况下,行为是包含在一个对象中,但是,在有的情况下,我们需要将这些行为也进行归类,形成一个总的行为接口,这就是桥模式的用处
namespace DesignPatternConsoleApp
{
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();
}
}
}