p>But, lets keep it with a rectangle:public class Rectangle : IDrawable{ private IDrawable _drawable; public class Rectangle(IDrawable drawable) { _drawable = drawable; //just to make it uniform } private InernalDraw() { ... } public void Draw() { //do the drawing magic InernalDraw(); //some drawing code //and do something with the decorated item _drawable?.Draw(); }}没什么特别的。但是让我们假设我们要在矩形周围绘制边框。Nothing special yet. But let us assume we want to draw a border around the rectangle.让我们创建一个可绘制边框:Let us create a border drawable:public class Border : IDrawable{ private IDrawable _drawable; public class Border(IDrawable drawable) { _drawable = drawable; } private InternalDrawWithSomeSpecialLogicAndStuff() { ... } public void Draw() { //draw the decorated item: _drawable?.Draw(); //and work out some magic to draw a border around it, //note that properties as dimensions would be helpful. InternalDrawWithSomeSpecialLogicAndStuff(); }}现在,让我们考虑以下代码:Now, let us consider the following code:public static void Main(){ IDrawable borderlessRectangle = new Rectangle(null); IDrawable borderedRectangle = new Border(borderlessRectangle); borderlessRectangle.Draw();//gives a rectangle without a border borderedRectangle.Draw();//gives a rectangle with a border} 这篇关于简化的装饰图案。那是对的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 11:22