修饰模式
修饰模式,是面向对象编程领域中,一种动态地往一个类中添加新的行为的设计模式。就功能而言,修饰模式相比生成子类更为灵活,这样可以给某个对象而不是整个类添加一些功能。[1]
目录 |
介绍 [编辑]
通过使用修饰模式,可以在运行时扩充一个类的功能。原理是:增加一个修饰类包裹原来的类,包裹的方式一般是通过在将原来的对象作为修饰类的构造函数的参数。装饰类实现新的功能,但是,在不需要用到新功能的地方,它可以直接调用原来的类中的方法。修饰类必须和原来的类有相同的接口。
修饰模式是类继承的另外一种选择。类继承在编译时候增加行为,而装饰模式是在运行时增加行为。
当有几个相互独立的功能需要扩充时,这个区别就变得很重要。在有些面向对象的编程语言中,类不能在运行时被创建,通常在设计的时候也不能预测到有哪几种功能组合。这就意味著要为每一种组合创建一个新类。相反,修饰模式是面向运行时候的对象实例的,这样就可以在运行时根据需要进行组合。一个修饰模式的示例是JAVA里的Java I/O Streams的实现。
动机 [编辑]
例如,一个窗口系统中的窗口,允许这个窗口内容滚动,我们希望给它添加水平或垂直滚动条。假设窗口通过“Window”类实例来表示,并且假设它没有添加滚动条功能。我们可以创建一个子类“ScrollingWindow”来提供,或者我们可以创建一个ScrollingWindowDecorator来为已存在的Window对象添加这个功能。在这点上,只要是解决方案就可以了。 现在我们假设希望选择给我们的窗口添加边框,同样,我们的原始Window类不支持。ScrollingWindow子类现在会造成一个问题,因为它会有效的创建一种新的窗口。如果我们想要给所有窗口添加边框,我们必须创建WindowWithBorder和ScrollingWindowWithBorder子类。显然,这个问题由于被添加类而变得更糟了。对于修饰模式,我们简单的创建一个新类BorderedWindowDecorator,在运行时,我们能够使用ScrollingWindowDecorator或BorderedWindowDecorator或两者结合来修饰已存在的窗口。 一个修饰能够被应用的另一个好例子是当有需要根据某套规则或者几个平行的规则集(不同的用户凭据等)限制访问对象的属性或方法时。
一个对象的属性或方法按照某组规则或几个并行规则(不同用户证书等)需要限制访问时,在这种情况下,不是在原始对象中实现访问控制而是在他的使用中不变或不知道任何限制,并且他被包装在一个访问控制修饰对象中,这个对象能够对允许的原始对象的接口子集服务。
应用 [编辑]
代码示例 [编辑]
这个JAVA示例使用window/scrolling情境。
// the Window interface interface Window { public void draw(); // draws the Window public String getDescription(); // returns a description of the Window } // implementation of a simple Window without any scrollbars class SimpleWindow implements Window { public void draw() { // draw window } public String getDescription() { return "simple window"; } }
以下类包含所有Window类的decorator,以及修饰类本身。
// abstract decorator class - note that it implements Window abstract class WindowDecorator implements Window { protected Window decoratedWindow; // the Window being decorated public WindowDecorator (Window decoratedWindow) { this.decoratedWindow = decoratedWindow; } } // the first concrete decorator which adds vertical scrollbar functionality class VerticalScrollBarDecorator extends WindowDecorator { public VerticalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { drawVerticalScrollBar(); decoratedWindow.draw(); } private void drawVerticalScrollBar() { // draw the vertical scrollbar } public String getDescription() { return decoratedWindow.getDescription() + ", including vertical scrollbars"; } } // the second concrete decorator which adds horizontal scrollbar functionality class HorizontalScrollBarDecorator extends WindowDecorator { public HorizontalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } public void draw() { drawHorizontalScrollBar(); decoratedWindow.draw(); } private void drawHorizontalScrollBar() { // draw the horizontal scrollbar } public String getDescription() { return decoratedWindow.getDescription() + ", including horizontal scrollbars"; } }
以下是一个测试程序,它创建了一个包含多重装饰的Window实例(如,包含了垂直的和水平的滚动条),然后输出它的描述:
public class DecoratedWindowTest { public static void main(String[] args) { // create a decorated Window with horizontal and vertical scrollbars Window decoratedWindow = new HorizontalScrollBarDecorator ( new VerticalScrollBarDecorator(new SimpleWindow())); // print the Window's description System.out.println(decoratedWindow.getDescription()); } }
C++代码示例:
#include <iostream> using namespace std; /* Component (interface) */ class Widget { public: virtual void draw() = 0; virtual ~Widget() {} }; /* ConcreteComponent */ class TextField : public Widget { private: int width, height; public: TextField( int w, int h ){ width = w; height = h; } void draw() { cout << "TextField: " << width << ", " << height << '\n'; } }; /* Decorator (interface) */ class Decorator : public Widget { private: Widget* wid; // reference to Widget public: Decorator( Widget* w ) { wid = w; } void draw() { wid->draw(); } ~Decorator() { delete wid; } }; /* ConcreteDecoratorA */ class BorderDecorator : public Decorator { public: BorderDecorator( Widget* w ) : Decorator( w ) { } void draw() { Decorator::draw(); cout << " BorderDecorator" << '\n'; } }; /* ConcreteDecoratorB */ class ScrollDecorator : public Decorator { public: ScrollDecorator( Widget* w ) : Decorator( w ) { } void draw() { Decorator::draw(); cout << " ScrollDecorator" << '\n'; } }; int main( void ) { Widget* aWidget = new BorderDecorator( new BorderDecorator( new ScrollDecorator( new TextField( 80, 24 )))); aWidget->draw(); delete aWidget; }
C#代码示例:
namespace GSL_Decorator_pattern { interface IWindowObject { void Draw(); // draws the object string GetDescription(); // returns a description of the object } class ControlComponent : IWindowObject { public ControlComponent() { } public void Draw() // draws the object { Console.WriteLine( "ControlComponent::draw()" ); } public string GetDescription() // returns a description of the object { return "ControlComponent::getDescription()"; } } abstract class Decorator : IWindowObject { protected IWindowObject _decoratedWindow = null; // the object being decorated public Decorator( IWindowObject decoratedWindow) { _decoratedWindow = decoratedWindow; } public virtual void Draw() { _decoratedWindow.Draw(); Console.WriteLine("\tDecorator::draw() "); } public virtual string GetDescription() // returns a description of the object { return _decoratedWindow.GetDescription() + "\n\t" + "Decorator::getDescription() "; } } // the first decorator class DecorationA : Decorator { public DecorationA(IWindowObject decoratedWindow) : base(decoratedWindow) { } public override void Draw() { base.Draw(); DecorationAStuff(); } private void DecorationAStuff() { Console.WriteLine("\t\tdoing DecorationA things"); } public override string GetDescription() { return base.GetDescription() + "\n\t\tincluding " + this.ToString(); } }// end class ConcreteDecoratorA : Decorator class DecorationB : Decorator { public DecorationB(IWindowObject decoratedWindow) : base(decoratedWindow) { } public override void Draw() { base.Draw(); DecorationBStuff(); } private void DecorationBStuff() { Console.WriteLine("\t\tdoing DecorationB things"); } public override string GetDescription() { return base.GetDescription() + "\n\t\tincluding " + this.ToString(); } }// end class DecorationB : Decorator class DecorationC : Decorator { public DecorationC(IWindowObject decoratedWindow) : base(decoratedWindow) { } public override void Draw() { base.Draw(); DecorationCStuff(); } private void DecorationCStuff() { Console.WriteLine("\t\tdoing DecorationC things"); } public override string GetDescription() { return base.GetDescription() + "\n\t\tincluding " + this.ToString(); } }// end class DecorationC : Decorator }// end of namespace GSL_Decorator_patternpublic class MyClass
参考资料 [编辑]
- ^ Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. 设计模式:可复用面向对象软件的基础. 北京: 机械工业出版社. 2000: 115. ISBN 9787111075752 (中文).
外部链接 [编辑]
- Article "The Decorator Design Pattern" (Java) by Antonio García and Stephen Wong
- Article "Using the Decorator Pattern" (Java) by Budi Kurniawan
- Sample Chapter "C# Design Patterns: The Decorator Pattern" (C#) by James W. Cooper
- A PHP approach (PHP)
- A Delphi approach (Delphi)
- A JavaScript implementation
- Jt J2EE Pattern Oriented Framework
- Decorator Pattern with Ruby in 8 Lines (Ruby)