前言
我昨天做了個夢,我夢見我在一條路走,走的時候經過一個房間,裡面關著一條邊牧和雞和豬,後來我醒了,我知道那隻邊牧就是小葉子(哈仔十一的邊牧),小葉子具備牧羊和牧雞的技能,因此,假如我想把一隻邊牧或者一隻狗變成小葉子那樣,那麼首先就得讓其賦予牧雞的本事,而十一(哈仔十一的哈士奇)則具備賣萌的技能
介面卡模式
我們先定義一些程式碼
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 在賣萌