gpt4 book ai didi

java - 如何从 Java 中的装饰器模式中删除装饰对象

转载 作者:搜寻专家 更新时间:2023-10-31 08:28:02 29 4
gpt4 key购买 nike

我正在阅读“傻瓜设计模式”。我阅读并实践了装饰者模式。使用装饰器模式,我们可以用任何东西装饰一个对象。现在,我想在装饰之前删除装饰对象。我已经通过 ArrayList 解决了这个问题,但我仍然觉得它不好。你能告诉我如何移除装饰过的物体吗?什么是更好的方法?

这是我的方式:

计算机.java

public class Computer {

public Computer() {
}

public String description() {
return "computer";
}

}

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() {
}

public CD(Computer computer) {
this.computer = computer;
}

@Override
public String description() {
return computer.description() + " and a CD";
}

}

磁盘.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";
}

}

主.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());
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;
}
}

最佳答案

更好的方法是将“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;
} else if (subject.equals(toRemove)) {
subject = subject.getSubject();
} else {
subject.removeDecorator(toRemove);
}
}

public ComponentDecorator getSubject() {
return subject;
}


// Computer
public class Computer extends ComponentDecorator{

public Computer() {
super(null);
}

public String description() {
return "computer";
}

// CD
public class CD extends ComponentDecorator {

public CD(ComponentDecorator computer) {
super(computer);
}

@Override
public String description() {
return getSubject().description() + " and a CD";
}
}

// main
public static void main(String[] args) {
ComponentDecorator penIII = new Computer();
penIII = new CD(penIII);
penIII = new Monitor(penIII);
System.out.println(penIII.description());
}

如果您没有要删除的装饰器的引用,您可以创建另一个类的方法。

但是,您需要将装饰对象设置为“ComponentDecorator”而不是“Computer”。我建议让 Computer 类扩展 ComponentDecorator,而不是相反。

关于java - 如何从 Java 中的装饰器模式中删除装饰对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12239784/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com