结构模式之装饰器模式
程序员文章站
2022-05-04 19:24:09
...
package com.eric.design.decorator; //单向适配器模式就是在主模块上接入另外一个模块,用继承法 //装饰器模式特点:主模块注入到扩展模块中 interface IRead{ public void read(); } class ReadStrFromFile implements IRead{//主模块 public void read(){ System.out.println("ReadStrFromFile.read()"); } } class ReadStrFromNet implements IRead{//主模块 public void read(){ System.out.println("ReadStrFromNet.read()"); } } class Convert{//扩展模块 private IRead iRead; public void setIRead(IRead iRead){ this.iRead = iRead; } public void convert(){ iRead.read(); } } public class Decorator { public static void main(String[] args) { // TODO Auto-generated method stub Convert convert = new Convert(); //convert.setIRead(new ReadStrFromFile()); convert.setIRead(new ReadStrFromNet()); convert.convert(); } }
上一篇: 死锁如何定位,修复?及其预防?
下一篇: [3]设计模式——装饰器模式