設計模式實戰之–原廠模式結合適配器模式

來源:互聯網
上載者:User

     設計模式園子裡面很多的文章都寫過,關於什麼是設計模式以及設計模式用來做什麼我就不再贅述了。這裡就結合在工作中遇到的問題加以簡化來說明為什麼會需要用到適配器和工廠這兩個模式,以及這兩個模式組合使用的情況。

     首先來直觀的看一下簡化過的類圖,我會在後面做解釋:

   

        其中的單箭頭表示引用,雙箭頭表示繼承或者說實現。

        在當時的情況下,我們已經有了一個老介面(IExtraAdapter)以及實現這個介面的兩個類(SimpleExAdapter)和(ComplexExAdapter).現在的需求是定義了一個新的介面(IAdapter),這個介面跟老介面很相似(我在後面的代碼裡面簡化了這個介面),但是不是統一介面。繼承自這個介面的新類(UseAdapter)需要實現新的介面,這個新介面裡面可以用到SimpleExAdapter或者ComplexExAdapter中已經實現的IExtraAdapter介面的方法。為瞭解決上述問題,我實作類別圖中的解決方案。下面是代碼,看了代碼應該很快會明白這兩種模式的思想。   

/////////////////////////////////////////////////////////    

 //老的介面
    public interface IExtraAdapter
    {
        void openResource();
        void closeResource();
        void freeResource();
    }

 ///////////////////////////////////////////////////////

//老介面的一種實現
    public class SimpleExAdapter:IExtraAdapter
    {
        public void openResource()
        {
            Console.WriteLine("simple adapter has opened the resource");
        }
        public void closeResource()
        {
            Console.WriteLine("simple adapter has closed the resource");
        }
        public void freeResource()
        {
            Console.WriteLine("simple adapter has freed the resource");
        }
    }

////////////////////////////////////////////////////////////////

//老介面的另外一種實現
    public class ComplexExAdapter:IExtraAdapter
    {
        public void openResource()
        {
            Console.WriteLine("complex adapter has opened the resource");
        }
        public void closeResource()
        {
            Console.WriteLine("complex adapter has closed the resource");
        }
        public void freeResource()
        {
            Console.WriteLine("complex adapter has freed the resource");
        }
    }

////////////////////////////////////////////////////////////////////////////

//老介面的實現工廠管理類
    class Manager
    {
        //根據傳入參數返回對應的工廠執行個體,用這種方法,我們就可以避免
        //在每一個實現介面的方法裡面都寫很多的swith case 語句了。
        public IExtraAdapter GetAdapter(string adapterType)
        {
            if (adapterType == "simple")
            {
                return new SimpleExAdapter();
            }
            else
            {
                return new ComplexExAdapter();
            }
        }
    }

//////////////////////////////////////////////////////////////////////////////////

 //新需求中的介面
    public interface IAdapter
    {
        void NewOpenResource();
        void NewColseResource();
        void NewFreeResource();
    }

//////////////////////////////////////////////////////////

 

 //繼承新介面的實作類別
    class UseAdapter:IAdapter
    {
        private IExtraAdapter exAdapter;//老介面
        public UseAdapter(string adapterType)
        {
            //工廠返回老介面的一個實現執行個體
            Manager manager=new Manager();
            exAdapter = manager.GetAdapter(adapterType);
        }
        //下面三個實現新介面的方法,調用三個自訂方法
        public void NewOpenResource()
        {
            this.openResource();
        }
        public void NewColseResource()
        {
            this.closeResource();
        }
        public void NewFreeResource()
        {
            this.freeResource();
        }
      
        //這三個自訂方法,在做一些新需求的額外操作以後,調用老介面的實現方法,
        //完成了新老介面的適配
        private void openResource()
        {
            Console.WriteLine("do some special task...");
            exAdapter.openResource();
        }
        private void closeResource()
        {
            Console.WriteLine("do some special task...");
            exAdapter.closeResource();
        }
        private void freeResource()
        {
            Console.WriteLine("do some special task...");
            exAdapter.freeResource();
        }

    }

/////////////////////////////////////////////////////////////////////

class Program
    {
        static void Main(string[] args)
        {
            //實現了介面的執行個體的使用
            IAdapter instance = new UseAdapter("simple");
            instance.NewOpenResource();
            instance.NewColseResource();
            instance.NewFreeResource();
        }
    }

///////////////////////////////////////////////////////////////////////////

      以上就是我簡化了的各類的實現,僅供沒有用過的兄弟參考,歡迎高手打擊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.