型態:結構模式(Structural Pattern)。     
定義:將抽象部分與它的實現部分分離,使它們都可以獨立地變化。     
使用時機:結實現系統可能有多角度分類,每一種分類都有可能變化,那麼就把這種多角度分離出來讓它們獨立變化,減少它們之間的耦合。
結構圖:來源      
Implementor 類別
abstract class Implementor
    {
        public abstract void Operation();
    }ConcreteImplementorA 類別
class ConcreteImplementorA : Implementor
    {
        public override void Operation()
        {
            Console.WriteLine("具體實現A的方法執行");
        }
    }ConcreteImplementorB 類別
 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();
        }
    }RefinedAbstraction 類別
class RefinedAbstraction : Abstraction
    {
        public override void Operation()
        {
            implementor.Operation();
        }
    }前端調用
 class Program
    {
        static void Main(string[] args)
        {
            Abstraction ab = new RefinedAbstraction();
            ab.SetImplementor(new ConcreteImplementorA());
            ab.Operation();
            ab.SetImplementor(new ConcreteImplementorB());
            ab.Operation();
            Console.Read();
        }
    }
0 Comments:
張貼留言