static void Main(string[] args) { Console.WriteLine("类适配器模式测试:"); ITarget classAdapter = new ClassAdapter(); classAdapter.Request(); Console.WriteLine("对象适配器模式测试:"); ITarget objectAdapter = new ObjectAdapter(new Adaptee()); objectAdapter.Request(); }
static void Main(string[] args) { ITarget classAdapter = new ClassAdapter(); classAdapter.Convert(); LogHelper.LogSeparator(); ITarget objectAdapter = new ObjectAdapter(); objectAdapter.Convert(); Console.ReadLine(); }
static void Main(string[] args) { /* * 场景:之前项目的消息模块是用C#写的,现在改用成熟的第三方Python库来实现,由于时间仓促,部分功能依然沿用C#实现 * * */ #if oject IMessageHandleWithPython messageHandleWithPythonAdapter = new ClassAdapter(); messageHandleWithPythonAdapter.HandleMessageWithPython(); #else MessageModuleWithPython messageModuleWithPython = new ObjectAdapter(); messageModuleWithPython.HandleMessageWithPython(); #endif Console.ReadKey(); }
static void Main(string[] args) { //Mallard duck is the target class which is derived from IDuck //WildTurkey is the adaptee derived from ITurkey //TurkeyAdapter is the adapter which adapts to IDuck which client knows so implements IDuck //But since the behaviour is of type WildTurkey an instance of WildTurkey is passed in the constuctor. /*If we have a third party API and client does not know about these api's then we can come up with the class * which is derived from the class known to the client and provide a constructor which takes the third party API instance * Each of the methods in the known class will be modified to call the third pary api.*/ Console.WriteLine("\nThis is Mallard actions:"); IDuck mallardDuck = new MallardDuck(); mallardDuck.Quack(); mallardDuck.Fly(); Console.WriteLine("\nThis is Wild Turkey actions:"); ITurkey wildTurkey = new WildTurkey(); wildTurkey.Gobble(); wildTurkey.Fly(); \ IDuck turkeyAdapter = new TurkeyAdapter(wildTurkey); Console.WriteLine("\nThis is Duck Adapter actions:"); TestDuck(turkeyAdapter); DuckAdapter duckAdapter = new DuckAdapter(mallardDuck); Console.WriteLine("\nThis is Turkey Adapter actions:"); TestDuck(duckAdapter); IDuck classAdapter = new ClassAdapter(wildTurkey); Console.WriteLine("\nThis is class Adapter actions:"); TestDuck(classAdapter); Console.ReadLine(); }