Java装饰者模式简介

一、假设有一个Worker接口,它有一个doSomething方法,Plumber和Carpenter都实现了Worker接口,代码及关系如下:

Java-马士兵设计模式学习笔记-装饰者模式-LMLPHP

1.Worker.java
package decorator; public interface Worker {
public void doSomething();
}
2.Plumber.java

public class Plumber implements Worker {

    @Override
public void doSomething() {
System.out.println("修理水管");
} }
3.Carpenter

public class Carpenter implements Worker{
public void doSomething(){
System.out.println("修门窗");
}
}
4.测试代码 Test.java

public class Test {
public static void main(String[] args) {
Worker worker = null;
Carpenter carpenter = new Carpenter();
Plumber plumber = new Plumber();
worker = carpenter;
worker.doSomething();
worker = plumber;
worker.doSomething();
}
}

测试结果如下:

Java-马士兵设计模式学习笔记-装饰者模式-LMLPHP

二、现有A、B两家公司,都有木工(Carpenter)和水管工(Plumber),但两公司的对两个工种的要求都不同,A公司要求员工在doSomething前自己介绍“我是A公司的”;B公司要求员工在doSomething前自己介绍“我是B公司的”;一个直接但不科学的设计方法是为每个公司都设计各自的木工(Carpenter)和水管工(Plumber),若公司增加到100个,则类要增加到200个,若再增加其他工种,则类会更多,关系如下:

Java-马士兵设计模式学习笔记-装饰者模式-LMLPHP

三、此情况适合采用装饰者模式,为每个公司设计一个装饰类xWorker,xWorker也实现Worker接口,且有一个参数类型为Worker的构造函数和一个类型为Worker的成员变量,这样xWorker就可以取得相应类型worker的引用,在xWorker的doSomething()方法里处理各自公司的特殊要求:

Java-马士兵设计模式学习笔记-装饰者模式-LMLPHP

 增加类 AWorker.java

 public class AWorker implements Worker {

     private Worker worker;

     //通过些构造函数,AWorker就可以得到具体工种的引用,
//为下面的doSomething()方法中加入A公司的特殊要求做准备
public AWorker(Worker worker){
this.worker = worker;
} @Override
public void doSomething() {
System.out.println("您好!我是A公司的员工");
worker.doSomething();
} }
 //增加类 BWorker.java

 public class BWorker implements Worker {

     private Worker worker;

     public BWorker(Worker worker){
this.worker = worker;
} @Override
public void doSomething() {
System.out.println("您好!我是B公司的员工。");
worker.doSomething();
} }
 //测试代码

 public static void main(String[] args) {

         //A公司
Carpenter carpenterA = new Carpenter();
Plumber plumberA = new Plumber();
AWorker aWorker1 = new AWorker(carpenterA);
AWorker aWorker2 = new AWorker(plumberA); aWorker1.doSomething();
aWorker2.doSomething(); System.out.println("=================================="); //B公司
Carpenter carpenterB = new Carpenter();
Plumber plumberB = new Plumber();
BWorker bWorker1 = new BWorker(carpenterB);
BWorker bWorker2 = new BWorker(plumberB); bWorker1.doSomething();
bWorker2.doSomething();
}
}

测试结果:

Java-马士兵设计模式学习笔记-装饰者模式-LMLPHP

05-11 11:07