装饰者模式 DecoratorPattern

本文介绍了一个使用C#实现的装饰者模式案例,通过抽象基类和具体装饰者类的组合,动态地为对象添加职责。具体展示了从创建鞋子生产线到最终销售推广的整个过程。

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(); } } }

调用代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值