设计模式9 - 装饰器模式Decorator
2012-12-05 16:31
337 查看
To extend or modify the behaviour of ‘an instance’ at runtime decoratordesign pattern is used. Inheritance is used to extend the abilities of ‘a class’. Unlike inheritance, you can choose any single object
of a class and modify its behaviour leaving the other instances unmodified.
In implementing the decorator pattern you construct a wrapper around an object by extending its behavior. The wrapper will do its job before or after and delegate the call to the wrapped instance.
You start with an interface which creates a blue print for the class which will have decorators. Then implement that interface with basic functionalities. Till now we have got an interface and an implementation
concrete class. Create an abstract class that contains (aggregation relationship) an attribute type of the interface. The constructor of this class assigns the interface type instance to that attribute. This class
is the decorator base class. Now you can extend this class and create as many concrete decorator classes. The concrete decorator class will add its own methods. After / before executing its own method the concrete decorator will call the base instance’s method.
Key to this decorator design pattern is the binding of method and the base instance happens at runtime based on the object passed as parameter to the constructor. Thus dynamically customizing the behavior of that specific instance alone.
Following given example is an implementation of decorator design pattern. Icecream is a classic example for decorator design pattern. You create a basic icecream and then add toppings to it as you prefer. The added toppings change
the taste of the basic icecream. You can add as many topping as you want. This sample scenario is implemented below.
The above is an interface depicting an icecream. I have kept things as simple as possible so that the focus will be on understanding the design
pattern. Following class is a concrete implementation of this interface. This is the base class on which the decorators will be added.
Following class is the decorator class. It is the core of the decorator design pattern. It contains an attribute for the type of interface. Instance
is assigned dynamically at the creation of decorator using its constructor. Once assigned that instance method will be invoked.
Following two classes are similar. These are two decorators, concrete class implementing the abstract decorator. When the decorator is created
the base instance is passed using the constructor and is assigned to the super class. In the makeIcecream method we call the base method followed by its own method addNuts(). This addNuts() extends the behavior by adding its own steps.
I have created a simple icecream and decorated that with nuts and on top of it with honey. We can use as many decorators in any order we want. This excellent flexibility and changing the behaviour of an instance of our choice at
runtime is the main advantage of the decorator design pattern.
of a class and modify its behaviour leaving the other instances unmodified.
In implementing the decorator pattern you construct a wrapper around an object by extending its behavior. The wrapper will do its job before or after and delegate the call to the wrapped instance.
Design of decorator pattern
You start with an interface which creates a blue print for the class which will have decorators. Then implement that interface with basic functionalities. Till now we have got an interface and an implementationconcrete class. Create an abstract class that contains (aggregation relationship) an attribute type of the interface. The constructor of this class assigns the interface type instance to that attribute. This class
is the decorator base class. Now you can extend this class and create as many concrete decorator classes. The concrete decorator class will add its own methods. After / before executing its own method the concrete decorator will call the base instance’s method.
Key to this decorator design pattern is the binding of method and the base instance happens at runtime based on the object passed as parameter to the constructor. Thus dynamically customizing the behavior of that specific instance alone.
Decorator Design Pattern – UML Diagram
Implementation of decorator pattern
Following given example is an implementation of decorator design pattern. Icecream is a classic example for decorator design pattern. You create a basic icecream and then add toppings to it as you prefer. The added toppings changethe taste of the basic icecream. You can add as many topping as you want. This sample scenario is implemented below.
public interface Icecream { public String makeIcecream(); }
The above is an interface depicting an icecream. I have kept things as simple as possible so that the focus will be on understanding the design
pattern. Following class is a concrete implementation of this interface. This is the base class on which the decorators will be added.
public class SimpleIcecream implements Icecream { public String makeIcecream() { return "Base Icecream"; } }
Following class is the decorator class. It is the core of the decorator design pattern. It contains an attribute for the type of interface. Instance
is assigned dynamically at the creation of decorator using its constructor. Once assigned that instance method will be invoked.
public abstract class IcecreamDecorator implements Icecream { protected Icecream specialIcecream; public IcecreamDecorator(Icecream specialIcecream) { this.specialIcecream = specialIcecream; } public String makeIcecream() { return specialIcecream.makeIcecream(); } }
Following two classes are similar. These are two decorators, concrete class implementing the abstract decorator. When the decorator is created
the base instance is passed using the constructor and is assigned to the super class. In the makeIcecream method we call the base method followed by its own method addNuts(). This addNuts() extends the behavior by adding its own steps.
public class NuttyDecorator extends IcecreamDecorator { public NuttyDecorator(Icecream specialIcecream) { super(specialIcecream); } public String makeIcecream() { return specialIcecream.makeIcecream() + addNuts(); } private String addNuts() { return " + cruncy nuts"; } }
public class HoneyDecorator extends IcecreamDecorator { public HoneyDecorator(Icecream specialIcecream) { super(specialIcecream); } public String makeIcecream() { return specialIcecream.makeIcecream() + addHoney(); } private String addHoney() { return " + sweet honey"; } }
Execution of the decorator pattern
I have created a simple icecream and decorated that with nuts and on top of it with honey. We can use as many decorators in any order we want. This excellent flexibility and changing the behaviour of an instance of our choice atruntime is the main advantage of the decorator design pattern.
public class TestDecorator { public static void main(String args[]) { Icecream icecream = new HoneyDecorator(new NuttyDecorator(new SimpleIcecream())); System.out.println(icecream.makeIcecream()); } }
Output
相关文章推荐
- 装饰设计模式(decorator design pattern)
- 设计模式 (十)装饰模式(Decorator)
- java.io.*中的设计模式之 装饰(Decorator)模式
- 设计模式之---装饰(decorator)
- 设计模式09: Decorator 装饰模式(结构型模式)
- Java设计模式之再从[暗黑破坏神"装备镶嵌宝石系统"]分析装饰(Decorator)模式
- 设计模式8:Decorator Pattern (装饰模式)
- 初学设计模式【3】装饰模式——Decorator
- Java经典设计模式-结构型模式-装饰模式(Decorator)
- 设计模式(11)-装饰模式(Decorator)
- 研磨设计模式 之 装饰模式(Decorator)2??跟着cc学设计系列
- 设计模式之装饰模式(Decorator)摘录
- 设计模式之装饰模式,decorator
- Decorator(装饰)设计模式
- 23种设计模式-7.装饰模式(Decorator Pattern)
- 设计模式之decorator装饰模式
- java设计模式:装饰器模式[Decorator]
- DOTA版设计模式——装饰模式[Decorator Pattern]
- 设计模式--装饰模式(Decorator)
- 结构型设计模式---Decorator模式(装饰模式)