text: Пример на языке Java public interface InterfaceComponent { void doOperation(); } class MainComponent implements InterfaceComponent { @Override public void doOperation() { System.out.print("World!"); } } abstract class Decorator implements InterfaceComponent { protected InterfaceComponent component; public Decorator (InterfaceComponent c) { component = c; } @Override public void doOperation() { component.doOperation(); } public void newOperation() { System.out.println("Do Nothing"); } } class DecoratorSpace extends Decorator { public DecoratorSpace(InterfaceComponent c) { super(c); } @Override public void doOperation() { System.out.print(" "); super.doOperation(); } @Override public void newOperation() { System.out.println("New space operation"); } } class DecoratorComma extends Decorator { public DecoratorComma(InterfaceComponent c) { super(c); } @Override public void doOperation() { System.out.print(","); super.doOperation(); } @Override public void newOperation() { System.out.println("New comma operation"); } } class DecoratorHello extends Decorator { public DecoratorHello(InterfaceComponent c) { super(c); } @Override public void doOperation() { System.out.print("Hello"); super.doOperation(); } @Override public void newOperation() { System.out.println("New hello operation"); } } class Main { public static void main (String... s) { Decorator c = new DecoratorHello(new DecoratorComma(new DecoratorSpace(new MainComponent()))); c.doOperation(); // Результат выполнения программы "Hello, World!" c.newOperation(); // New hello operation } } Пример на языке C# using System; namespace Decorator { class MainApp { static void Main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA dA = new ConcreteDecoratorA(); ConcreteDecoratorB dB = new ConcreteDecoratorB(); // Link decorators dA.SetComponent(c); dB.SetComponent(dA); dA.Operation(); Console.WriteLine(); dB.Operation(); // Wait for user Console.Read(); } } /// <summary> /// Component - компонент /// </summary> /// <remarks> /// <li> /// <lu>определяем интерфейс для объектов, на которые могут быть динамически /// возложены дополнительные обязанности;</lu> /// </li> /// </remarks> abstract class Component { public abstract void Operation(); } /// <summary> /// ConcreteComponent - конкретный компонент /// </summary> /// <remarks> /// <li> /// <lu>определяет объект, на который возлагается дополнительные обязанности</lu> /// </li> /// </remarks> class ConcreteComponent : Component { public override void Operation() { Console.Write("Привет"); } } /// <summary> /// Decorator - декоратор /// </summary> /// <remarks> /// <li> /// <lu>хранит ссылку на объект <see cref="Component"/> и определяет интерфейс, /// соответствующий интерфейсу <see cref="Component"/></lu> /// </li> /// </remarks> abstract class Decorator : Component { protected Component component; public void SetComponent(Component component) { this.component = component; } public override void Operation() { if (component != null) { component.Operation(); } } } /// <summary> /// ConcreteDecoratorA - конкретный декоратор /// </summary> /// <remarks> /// <li> /// <lu>Выполняет основную задачу</lu> /// </li> /// </remarks> class ConcreteDecoratorA : Decorator { public override void Operation() { base.Operation(); } } /// <summary> /// ConcreteDecorator - конкретный декоратор /// </summary> /// <remarks> /// <li> /// <lu>Выполняет основную задачу + дополнительную</lu> /// </li> /// </remarks> class ConcreteDecoratorB : Decorator { public override void Operation() { base.Operation(); Console.Write(" Мир!"); } } } Пример на языке С++ #include <iostream> #include <memory> class IComponent { public: virtual void operation() = 0; virtual ~IComponent(){} }; class Component : public IComponent { public: virtual void operation() { std::cout<<"World!"<<std::endl; } }; class DecoratorOne : public IComponent { std::shared_ptr<IComponent> m_component; public: DecoratorOne(IComponent* component): m_component(component) {} virtual void operation() { std::cout << ", "; m_component->operation(); } }; class DecoratorTwo : public IComponent { std::shared_ptr<IComponent> m_component; public: DecoratorTwo(IComponent* component): m_component(component) {} virtual void operation() { std::cout << "Hello"; m_component->operation(); } }; int main() { DecoratorTwo obj(new DecoratorOne(new Component())); obj.operation(); // prints "Hello, World!\n" return 0; } Пример на языке D import std.stdio; abstract class TFigure { protected: string name; public: string GetInfo(); } static class TEmpty: TFigure { public: string GetInfo() { return ""; }; } class TCircle: TFigure { protected: TFigure figure; public: this(TFigure theFigure) { figure = theFigure; name = " circle "; } string GetInfo() { return (name ~ figure.GetInfo()); } } class TBar: TFigure { protected: TFigure figure; public: this(TFigure theFigure) { figure = theFigure; name = " bar "; } string GetInfo() { return (figure.GetInfo() ~ name); } } void main() { TFigure figures = new TBar(new TCircle(new TBar(new TCircle(new TEmpty())))); writeln(figures.GetInfo()); } Пример на языке Python from abc import ABCMeta, abstractmethod class IOperator(object): """ Интерфейс, который должны реализовать как декоратор, так и оборачиваемый объект. """ __metaclass__ = ABCMeta @abstractmethod def operator(self): pass class Component(IOperator): """Компонент программы""" def operator(self): return 10.0 class Wrapper(IOperator): """Декоратор""" def __init__(self, obj): self.obj = obj def operator(self): return self.obj.operator() + 5.0 comp = Component() comp = Wrapper(comp) print comp.operator() # 15.0 Пример на языке PHP abstract class AbstractComponent { abstract public function operation(); } class ConcreteComponent extends AbstractComponent { public function operation() { // ... } } abstract class AbstractDecorator extends AbstractComponent { protected $component; public function __construct(AbstractComponent $component) { $this->component = $component; } } class ConcreteDecorator extends AbstractDecorator { public function operation() { // ... расширенная функциональность ... $this->component->operation(); // ... расширенная функциональность ... } } $decoratedComponent = new ConcreteDecorator( new ConcreteComponent() ); $decoratedComponent->operation(); Пример на языке PHP5, наиболее часто используемый <?php interface IText { public function show(); } class TextHello implements IText { protected $object; public function __construct(IText $text) { $this->object = $text; } public function show() { echo 'Hello'; $this->object->show(); } } class TextWorld implements IText { protected $object; public function __construct(IText $text) { $this->object = $text; } public function show() { echo 'world'; $this->object->show(); } } class TextSpace implements IText { protected $object; public function __construct(IText $text) { $this->object = $text; } public function show() { echo ' '; $this->object->show(); } } class TextEmpty implements IText { public function show() { } } $decorator = new TextHello(new TextSpace(new TextWorld(new TextEmpty()))); $decorator->show(); // Hello world echo '<br />' .