java:装饰者模式,节点流和处理流
接口:
interface Worker { public void dowork(); }
清洁工:被装饰者,节点流
class Clean implements Worker { public void dowork(){ System.out.println("搞清洁"); } }
A公司:装饰者,处理流
class Aworker implements Worker { private Worker worker; //传进来的对象是woker的实现类 public Aworker(Worker worker){ this.worker = worker; } public void dowork(){ System.out.println("A公司"); worker.dowork(); } }
实例化:
class Test { public static void main(String args[]){ Clean clean = new Clean(); Aworker worker1 = new Aworker(clean); worker1.dowork(); } }
2024还活着,挺好的,向着星辰与深渊,加油,博客园不要倒下啊!