桥接模式
桥接模式:将对象部分与它的实现部分分离,使它们可以独立的变化。
结构图:
class Abstraction
{
protected Implementor implementor;
public void SetImplementor(Implementor implementor)
{
this.implementor = implementor;
}
public virtual void Operation()
{
implementor.Operation();
}
}
{
protected Implementor implementor;
public void SetImplementor(Implementor implementor)
{
this.implementor = implementor;
}
public virtual void Operation()
{
implementor.Operation();
}
}
class RefinedAbstraction : Abstraction
{
public override void Operation()
{
implementor.Operation();
}
}
{
public override void Operation()
{
implementor.Operation();
}
}
abstract class Implementor
{
public abstract void Operation();
}
{
public abstract void Operation();
}
class ConcreteImplementorA : Implementor
{
public override void Operation()
{
//throw new NotImplementedException();
Console.WriteLine("具体实现A的方法执行");
}
}
{
public override void Operation()
{
//throw new NotImplementedException();
Console.WriteLine("具体实现A的方法执行");
}
}
class ConcreteImplementorB : Implementor
{
public override void Operation()
{
//throw new NotImplementedException();
Console.WriteLine("具体实现B的方法执行");
}
}
{
public override void Operation()
{
//throw new NotImplementedException();
Console.WriteLine("具体实现B的方法执行");
}
}
class Program
{
static void Main(string[] args)
{
Abstraction ab = new RefinedAbstraction();
ab.SetImplementor(new ConcreteImplementorA());
ab.Operation();
ab.SetImplementor(new ConcreteImplementorB());
ab.Operation();
Console.ReadKey();
}
}
{
static void Main(string[] args)
{
Abstraction ab = new RefinedAbstraction();
ab.SetImplementor(new ConcreteImplementorA());
ab.Operation();
ab.SetImplementor(new ConcreteImplementorB());
ab.Operation();
Console.ReadKey();
}
}