装饰者模式
来源:互联网 发布:淘宝护肤品店铺范文 编辑:程序博客网 时间:2024/06/06 10:56
/**
* 类的包装器
* 不改变接口,增加功能
* @author ruis
*
*/
//创建一个打印机接口
public interface Printer {
void print();
}
//纸张打印机
public class PaperPrinterimplements Printer {
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("Paper Printer");
}
}
//塑料打印机
public class PlasticPrinter implements Printer {
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("Plastic Printer");
}
}
//装饰者打印机
public abstract class PrinterDecoratorimplements Printer {
//本包中可以使用
protected PrinterdecoratedPrinter;
//形参为装饰者打印机
public PrinterDecorator(Printerd){
this.decoratedPrinter=d;
}
public void print(){
decoratedPrinter.print();
}
}
//3D打印机继承装饰者打印机功能
public class Printer3Dextends PrinterDecorator {
//给3D打印机增加的一个功能 是装饰者打印机
public Printer3D(PrinterdecoratedShape) {
super(decoratedShape);
// TODO Auto-generated constructor stub
}
//3D打印机 装饰者打印机
@Override
public void print(){
System.out.println("3D.");
decoratedPrinter.print();
}
}
/**
* 创建一个3D打印机,给3D打印机增加一个打印机功能
* @author ruis
*
*/
public class Main {
public static void main(String[]args) {
PlasticPrinter plasticPrinter = new PlasticPrinter();
Printer3D plastic3DPrinter =new Printer3D(new PlasticPrinter());
Printer3D paper3DPrinter =new Printer3D(new PaperPrinter());
plasticPrinter.print();
plastic3DPrinter.print();
paper3DPrinter.print();
}
/**
* Plastic Printer
3D. Plastic Printer
3D. Paper Printer
*/
}
- 装饰者模式(Derector)
- 装饰者模式
- Decorator 装饰者模式
- 装饰者设计模式
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式(Decorator)
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式 DecoratorPattern
- 设计模式 - 装饰者
- 装饰者模式
- 装饰者模式
- 装饰者模式 - 2
- 装饰者模式
- 装饰者模式
- Java_16 集合类接口的实现类
- Linux几个常用文件处理命令
- HDU 1021 Fibonacci Again
- 状态模式
- L1自适应控制-理论基础
- 装饰者模式
- DP入门 最佳加法表达式
- Springmvc @ResponseBody返回ajax数据时,报错406
- 【BZOJ】1087 [SCOI2005]互不侵犯King 状压DP(轮廓线DP)
- SpringMVC的406错误
- 了解Redis
- The Zen of Python
- Spring AOP面向切面编程
- hdu 6143 Killer Names 容斥||第二类斯特林数