《Head First 設計模式》C#版:三、修飾者模式
阿新 • • 發佈:2019-02-17
using UnityEngine;
using System.Collections;
/// <summary>
/// 裝飾著模式
/// </summary>
public class DecoratorPattern : MonoBehaviour
{
private void Start()
{
Beverage beverage = new Espresso(Beverage.CFSIZE.TALL);
Debug.Log(beverage.GetDescription() + " $ " + beverage.Cost());
Beverage beverage2 = new DarkRoast(Beverage.CFSIZE.GRANDE);
beverage2 = new MoCha(beverage2);
beverage2 = new MoCha(beverage2);
beverage2 = new Milk(beverage2);
Debug.Log(beverage2.GetDescription() + " $ " + beverage2.Cost());
Beverage beverage3 = new HouseBlend(Beverage.CFSIZE.VENTI);
beverage3 = new Soy(beverage3);
beverage3 = new MoCha(beverage3);
beverage3 = new Milk(beverage3);
Debug.Log(beverage3.GetDescription() + " $ " + beverage3.Cost());
Beverage beverage4 = new Decaf(Beverage.CFSIZE.TALL);
beverage4 = new Soy(beverage4);
beverage4 = new MoCha(beverage4);
beverage4 = new Milk(beverage4);
beverage4 = new Soy(beverage4);
Debug.Log(beverage4.GetDescription() + " $ " + beverage4.Cost());
}
}
#region 星巴克咖啡店
// ============ 實現裝飾者 及咖啡的配料 ============
public class Milk : CondimentDecorator
{
protected Beverage m_veverage;
public Milk(Beverage beverage)
{
m_veverage = beverage;
}
public override string GetDescription()
{
return m_veverage.GetDescription() + ", 牛奶";
}
public override double Cost()
{
return m_veverage.Cost() + 0.4;
}
public override CFSIZE GetSize()
{
return m_cfSize;
}
}
public class Soy : CondimentDecorator
{
protected Beverage m_beverage;
public Soy(Beverage beverage)
{
m_beverage = beverage;
}
public override string GetDescription()
{
return m_beverage.GetDescription() + ", 豆漿 ";
}
public override double Cost()
{
return m_beverage.Cost() + 0.2;
}
public override CFSIZE GetSize()
{
return m_cfSize;
}
}
/// <summary>
/// Mocha.裝飾者
/// </summary>
public class MoCha : CondimentDecorator
{
protected Beverage m_beverage;
public MoCha(Beverage beverage)
{
m_beverage = beverage;
}
public override string GetDescription()
{
return m_beverage.GetDescription() + ", 摩卡";
}
public override double Cost()
{
return m_beverage.Cost() + 0.2;
}
public override CFSIZE GetSize()
{
return m_cfSize;
}
}
// ======= 抽象元件 beverage 具體元件 houseBlend 抽象裝飾者 CondimentDecorator ======
/*
主要知識點:
Java 繼承中的方法變更只有 Override 一種方式,作用於虛擬函式表;
C# 繼承中的方法變更有 Override 和 New 兩種方式,前者與 Java 相同,後者不作用於虛擬函式表,只對顯式標記為子類的型別生效;
Java 繼承中的方法覆寫可以隱式實現(即任何方法都可以被覆寫,覆寫只需簽名相同即自動完成);
C# 繼承中的方法覆寫必須要顯式實現(即只有被標記為可覆寫的方法才能被覆寫,覆寫必須使用 Override 關鍵字);
*/
/// <summary>
/// 飲料 抽象類 Beverage.
/// </summary>
public abstract class Beverage
{
public enum CFSIZE
{
/// <summary>
/// 小杯
/// </summary>
TALL,
/// <summary>
/// 中杯
/// </summary>
GRANDE,
/// <summary>
/// 大杯
/// </summary>
VENTI
}
/// <summary>
/// 飲料品牌
/// </summary>
protected string m_description = "Unknown Beverage";
protected CFSIZE m_cfSize = CFSIZE.GRANDE;
protected double m_price = 0.0;
/// <summary>
/// 獲得當前品牌
/// </summary>
/// <returns>The description.</returns>
public virtual string GetDescription()
{
return m_description;
}
public abstract double Cost();
/// <summary>
/// 獲取咖啡杯大小
/// </summary>
/// <returns>The size.</returns>
public virtual CFSIZE GetSize(){
return m_cfSize;
}
}
/// <summary>
/// 調料的抽象類 也就是 裝飾類 Decorator
/// </summary>
public abstract class CondimentDecorator : Beverage
{
public abstract override string GetDescription();
public abstract override CFSIZE GetSize();
}
/// <summary>
/// 濃縮咖啡類
/// </summary>
public class Espresso : Beverage
{
public Espresso(CFSIZE cFSIZE)
{
m_description = "濃縮咖啡類";
m_price = 1.99;
m_cfSize = cFSIZE;
switch (m_cfSize)
{
case CFSIZE.TALL:
m_description += " Small ";
m_price += 0.1;
break;
case CFSIZE.GRANDE:
m_description += " Middle ";
m_price += 0.2;
break;
case CFSIZE.VENTI:
m_description += " Large ";
m_price += 0.3;
break;
}
}
public override double Cost()
{
return m_price;
}
}
public class DarkRoast : Beverage
{
public DarkRoast(CFSIZE cFSIZE)
{
m_description = "深培 ";
m_price = 0.8;
m_cfSize = cFSIZE;
switch (m_cfSize)
{
case CFSIZE.TALL:
m_description += " Small ";
m_price += 0.1;
break;
case CFSIZE.GRANDE:
m_description += " Middle ";
m_price += 0.2;
break;
case CFSIZE.VENTI:
m_description += " Large ";
m_price += 0.3;
break;
}
}
public override double Cost()
{
return m_price;
}
}
/// <summary>
/// 綜合咖啡
/// </summary>
public class HouseBlend : Beverage
{
public HouseBlend(CFSIZE cFSIZE)
{
m_description = "綜合咖啡 ";
m_price = 0.99;
m_cfSize = cFSIZE;
switch (m_cfSize)
{
case CFSIZE.TALL:
m_description += " Small";
m_price += 0.1;
break;
case CFSIZE.GRANDE:
m_description += " Middle";
m_price += 0.2;
break;
case CFSIZE.VENTI:
m_description += " Large";
m_price += 0.3;
break;
}
}
public override double Cost()
{
return m_price;
}
}
/// <summary>
/// 無咖啡因咖啡
/// </summary>
public class Decaf : Beverage
{
public Decaf(CFSIZE cFSIZE)
{
m_description = "無咖啡因咖啡 ";
m_price = 0.9;
m_cfSize = cFSIZE;
switch(m_cfSize){
case CFSIZE.TALL:
m_description += " Small";
m_price += 0.1;
break;
case CFSIZE.GRANDE:
m_description += " Middle";
m_price += 0.2;
break;
case CFSIZE.VENTI:
m_description += " Large";
m_price += 0.3;
break;
}
}
public override double Cost()
{
return m_price;
}
}
#endregion