关注码农话题
做一个实实在在的内行人

Java 装饰模式

装饰模式在不链接其结构的情况下向现有对象添加新功能。

它是一种结构型模式,因为它充当现有类的包装器。

装饰模式创建一个装饰器类来包装原始类并提供其他功能。

例子

interface Printer {
   void print();
}
class PaperPrinter implements Printer {
   @Override
   public void print() {
      System.out.println("Paper Printer");
   }
}
class PlasticPrinter implements Printer {
   @Override
   public void print() {
      System.out.println("Plastic Printer");
   }
}
abstract class PrinterDecorator implements Printer {
   protected Printer decoratedPrinter;
   public PrinterDecorator(Printer d){
      this.decoratedPrinter = d;
   }
   public void print(){
      decoratedPrinter.print();
   }  
}
class Printer3D extends PrinterDecorator {
   public Printer3D(Printer decoratedShape) {
      super(decoratedShape);    
   }
   @Override
   public void print() {
     System.out.println("3D.");
     decoratedPrinter.print();         
   }
}
public class Main {
   public static void main(String[] args) {
      Printer plasticPrinter = new PlasticPrinter();
      Printer plastic3DPrinter = new Printer3D(new PlasticPrinter());
      Printer paper3DPrinter = new Printer3D(new PaperPrinter());
      plasticPrinter.print();
      plastic3DPrinter.print();
      paper3DPrinter.print();
   }
}

上面的代码生成以下结果。


入职你的梦想 VS 变现你的技术

IT面试宝典码农市场