设计模式——装饰者模式
View Code
package com.hujl.demo; /** * 创建一个电工类 * @author Janrone * @website http://hujl.sinaapp.com */ public class Wireman implements Worker{ @Override public void doSometing() { System.out.println("我是电工,负责维修电路!"); } }
<span style="font-family:Arial, Verdana, sans-serif;"><span style="white-space: normal;"> </span></span>
package com.hujl.demo; /** * 创建一个A物业公司 * @author Janrone * @website http://hujl.sinaapp.com */ public class Aproperty implements Worker { private Worker worker; public Aproperty(Worker worker){ this.worker=worker; } @Override public void doSometing() { // TODO Auto-generated method stub System.out.println("您好,A物业公司."); worker.doSometing(); } }
package com.hujl.demo; /** * 创建一个B物业公司 * @author Janrone * @website http://hujl.sinaapp.com */ public class Bproperty implements Worker { private Worker worker; public Bproperty(Worker worker) { this.worker = worker; } @Override public void doSometing() { // TODO Auto-generated method stub System.out.println("您好,B物业公司."); worker.doSometing(); } }
public class Test { /** * @param args */ public static void main(String[] args) { //生成一个电工对象 Wireman worker=new Wireman(); //生成A物业公司对象 Aproperty aproperty=new Aproperty(worker); aproperty.doSometing(); ////生成B物业公司对象 Bproperty bproperty=new Bproperty(worker); bproperty.doSometing(); } }