类图
using System; abstract class MakeLine { protected MakeLine _MakeLine; private String _Description; public string Description { get { return _Description; } set { _Description = value; } } public abstract void Process(); }
公共抽象
using System; class MakeLineDecorator:MakeLine { public MakeLineDecorator(MakeLine makeline,String description) { _MakeLine = makeline; Description = description; } public override void Process() { _MakeLine.Process(); Console.WriteLine(Description + " is working now! "); } }
公共抽象实例A(装饰者)
using System; class ShoesMakeLine : MakeLine { public ShoesMakeLine(String descpirtion) { base.Description = descpirtion; } public override void Process() { Console.WriteLine(Description + "beggin to make shoes now!"); } }
公共抽象实例B(调用者)
using System; class BoxShoes : MakeLineDecorator { public BoxShoes(MakeLine makeline, string description) : base(makeline, description) { } }
装饰对象A
using System; class CheapSell : MakeLineDecorator { public CheapSell(MakeLine makeline, string description):base(makeline,description) { } }
装饰对象B
using System; class MakeModel : MakeLineDecorator { public MakeModel(MakeLine makeline, string description):base(makeline,description) { } }
装饰对象C
using System; class PaintColor : MakeLineDecorator { public PaintColor(MakeLine makeline, string description) : base(makeline, description) { } }
装饰对象D
using System; using System.Collections.Generic; using System.Text; namespace DecoratorPattern { class Program { static void Main(string[] args) { ShoesMakeLine shoesMakeLine = new ShoesMakeLine("耐克鞋厂"); MakeModel makeModel = new MakeModel(shoesMakeLine, "设计模型"); PaintColor paintColor = new PaintColor(makeModel, "喷涂颜色"); BoxShoes boxShoes = new BoxShoes(paintColor, "包装发货"); CheapSell cheapSell = new CheapSell(boxShoes, "上街推广"); cheapSell.Process(); Console.ReadKey(); } } }
调用代码