Design
Pattern : Decorator (2)
需要在既有類別上新增新功能,
在不更動原有的介面和類別,也不做繼承的情況下,便可以使用裝飾模式。
車廠老闆要擺出展示有特殊設備的房車,吸引更多來試駕的客戶。
車廠老闆要擺出展示有特殊設備的房車,吸引更多來試駕的客戶。
但是要加什麼設備,可能每個月不一定,有可能是增強馬力或是放入超重低音音響或 …。
1. 既有的車子Interface/Class
public interface ICar
{
DPLib.Color Color { get; set; }
String Name { get; set; }
String Equipment { get; set; }
void Drive();
}
public class Focus : ICar
{
private const String BRAND = "FORD";
public Focus()
{
//取得當前類別名稱
this.Name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
}
public DPLib.Color Color { get; set; }
public String Name { get; set; }
public String Equipment { get; set; }
public void Drive()
{
Console.WriteLine("I am driving {0} {1}({2}) {3}",
this.Color.ToString(), this.Name, BRAND, (this.Equipment??""));
}
}
public interface ICar
{
DPLib.Color Color { get; set; }
String Name { get; set; }
String Equipment { get; set; }
void Drive();
}
public class Focus : ICar
{
private const String BRAND = "FORD";
public Focus()
{
//取得當前類別名稱
this.Name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
}
public DPLib.Color Color { get; set; }
public String Name { get; set; }
public String Equipment { get; set; }
public void Drive()
{
Console.WriteLine("I am driving {0} {1}({2}) {3}",
this.Color.ToString(), this.Name, BRAND, (this.Equipment??""));
}
}
2. 新增一個展示車類別(實作ICar):
展示車的Drive方法會特別顯示為”試駕”。
public class ExhibitCar:DPLib.ICar
{
public String Name { get; set; }
public DPLib.Color Color { get; set; }
public String Equipment { get; set; }
public virtual void Drive()
{
Console.WriteLine("試駕 {0} {1} {2}",
this.Color.ToString(), this.Name, (this.Equipment ?? ""));
}
}
展示車的Drive方法會特別顯示為”試駕”。
public class ExhibitCar:DPLib.ICar
{
public String Name { get; set; }
public DPLib.Color Color { get; set; }
public String Equipment { get; set; }
public virtual void Drive()
{
Console.WriteLine("試駕 {0} {1} {2}",
this.Color.ToString(), this.Name, (this.Equipment ?? ""));
}
}
3. Decorator :
Decorator有個最重要的地方,就是它的建構子是帶入我們要裝飾的類別,也就是ICar
public abstract class CarDecorator :ExhibitCar
{
private DPLib.ICar car;
public CarDecorator(DPLib.ICar _car)
{
this.car = _car;
}
}
Decorator有個最重要的地方,就是它的建構子是帶入我們要裝飾的類別,也就是ICar
public abstract class CarDecorator :ExhibitCar
{
private DPLib.ICar car;
public CarDecorator(DPLib.ICar _car)
{
this.car = _car;
}
}
4. 接下來開始建立繼承 Decorator
的類別
/// <summary>
/// 展示版本的Focus
/// </summary>
public class FocusE:CarDecorator
{
private const DPLib.Color EX_COLOR = DPLib.Color.White;
private const String EX_EUIPMENT="渦輪增壓";
public FocusE(DPLib.ICar _car): base(_car)
{
_car.Name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
_car.Color = EX_COLOR;
_car.Equipment = _car.Equipment + @"[" + EX_EUIPMENT + @"]";
}
public override void Drive()
{
base.Drive();
}
}
/// <summary>
/// 加上重低音音響版本的Focus
/// </summary>
public class FocusJ:CarDecorator
{
private const DPLib.Color EX_COLOR = DPLib.Color.White;
private const String EX_EUIPMENT="重低音音響";
public FocusJ(DPLib.ICar _car): base(_car)
{
_car.Name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
_car.Color = EX_COLOR;
_car.Equipment = _car.Equipment + @"["+EX_EUIPMENT+@"]";
}
public override void Drive()
{
base.Drive();
}
}
/// <summary>
/// 展示版本的Focus
/// </summary>
public class FocusE:CarDecorator
{
private const DPLib.Color EX_COLOR = DPLib.Color.White;
private const String EX_EUIPMENT="渦輪增壓";
public FocusE(DPLib.ICar _car): base(_car)
{
_car.Name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
_car.Color = EX_COLOR;
_car.Equipment = _car.Equipment + @"[" + EX_EUIPMENT + @"]";
}
public override void Drive()
{
base.Drive();
}
}
/// <summary>
/// 加上重低音音響版本的Focus
/// </summary>
public class FocusJ:CarDecorator
{
private const DPLib.Color EX_COLOR = DPLib.Color.White;
private const String EX_EUIPMENT="重低音音響";
public FocusJ(DPLib.ICar _car): base(_car)
{
_car.Name = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
_car.Color = EX_COLOR;
_car.Equipment = _car.Equipment + @"["+EX_EUIPMENT+@"]";
}
public override void Drive()
{
base.Drive();
}
}
u
取得ICar物件後,我們可以在它原本的內容再裝飾(新增或修改)新的內容。 例如上面程式碼中在車子的配備上再多加上馬力、重低音音響
5. 主程式
///宣告一台展示車
DPLib.ICar car = new ExhibitCar();
///Decorator為展示的Focus
object carE = new FocusE(car);
///Decorator為加上重低音音響的Focus
object carJ = new FocusJ(car);
car.Drive();
結果:
試駕 White FocusJ [渦輪增壓][重低音音響]
///宣告一台展示車
DPLib.ICar car = new ExhibitCar();
///Decorator為展示的Focus
object carE = new FocusE(car);
///Decorator為加上重低音音響的Focus
object carJ = new FocusJ(car);
car.Drive();
結果:
試駕 White FocusJ [渦輪增壓][重低音音響]
沒有留言:
張貼留言