裝飾模式(Decorator Pattern):動態的給一個對象添加一些額外的職責,就增加功能來說,裝飾模式比生成子類更爲靈活。
實例: 顯示一個人的穿衣
第一版: 直接使用一個Person類來實現穿衣
第二版: 把人和服飾類分開了,穿衣的一些表現通過繼承與服飾類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Decorate
{
class Person
{
private string name;
public Person(string name)
{
this.name = name;
}
public void Show()
{
Console.Write(" 裝扮的{0}", name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Decorate
{
abstract class Finery //n. 華麗、優雅的服裝或裝飾,作爲父類
{
public abstract void Show();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Decorate
{
class TShirts:Finery
{
public override void Show()
{
Console.Write(" 大T恤 ");
//throw new NotImplementedException();
}
}
class BigTrouser:Finery
{
public override void Show()
{
Console.Write(" 垮褲");
//throw new NotImplementedException();
}
}
class Sneakers:Finery
{
public override void Show()
{
Console.Write(" 球鞋");
//throw new NotImplementedException();
}
}
class Tie:Finery
{
public override void Show()
{
Console.Write(" 領帶");
//throw new NotImplementedException();
}
}
class LeatherShoes:Finery
{
public override void Show()
{
Console.Write(" 皮鞋");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Decorate
{
class Program
{
static void Main(string[] args)
{
/* Person xc = new Person("小菜");
Console.Write("第一種裝扮:\n");`
xc.WearTShirt();
xc.WearBigTrouser();
xc.WearSneakers();
xc.Show();
Console.Read();
*/
Person xc = new Person("小菜");
Finery dtx = new TShirts();
Finery kk = new BigTrouser();
Finery pqx = new Sneakers();
dtx.Show();
kk.Show();
pqx.Show();
xc.Show();
Console.Read();
}
}
}
第三版: 使用裝飾模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Decorate
{
class Program
{
static void Main(string[] args)
{
/* Person xc = new Person("小菜");
Console.Write("第一種裝扮:\n");`
xc.WearTShirt();
xc.WearBigTrouser();
xc.WearSneakers();
xc.Show();
Console.Read();
*/
/* Person xc = new Person("小菜");
Finery dtx = new TShirts();
Finery kk = new BigTrouser();
Finery pqx = new Sneakers();
dtx.Show();
kk.Show();
pqx.Show();
xc.Show();
*/
Person xc = new Person("小菜");
Sneakers pqx = new Sneakers(); /// 使用的Person的構造函數構造的
BigTrouser kk = new BigTrouser();
TShirts dtx = new TShirts();
// 裝飾過程
pqx.Decorate(xc);
kk.Decorate(pqx);
dtx.Decorate(kk);
dtx.Show();
Console.Read();
}
}
}
// 人類
class Person
{
public Person()
{
}
private string name;
public Person(string name)
{
this.name = name;
}
public virtual void Show()
{
Console.WriteLine(" 裝扮的{0}", name);
}
}
// 裝飾類
class Finery : Person
{
protected Person component; /// 成分,零件,組成
public void Decorate(Person component)
{
this.component = component;
}
public override void Show()
{
if (component != null)
{
component.Show();
}
}
}
class TShirts : Finery
{
public override void Show()
{
Console.Write(" 大T恤 ");
base.Show();
//throw new NotImplementedException();
}
}
class BigTrouser : Finery
{
public override void Show()
{
Console.Write(" 垮褲");
base.Show();
//throw new NotImplementedException();
}
}
class Sneakers : Finery
{
public override void Show()
{
Console.Write(" 球鞋");
base.Show();
//throw new NotImplementedException();
}
}
class Tie : Finery
{
public override void Show()
{
Console.Write(" 領帶");
base.Show();
//throw new NotImplementedException();
}
}
class LeatherShoes : Finery
{
public override void Show()
{
Console.Write(" 皮鞋");
base.Show();
}
}
裝飾模式總結: 把類中的裝飾功能從類中搬移出去,可以簡化類,能有效的吧核心職責和裝飾功能分開。取出相關類的裝飾邏輯。