Java设计模式(7)—— 装饰器
2009-08-21 18:13
726 查看
Decorator
英文简单描述
Intent
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality.
How to
The decorator conforms to the interface of the component it decorates
so that its presence is transparent to the component's clients.
Component
defines the interface for objects that can have responsibilities added to them dynamically.
ConcreteComponent
defines an object to which additional responsibilities can be attached.
Decorator
maintains a reference to a Component object and defines an interface that conforms to Component's interface.
ConcreteDecorator
adds responsibilities to the component.
Known cases
Java IO Stream
GUI
UML
好处:
(1)比静态的继承更灵活 [More flexibility than static inheritance]
(2)保持组件轻量级 [Keeping Component classes lightweight]
(3)和策略区别:
A decorator lets you change the skin of an object; a strategy lets you change the guts. These are two alternative ways of changing an object.
代码:
英文简单描述
Intent
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality.
How to
The decorator conforms to the interface of the component it decorates
so that its presence is transparent to the component's clients.
Component
defines the interface for objects that can have responsibilities added to them dynamically.
ConcreteComponent
defines an object to which additional responsibilities can be attached.
Decorator
maintains a reference to a Component object and defines an interface that conforms to Component's interface.
ConcreteDecorator
adds responsibilities to the component.
Known cases
Java IO Stream
GUI
UML
好处:
(1)比静态的继承更灵活 [More flexibility than static inheritance]
(2)保持组件轻量级 [Keeping Component classes lightweight]
(3)和策略区别:
A decorator lets you change the skin of an object; a strategy lets you change the guts. These are two alternative ways of changing an object.
代码:
public interface ILogger { void logger(String info); } public class ConsoleLogger implements ILogger { public void logger(String info) { System.out.println(info); } } public class FileLogger implements ILogger { private PrintWriter writer; public FileLogger(String path) { File file = new File(path); FileOutputStream output; try { output = new FileOutputStream(file); writer = new PrintWriter(output); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void logger(String info) { if (writer == null) { System.out.println("FileLogger init failure"); } else { writer.write(info); writer.flush(); } } } public abstract class DecoratorLogger implements ILogger { protected ILogger delegate; public DecoratorLogger(ILogger delegate) { this.delegate = delegate; } abstract public void logger(String info); } public class EncryptLogger extends DecoratorLogger { public EncryptLogger(ILogger delegate) { super(delegate); } public void logger(String info) { String temp = encrypt(info); delegate.logger(temp); } private String encrypt(String info) { // encrypt algorithm // ... return info; } }
相关文章推荐
- java设计模式之 装饰器模式
- Java设计模式——装饰器模式、适配器模式、外观模式
- Java设计模式——装饰器模式(Decorator)
- Java设计模式透析之 —— 装饰器模式(Decorator)
- Java设计模式概述之结构型模式(装饰器模式)
- Java设计模式(四) 装饰器模式 代理器模式
- Java设计模式(9) 装饰器
- 浅析Java IO 设计模式之—装饰器模式
- java学习笔记-设计模式8(装饰器模式)
- JAVA IO 设计模式彻底分析
- 设计模式(三)装饰者模式之java.io
- Java设计模式-读《head first 设计模式》
- Java23种设计模式——备忘录模式
- Java设计模式(三) 抽象工厂模式
- java设计模式大全
- java设计模式速记
- Java设计模式:桥接模式
- Java设计模式圣经连载(05)-代理模式
- java设计模式
- java 23种设计模式【摘录】