設計模式(十四):介面卡模式

K戰神發表於2016-01-17

一、定義

介面卡模式——使得新環境中不需要去重複實現已經存在了的實現而很好地把現有物件(指原來環境中的現有物件)加入到新環境來使用

二、例項:客戶端對於傳送資料的介面不能直接使用,但是裡面的邏輯重寫又會重複。

介面返回資料:

 public class Reponser
    {
        public void Reponse()
        {
            Console.WriteLine("傳送資料...");
        }
    }

為了讓兩個類在一起工作:定義一箇中轉,通過它的實現來呼叫介面資料

 public interface ITarget
    {
        void GetData();
    }

介面卡:通過繼承,達到呼叫獲取介面資料的方法

 public class Adapter:Reponser,ITarget
    {
        public void GetData()
        {
            this.Reponse();
        }
    }

客戶端:

 //---------------------介面卡模式----------------------
            Adapter adapter = new Adapter();
            adapter.Reponse();
            Console.ReadKey();

三、總結

很多模式我們平時在不知不覺中用著,還用的很好,只是沒有系統地瞭解定義。

這個模式比較簡單。

相關文章