本文介绍了如何从Java中的Decorator Pattern中删除装饰对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在阅读傻瓜的设计模式。我阅读并练习了Decorator Pattern。使用Decorator Pattern,我们可以用任何东西来装饰一个对象。现在,我想在装饰之前删除装饰的对象。我已经通过一个ArrayList解决了这个问题,但我仍然觉得不好。你能告诉我如何去除装饰对象?什么是更好的方法? 这是我的方式: Computer.java public class Computer { public Computer(){} public String description(){ returncomputer; } } ComponentDecorator.java public abstract class ComponentDecorator extends Computer { @Override public abstract String description() ; } CD.java public class CD extends ComponentDecorator { private Computer computer; public CD(){} 公共CD(电脑){ this.computer =电脑; } @Override public String description(){ return computer.description()+and a CD; } } Disk.java public class Disk extends ComponentDecorator { private Computer computer; public Disk(){} public Disk(Computer c){ computer = c; } @Override public String description(){ return computer.description()+and a disk; } } Monitor.java public class Monitor extends ComponentDecorator { private Computer computer; public Monitor(){} public Monitor(Computer computer){ this.computer = computer; } @Override public String description(){ return computer.description()+and a monitor; } } Main.java import java.util.ArrayList; import java.util.Arrays; public class Main { static ArrayList< ComponentDecorator> list = new ArrayList<>(); public static void main(String [] args){ addComponent(new CD(),new Disk(),new Monitor()); System.out.println(list.size()); 电脑penIII = getComputer(); removeComponent(new Monitor()); penIII = getComputer(); System.out.println(penIII.description()); } private static void addComponent(ComponentDecorator ... comp){ list.addAll(Arrays.asList(comp)); } private static void removeComponent(ComponentDecorator comp){ for(ComponentDecorator c:list){ if(c.getClass()== comp.getClass ()){ list.remove(list.indexOf(c)); break; } } } 私有静态计算机getComputer(){计算机c = new Computer(); 类e; (ComponentDecorator d:list) { e = d.getClass(); try { c =(Computer)e.getConstructor(new Class [] {Computer.class})。newInstance(c); } catch(Exception ex){ ex.printStackTrace(); } } return c; } } 解决方案更好的方法是将removeDecorator方法添加到您的ComponentDecorator类中。 public abstract class ComponentDecorator { private ComponentDecorator subject; public ComponentDecorator(ComponentDecorator subject){ this.subject = subject; } @Override public abstract String description(); } public void removeDecorator(ComponentDecorator toRemove){ if(subject == null){ return; }其他if(subject.equals(toRemove)){ subject = subject.getSubject(); } else { subject.removeDecorator(toRemove); } } public ComponentDecorator getSubject(){ return subject; } //计算机 public class计算机扩展ComponentDecorator { public Computer(){ super(null ); } public String description(){ returncomputer; } // CD public class CD扩展ComponentDecorator { public CD(ComponentDecorator computer){ super(computer); } @Override public String description(){ return getSubject()。description()+和CD; } } // main public static void main(String [] args){ ComponentDecorator penIII = new Computer(); penIII =新CD(penIII); penIII =新显示器(penIII); System.out.println(penIII.description()); } } 如果你没有装饰器的引用来删除,你可以创建一个类别的另一种方法。 你需要将装饰对象作为 ComponentDecorator而不是Computer。我建议让Computer类扩展ComponentDecorator而不是其他方式。 I'm reading "Design Pattern for Dummies". I read and practiced Decorator Pattern. With Decorator Pattern, we can decorate an object with anything. Now, I want to remove decorated object before decorated.I have solved this problem by an ArrayList but I still feel it's not good. Can you tell me how to remove a decorated object? And what is a better way?this is my way:Computer.javapublic class Computer { public Computer() { } public String description() { return "computer"; }}ComponentDecorator.javapublic abstract class ComponentDecorator extends Computer { @Override public abstract String description();}CD.javapublic class CD extends ComponentDecorator { private Computer computer; public CD() { } public CD(Computer computer) { this.computer = computer; } @Override public String description() { return computer.description() + " and a CD"; }}Disk.javapublic class Disk extends ComponentDecorator { private Computer computer; public Disk() { } public Disk(Computer c) { computer = c; } @Override public String description() { return computer.description() + " and a disk"; }}Monitor.javapublic class Monitor extends ComponentDecorator { private Computer computer; public Monitor() { } public Monitor(Computer computer) { this.computer = computer; } @Override public String description() { return computer.description() + " and a monitor"; }}Main.javaimport java.util.ArrayList;import java.util.Arrays;public class Main { static ArrayList<ComponentDecorator> list = new ArrayList<>(); public static void main(String[] args) { addComponent(new CD(), new Disk(), new Monitor()); System.out.println(list.size()); Computer penIII = getComputer(); removeComponent(new Monitor()); penIII = getComputer(); System.out.println(penIII.description()); } private static void addComponent(ComponentDecorator... comp) { list.addAll(Arrays.asList(comp)); } private static void removeComponent(ComponentDecorator comp) { for(ComponentDecorator c : list) { if(c.getClass() == comp.getClass()) { list.remove(list.indexOf(c)); break; } } } private static Computer getComputer() { Computer c = new Computer(); Class e; for(ComponentDecorator d : list) { e = d.getClass(); try { c = (Computer) e.getConstructor(new Class[]{Computer.class}).newInstance(c); } catch(Exception ex) { ex.printStackTrace(); } } return c; }} 解决方案 A nicer way would be adding the "removeDecorator" method to your ComponentDecorator class.public abstract class ComponentDecorator {private ComponentDecorator subject;public ComponentDecorator(ComponentDecorator subject) { this.subject = subject;}@Overridepublic abstract String description();}public void removeDecorator(ComponentDecorator toRemove) { if (subject == null) { return; } else if (subject.equals(toRemove)) { subject = subject.getSubject(); } else { subject.removeDecorator(toRemove); }}public ComponentDecorator getSubject() { return subject;}// Computerpublic class Computer extends ComponentDecorator{public Computer() { super(null);}public String description() { return "computer";}// CDpublic class CD extends ComponentDecorator { public CD(ComponentDecorator computer) { super(computer); } @Override public String description() { return getSubject().description() + " and a CD"; }}// mainpublic static void main(String[] args) { ComponentDecorator penIII = new Computer(); penIII = new CD(penIII); penIII = new Monitor(penIII); System.out.println(penIII.description());}}If you don't have the reference of the decorator to remove, you can create another method that the a Class instead.You'll need to the decorated object as "ComponentDecorator" instead of "Computer" however. I suggest to make the Computer class extends ComponentDecorator instead of the other way around. 这篇关于如何从Java中的Decorator Pattern中删除装饰对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-31 03:00