- 桥接模式 将抽象部分与它的实现部分分离,使它们都可以独立地变化。
abstract class Implementor {
public abstract void Operation();
}
- ConcreteImplementorA 和 ConcreteImplementorB等派生类
class ConcreteImplementorA : Implementor {
public override void Operation() {
Console.WriteLine("具体实现A的方法执行");
}
}
class ConcreteImplementorB : Implementor {
public override void Operation() {
Console.WriteLine("具体实现B的方法执行");
}
}
class Abstraction {
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();
}
}
static void Main (String[] args) {
Abstraction ab = new RefinedAbstraction();
ab.SetImplementor(new ConcreteImplementorA());
ab.Operation();
ab.SetImplementor(new ConcreteImplementorB());
ab.Operation();
Console.Read();
}