zoukankan      html  css  js  c++  java
  • 【设计模式】9、装饰器模式

     1 package com.shejimoshi.structural.Decorator;
     2 
     3 
     4 /**
     5  * 功能:这个是我们装饰器的基类,用来生成被装饰类和装饰器类
     6  * 时间:2016年2月25日上午10:05:37
     7  * 作者:cutter_point
     8  */
     9 public abstract class Component
    10 {
    11     //这个方法就是我们装饰器要进行装饰的操作
    12     public abstract void tuo();
    13 }
     1 package com.shejimoshi.structural.Decorator;
     2 
     3 
     4 /**
     5  * 功能:动态地给一个对象添加一些额外的职责。就增加功能来说装饰器模式比生成子类更加灵活
     6  *         装饰器就是用装饰器类来对我们的私有成员(装饰对象)进行相应的扩展功能
     7  * 适用:在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责
     8  *         处理那些可以撤销的职责
     9  * 时间:2016年2月25日上午9:45:43
    10  * 作者:cutter_point
    11  */
    12 public class Person extends Component
    13 {
    14     private String name;
    15     
    16     public Person(String name)
    17     {
    18         this.name = name;
    19     }
    20 
    21     @Override
    22     public void tuo()
    23     {
    24         System.out.print(this.name + "准备上床睡觉	");
    25     }
    26 }
     1 package com.shejimoshi.structural.Decorator;
     2 
     3 
     4 
     5 /**
     6  * 功能:装饰器基类,用来对相应的对象进行装饰
     7  * 时间:2016年2月25日上午10:15:02
     8  * 作者:cutter_point
     9  */
    10 public abstract class Decorator extends Component
    11 {
    12     protected Component component;
    13 
    14     public Decorator(Component component)
    15     {
    16         this.component = component;
    17     }
    18     
    19     @Override
    20     public void tuo()
    21     {
    22         if(component != null)
    23             component.tuo();
    24     }
    25 
    26 }
     1 package com.shejimoshi.structural.Decorator;
     2 
     3 
     4 /**
     5  * 功能:对于一个操作,上衣
     6  * 时间:2016年2月25日上午10:18:28
     7  * 作者:cutter_point
     8  */
     9 public class Shangyi extends Decorator
    10 {
    11     //构造函数
    12     public Shangyi(Component component)
    13     {
    14         super(component);
    15     }
    16     
    17     @Override
    18     public void tuo()
    19     {
    20         super.tuo();
    21         System.out.print("脱上衣	");
    22     }
    23 }
     1 package com.shejimoshi.structural.Decorator;
     2 
     3 
     4 /**
     5  * 功能:对应裤子操作
     6  * 时间:2016年2月25日上午10:49:03
     7  * 作者:cutter_point
     8  */
     9 public class Kuzhi extends Decorator
    10 {
    11 
    12     public Kuzhi(Component component)
    13     {
    14         super(component);
    15     }
    16 
    17     @Override
    18     public void tuo()
    19     {
    20         super.tuo();
    21         System.out.print("脱掉裤子	");
    22     }
    23 
    24 }
     1 package com.shejimoshi.structural.Decorator;
     2 
     3 
     4 /**
     5  * 功能:装饰器模式
     6  * 时间:2016年2月25日上午11:13:55
     7  * 作者:cutter_point
     8  */
     9 public class Test
    10 {
    11     public static void main(String[] args)
    12     {
    13         Component person = new Person("cutter_point");
    14         Decorator shangyi = new Shangyi(person);
    15         shangyi.tuo();
    16         System.out.println();
    17         Decorator kuzhi = new Kuzhi(person);
    18         kuzhi.tuo();
    19     }
    20 }

    测试结果:

    cutter_point准备上床睡觉	脱上衣	
    cutter_point准备上床睡觉	脱掉裤子	
    

      

  • 相关阅读:
    解决Firefox下outerHTML不支持问题
    神奇的css属性pointerevents
    IE6 double marginleft Bug
    解决IE低版本不支持call和apply问题
    JavaScript函数参数的可修改性
    IE6/7 double paddingbottom Bug
    各浏览器对document.getElementById等方法的实现差异
    JavaScript中两种类型的全局对象/函数
    JavaScript子类用Object.getPrototypeOf去调用父类方法
    JavaScript声明全局变量三种方式的异同
  • 原文地址:https://www.cnblogs.com/cutter-point/p/5216410.html
Copyright © 2011-2022 走看看