对设计模式—Decorator模式的认识
今天星期一,上班迟到了,走到办公室打开电脑就工作,抽空学习一下Decorator模式,电脑慢了啊,怎么办啊,内存才1G,电脑里的元器件都是做好的,又不能优化,只好给它加条内存.
public interface Work { public void start();}public class ComputerWork implements Work { public void start() { System.out.println("Computer change fast!"); }}//Add Memory function public class Decorator implements Work { private Work work; private List<String> memory = new ArrayList<String>(); public Decorator(Work work) { this.work = work; } public void addNewMemory(String newMemory) { memory.add(newMemory); } public void start() { otherMemory(); work.start(); } public void otherMemory() { ListIterator<String> listIterator = memory.listIterator(); while (listIterator.hasNext()) { System.out.println("Add Memory"+listIterator.next()); } }public class TestDecorator {@SuppressWarnings("unused")public static void main(String[]args){Work computer= new ComputerWork ();Decorator decorator = new Decorator(computer);decorator.addNewMemory("add 1 G Memory");decorator.start();}}