3、介面卡模式
阿新 • • 發佈:2021-12-21
3、介面卡模式 提供介面卡的類,此類可以根據傳參的類,執行不同的類的實現
namespace SJ.DesignMode { /// <summary> /// 設計模式 /// </summary> class Program { static void Main(string[] args) { IAdapter iadapterBig = new AdapterBigMan(); IAdapter iadapterLittle = new AdapterLittleMan(); Adapter adapter = new Adapter(iadapterLittle); adapter.Write(); } } public class Adapter { private IAdapter _adapter = null; public Adapter(IAdapter adapter) { _adapter = adapter; } public void Write() { _adapter.Write(); } } public interface IAdapter { void Write(); } public class AdapterBigMan : IAdapter { public void Write() { Console.WriteLine("Big man !"); } } public class AdapterLittleMan : IAdapter { public void Write() { Console.WriteLine("Little man !"); } } }