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

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
- (+) New methods can be added to Component and Decorator without modifying the source code.
- (-) Addition of both a new method and a new ConcreteDecorator requires a complementary module; however, if default behavior of the method can be implemented in Decorator, e.g. just forward the operation to the next decorator, no complementary module is required.
Structure and pseudo code

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
- (+) Object identity problem no longer occurs.
- (+) Class structure is not complicated.
- (+) New methods can be added to Component without modifying the source code.
- (-) It is impossible to attach additional responsibilities to an object dynamically.
- (-) This solution is applicable only if just one behavior of Component is used in a program.
Structure and pseudo code

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