橋接模式 基本程式碼

橋接模式 基本程式碼

橋接模式 基本程式碼

橋接模式 基本程式碼

abstract class Implementor
{
public abstract void Operation();
}

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();

}

實現系統可能有多角度分類,每一種分類都有可能變化,

那麼就把這種多角度分離讓它們獨立變化,減少它們之間

的耦合。