使用装饰模式来看接口
来源:互联网 发布:appstore美区软件 编辑:程序博客网 时间:2024/04/30 03:20
装饰模式(Decorator)
意图
装饰模式
适用性
- 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
- 处理那些可以撤消的职责。
- 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
- 使用装饰模式我们就可以看到调用接口的地方实现了接口,
- 实现接口的地方实现了接口的方法,然后在调用接口的地方接收到实现接口的对象然后在调用接口的类中可以直接实现接口中的方法(接口中的放法)。
- 抽象构件角色java 代码
- package decorator;
- /**
- * 装饰者和原组建的共同方法接口(抽象构件角色)
- * @author mouca.he
- *
- */
- public interface InterfaceComponent {
- /**
- * 组件方法 say()
- *
- */
- public void say();
- }
具体构件角色java 代码
- package decorator;
- /**
- * 原组件(具体构件角色)
- * @author mouca.he
- *
- */
- public class Component implements InterfaceComponent{
- public void say() {
- // TODO 自动生成方法存根
- System.out.println("Component.say():原组件的方法!");
- }
- }
抽象装饰者角色java 代码
- package decorator;
- /**
- * 抽象装饰者
- * @author mouca.he
- *
- */
- public abstract class AbstractDecorator implements InterfaceComponent{
- private InterfaceComponent component;
- public AbstractDecorator(InterfaceComponent component){
- this.component = component;
- }
- /**
- * 组件方法执行前预处理方法
- *
- */
- protected void preSay(){};
- /**
- * 组件方法执行后处理方法
- *
- */
- protected void afterSay(){};
- public void say(){
- preSay();
- component.say();
- afterSay();
- };
- }
具体装饰者二java 代码
- package decorator;
- /**
- * 装饰者二
- * @author mouca.he
- *
- */
- public class DecoratorTwo extends AbstractDecorator{
- public DecoratorTwo(InterfaceComponent component) {
- super(component);
- // TODO 自动生成构造函数存根
- }
- /**
- * 根据需要重载模板类preSay()方法
- */
- protected void preSay(){
- System.out.println("DecoratorTwo.preSay():装饰者二的preSay()方法!");
- }
- /**
- * 根据需要重载模板类afterSay()方法
- */
- protected void afterSay(){
- System.out.println("DecoratorTwo.afterSay():装饰者二的afterSay()方法!");
- }
- }
装饰者一java 代码
- package decorator;
- /**
- * 装饰者一
- * @author mouca.he
- *
- */
- public class DecoratorOne extends AbstractDecorator{
- public DecoratorOne(InterfaceComponent component) {
- super(component);
- // TODO 自动生成构造函数存根
- }
- /**
- * 根据需要重载模板类preSay()方法
- */
- protected void preSay(){
- System.out.println("DecoratorOne.preSay():装饰者一的preSay()方法!");
- }
- /**
- * 根据需要重载模板类afterSay()方法
- */
- protected void afterSay(){
- System.out.println("DecoratorOne.afterSay():装饰者一的afterSay()方法!");
- }
- /**
- * 测试方法
- * @param args
- */
- public static void main(String[] args) {
- // TODO 自动生成方法存根
- InterfaceComponent interfaceComponent = new DecoratorTwo(new DecoratorOne(new Component()));
- interfaceComponent.say();
- /*
- * 控制台输出:
- * DecoratorTwo.preSay():装饰者二的preSay()方法!
- * DecoratorOne.preSay():装饰者一的preSay()方法!
- * Component.say():原组件的方法!
- * DecoratorOne.afterSay():装饰者一的afterSay()方法!
- * DecoratorTwo.afterSay():装饰者二的afterSay()方法!
- */
- }
- }
4、优缺点
优点:1)提供比继承更多的灵活性 2)使用不同的装饰组合可以创造出不同行为的组合 3)需要的类的数目减少
缺点:1)灵活性带来比较大的出错性 2)产生更多的对象,给查错带来困难
0 0
- 使用装饰模式来看接口
- 从我出门来看装饰模式+vb.net实现
- 从我出门来看装饰模式+vb.net实现
- 使用装饰器模式
- SiteMesh使用示例 -装饰模式
- 以一个装饰模式来看IBM RSA与Together2006(附UML图)
- 第二十讲 Randomaccessfile使用、Properties工具类使用、文件压缩例子、装饰者模式、Path接口与Files工具类
- 装饰者设计模式的使用
- 使用装饰设计模式加密数据
- 装饰模式-撤销装饰
- 《GOF设计模式》—装饰(DECORATOR)—Delphi源码示例:装饰接口
- 来看看单例模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 《网蜂A8实战演练》——7.Linux LCD设备驱动
- [字典树、map] HDU 1800 - Flying to the Mars
- 实验4(统计文本单词个数)
- hdu5105(求一元三次方程的最值)
- 深入Redis内部-Redis 源码讲解
- 使用装饰模式来看接口
- fgbfbgbvnvbn
- 解决无法删除的dll文件
- 新博客开始
- Two Sum
- 实验4(幻方)
- qt4.X编译qtcreator2.8
- Escape(匈牙利算法变形)
- Effective JavaScript Item 63 注意异步调用中可能会被忽略的异常