C#适配器模式
RyzenAdorer 人气:0前言
我昨天做了个梦,我梦见我在一条路走,走的时候经过一个房间,里面关着一条边牧和鸡和猪,后来我醒了,我知道那只边牧就是小叶子(哈仔十一的边牧),小叶子具备牧羊和牧鸡的技能,因此,假如我想把一只边牧或者一只狗变成小叶子那样,那么首先就得让其赋予牧鸡的本事,而十一(哈仔十一的哈士奇)则具备卖萌的技能
适配器模式
我们先定义一些代码
internal interface IDog { public void Eat(); public void Bark(); } /// <summary> /// 哈士奇 /// </summary> internal interface IHuskyDog:IDog { /// <summary> /// 破坏、拆家 /// </summary> public void Demolition(); } /// <summary> /// 牧羊犬 /// </summary> internal interface IShepherdDog:IDog { /// <summary> /// 牧羊 /// </summary> public void Shepherd(); } /// <summary> /// 小叶子、小椰汁 /// </summary> internal class XiaoYezi : IShepherdDog { public void Bark() { Console.WriteLine($"I Am {nameof(XiaoYezi)} 汪 汪 汪 !!!!!"); } public void Eat() { Console.WriteLine($"I Am {nameof(XiaoYezi)} 好吃 !!!!!"); } /// <summary> /// 牧羊 /// </summary> public void Shepherd() { Console.WriteLine($"I Am {nameof(XiaoYezi)}, 我在牧羊 !!!!!"); } } /// <summary> /// 十一 /// </summary> internal class ShiYi : IHuskyDog { public void Bark() { Console.WriteLine($"I Am {nameof(ShiYi)} 汪 汪 汪 !!!!!"); } public void Demolition() { Console.WriteLine($"I Am {nameof(ShiYi)} ,拆家啦 !!!!!"); } public void Eat() { Console.WriteLine($"I Am {nameof(ShiYi)} 好吃得停不下来 !!!!!"); } }
那么,用代码怎么写呢?或许你会定义一个变叶子的接口,有个方法叫HerdingChicken
,让其实现牧鸡的技能就行,但是实际上这里有两个主要问题:
- 叶子本身是一个狗,如果其他人实现了这个接口,他能变叶子?是不是前提他是狗
- 假如叶子不单单只是牧鸡,她会牧鹅、猪、牧自己?还得多接口,或者在变叶子的接口加方法给其实现
这时候适配器模式就是可以适应这种情况:
internal class XiaoYeziAdapter : IShepherdDog { public IDog Dog { get; set; } public XiaoYeziAdapter(IDog dog) { Dog = dog; } public void Bark() { Dog.Bark(); } public void Eat() { Dog.Eat(); } public void Shepherd() { if (Dog is IShepherdDog shepherdDog) { shepherdDog.Shepherd(); } } /// <summary> /// 牧鸡 /// </summary> public void HerdingChicken() { Console.WriteLine($"{Dog.GetType().Name} is Herding Chicken"); } }
代码如下:
var xiaoYezi = new XiaoYezi(); var xiaoYeziAdapter = new XiaoYeziAdapter(xiaoYezi); xiaoYeziAdapter.Bark(); xiaoYeziAdapter.Eat(); xiaoYeziAdapter.HerdingChicken(); Console.WriteLine("---------------- \n"); var shiYi = new ShiYi(); xiaoYeziAdapter = new XiaoYeziAdapter(shiYi); xiaoYeziAdapter.Bark(); xiaoYeziAdapter.Eat(); xiaoYeziAdapter.HerdingChicken();
输出:
I Am XiaoYezi 汪 汪 汪 !!!!!
I Am XiaoYezi 好吃 !!!!!
XiaoYezi 在牧鸡
----------------I Am ShiYi 汪 汪 汪 !!!!!
I Am ShiYi 好吃得停不下来 !!!!!
ShiYi 在牧鸡
可以看到,十一用了小叶子适配器,也具备了牧鸡的功能
那么反过来,叶子变十一呢?那就定义一个十一适配器呗
internal class ShiYiAdapter : IHuskyDog { public IDog Dog { get; set; } public ShiYiAdapter(IDog dog) { Dog=dog; } /// <summary> /// 吠 /// </summary> /// <exception cref="NotImplementedException"></exception> public void Bark() { Dog.Bark(); } /// <summary> /// 拆家 /// </summary> /// <exception cref="NotImplementedException"></exception> public void Demolition() { if (Dog is IHuskyDog huskyDog) { huskyDog.Demolition(); } } /// <summary> /// 吃 /// </summary> /// <exception cref="NotImplementedException"></exception> public void Eat() { Dog.Eat(); } /// <summary> /// 卖萌 /// </summary> public void ActingCute() { Console.WriteLine($"{Dog.GetType().Name} 卖萌啦!!"); } }
代码如下:
var xiaoYezi = new XiaoYezi(); var shiYiAdapter = new ShiYiAdapter(xiaoYezi); shiYiAdapter.Bark(); shiYiAdapter.ActingCute(); shiYiAdapter.Demolition(); Console.WriteLine("---------------- \n"); var shiYi = new ShiYi(); shiYiAdapter = new ShiYiAdapter(shiYi); shiYiAdapter.Bark(); shiYiAdapter.ActingCute(); shiYiAdapter.Demolition();
输出:
I Am XiaoYezi 汪 汪 汪 !!!!!
XiaoYezi 卖萌啦!!
----------------I Am ShiYi 汪 汪 汪 !!!!!
ShiYi 卖萌啦!!
I Am ShiYi ,拆家啦 !!!!!
看,小叶子具备卖萌的技能了,而且由于拆家是哈士奇的技能,因此小叶子拆家没拆到,实际上,或者卖萌和牧鸡只是哈仔十一(大哈)家的特色,那么是不是可以定义一个大哈家的适配器
/// <summary> /// 哈仔十一家的狗适配器 /// </summary> internal class DaHaFamilyDogAdapter { public IDog Dog { get; set; } public DaHaFamilyDogAdapter(IDog dog) { Dog = dog; } /// <summary> /// 牧鸡 /// </summary> public void HerdingChicken() { Console.WriteLine($"{Dog.GetType().Name} 在牧鸡"); } /// <summary> /// 卖萌 /// </summary> public void ActingCute() { Console.WriteLine($"{Dog.GetType().Name} 在卖萌"); } }
那么
var xiaoYezi = new XiaoYezi(); var daHaFamilyDogAdapter = new DaHaFamilyDogAdapter(xiaoYezi); daHaFamilyDogAdapter.Bark(); daHaFamilyDogAdapter.Eat(); daHaFamilyDogAdapter.HerdingChicken(); daHaFamilyDogAdapter.ActingCute(); Console.WriteLine("---------------- \n"); var shiYi = new ShiYi(); daHaFamilyDogAdapter = new DaHaFamilyDogAdapter(shiYi); daHaFamilyDogAdapter.Bark(); daHaFamilyDogAdapter.Eat(); daHaFamilyDogAdapter.HerdingChicken(); daHaFamilyDogAdapter.ActingCute();
输出:
I Am XiaoYezi 汪 汪 汪 !!!!!
I Am XiaoYezi 好吃 !!!!!
XiaoYezi 在牧鸡
XiaoYezi 在卖萌
----------------I Am ShiYi 汪 汪 汪 !!!!!
I Am ShiYi 好吃得停不下来 !!!!!
ShiYi 在牧鸡
ShiYi 在卖萌
加载全部内容