Decorator

Intent

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Class diagram of the original GoF pattern

structure-GoF:decorator

Improvement in MixJuice

Solved Problem(s)

Extensibility problem
It is impossible to add new method to Component and Decorator without modifying the source code. (This problem is mentioned at p.179,line.3-5 in the GoF book.)

Used programming technique(s)

Method addition or abstract method addition to Component and Decorator.

Consequences

Structure and pseudo code

structure:decorator_sample1

module original {
  define abstract class Component {
    define abstract void operation();
  }
  define class ConcreteComponent extends Component {
    void operation(){...}
  }
  define abstract class Decorator extends Component {
    Component component;
    void operation(){ component.operation(); }
  }
  define class ConcreteDecorator extends Decorator {
    void operation(){...}
  }
}
module extension extends original {
  class Component {
    define abstract void newOperation();
  }
  class ConcreteComponent {
    void newOperation(){...}
  }
  class Decorator {
    void newOperation(){ component.newOperation(); }
  }
}

Another solution in MixJuice

Solved Problem(s)

Extensibility problem
It is impossible to add new method to Component and Decorator without modifying the source code. (This problem is mentioned at p.179,line.3-5 in the GoF book.)
Complexity problem
A decorator and its component aren't identical. (This problem is mentioned at p.178,line.29-32 in the GoF book.) Lots of little objects. (This problem is mentioned at p.178,line.33-37 in the GoF book.)

Used programming technique(s)

Class extension for Component.

Consequences

Structure and pseudo code

structure:decorator_sample2

module original {
  define class Component {
    define void operation();
  }
}
module extension1 extends original {
  class Component {
    void operation(){...}
  }
}
module extension2 extends original {
  class Component {
    void operation(){...}
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top