C#适配器模式

前言

我昨天做了一个梦。我梦见我走在路上。当我走路时,我经过一个房间,里面有一个边境牧羊人,里面关着鸡和猪。然后一觉醒来,我知道边牧就是小野(哈子十一的边牧),而小野有放羊养鸡的本领,所以如果想把边牧或者狗变成小野,那么首先,它要赋予它放羊的能力,而十一(哈仔十一的哈士奇)有卖萌的技能

适配器模式

p>

让我们先定义一些代码


    internal interface IDog
    {
        public void Eat();
        public void Bark();
    }
    /// 
    /// 哈士奇
    /// 
    internal interface IHuskyDog:IDog
    {
        /// 
        /// 破坏、拆家
        /// 
        public void Demolition();
    }
    /// 
    /// 牧羊犬
    /// 
    internal interface IShepherdDog:IDog
    {
        /// 
        /// 牧羊
        /// 
        public void Shepherd();
    }
    /// 
    /// 小叶子、小椰汁
    /// 
    internal class XiaoYezi : IShepherdDog
    {
        public void Bark()
        {
            Console.WriteLine($"I Am {nameof(XiaoYezi)} 汪 汪 汪 !!!!!");
        }
        public void Eat()
        {
            Console.WriteLine($"I Am {nameof(XiaoYezi)} 好吃 !!!!!");
        }
        /// 
        /// 牧羊
        /// 
        public void Shepherd()
        {
            Console.WriteLine($"I Am {nameof(XiaoYezi)}, 我在牧羊 !!!!!");
        }
    }
    /// 
    /// 十一
    /// 
    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)} 好吃得停不下来 !!!!!");
        }
    }

那么,如何用代码编写呢?也许你会定义一个改变叶子的接口,有一个方法调用,让它实现牧羊人的技能,但这里其实主要有两个问题:

此时适配器模式可以适应这种情况:

 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();
            }
        }
        /// 
        /// 牧鸡
        /// 
        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;
        }
        /// 
        /// 吠
        /// 
        /// 
        public void Bark()
        {
            Dog.Bark();
        }
        /// 
        /// 拆家
        /// 
        /// 
        public void Demolition()
        {
            if (Dog is IHuskyDog huskyDog)
            {
                huskyDog.Demolition();
            }
        }
        /// 
        /// 吃
        /// 
        /// 
        public void Eat()
        {
            Dog.Eat();
        }
        /// 
        /// 卖萌
        /// 
        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 ,拆家啦 !!!!!

你看,小叶有卖萌的技能,而且因为拆是哈士奇技能,所以小叶子没有拆。其实还是卖萌萌的牧羊鸡只是哈仔十一(大哈)的一个特色,那么是不是可以为大哈家定义一个适配器呢?


    /// 
    /// 哈仔十一家的狗适配器
    /// 
    internal class DaHaFamilyDogAdapter
    {
        public IDog Dog { get; set; }
        public DaHaFamilyDogAdapter(IDog dog)
        {
            Dog = dog;
        }
        /// 
        /// 牧鸡
        /// 
        public void HerdingChicken()
        {
            Console.WriteLine($"{Dog.GetType().Name} 在牧鸡");
        }
        /// 
        /// 卖萌
        /// 
        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 在卖萌

© 版权声明
THE END
喜欢就支持一下吧
点赞259赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容