Java 装饰图案的问题
我试图学习装饰图案,但我有问题 首先我有一个界面Java 装饰图案的问题,java,design-patterns,Java,Design Patterns,我试图学习装饰图案,但我有问题 首先我有一个界面 public interface MyCar { public String getMessage(int speed); public int getPrice(); } 我这样实现了这个类 public class Car implements MyCar{ protected int price; protected boolean feature1; protected boolean featu
public interface MyCar {
public String getMessage(int speed);
public int getPrice();
}
我这样实现了这个类
public class Car implements MyCar{
protected int price;
protected boolean feature1;
protected boolean feature2;
public Car(){
this.price = 0;
this.feature1 = false;
this.feature2 = false;
}
publicCar(int price){
this.price = price;
this.feature1 = false;
this.feature2 = false;
}
int getPrice(){
return price + (feature1 ? 1000 : 0) + (feature2 ? 2000 : 0);
}
}
之后,我从这个类中导出了两辆车,如
public class Car1 extends Car{
private static int price = 20000;
public Car1() {
super(price);
}
}
除价格为30000外,Car2等级完全相同
在此之后,我创建了一个cardecorator类,它是
public abstract class CarDecorator extends Car {
protected Car decoratedCar;
public CarDecorator(){
decoratedCar = new Car();
}
public CarDecorator(Car decoratedCar) {
this.decoratedCar = decoratedCar;
}
public int getPrice(){
return this.decoratedCar.getPrice();
}
}
最后,我创建了2个从CarDecorator派生的装饰器类:
public class F1Decorator extends CarDecorator{
public F1Decorator(Car car) {
super(car);
decoratedCar.feature1 = true;
}
}
public class F2Decorator extends CarDecorator{
public F2Decorator(Car car) {
super(car);
decoratedCar.feature2 = true;
}
}
public class Test {
public static void main(String[] args){
Car car1 = new Car1();
System.out.println("Price: " + car1.getPrice());
car1 = new F1Decorator(car1);
System.out.println("Price: " + car1.getPrice());
car1 = new F2Decorator(car1);
System.out.println("Price: " + car1.getPrice());
}
}
输出是
Price: 20000
Price: 21000
Price: 21000
为什么功能2对car1没有任何影响。我的设计有什么问题。如果你能帮忙,我想我会很好地理解装饰者的模式。当你用
F1Decorator
装饰car1
时,你会返回F1Decorator
,这是一辆车。构造函数在原始的car1
上设置feature1
。这辆新装饰过的汽车被分配回car1
但是,当您再次使用F2Decorator
装饰car1
时,您是在装饰F1Decorator
,而不是原来的汽车。您正在设置的是f1装饰器的功能2
,而不是原来的汽车的功能2
。因此,原装汽车上的功能2
仍然是false
,价格仍然是21000
介绍并调用Car
上的方法以及将通过特性设置传递到Car
的装饰器类
在汽车中
:
public void setFeature1(boolean feat1)
{
this.feature1 = feat1;
}
public void setFeature2(boolean feat2)
{
this.feature2 = feat2;
}
在CarDecorator
中:
public void setFeature1(boolean feat1)
{
this.decoratedCar.setFeature1(feat1);
}
public void setFeature2(boolean feat2)
{
this.decoratedCar.setFeature2(feat2);
}
public F1Decorator(Car car) {
super(car);
// Replace the assignment with this line.
decoratedCar.setFeature1(true);
}
public F2Decorator(Car car) {
super(car);
decoratedCar.setFeature2(true);
}
在F1Decorator
中:
public void setFeature1(boolean feat1)
{
this.decoratedCar.setFeature1(feat1);
}
public void setFeature2(boolean feat2)
{
this.decoratedCar.setFeature2(feat2);
}
public F1Decorator(Car car) {
super(car);
// Replace the assignment with this line.
decoratedCar.setFeature1(true);
}
public F2Decorator(Car car) {
super(car);
decoratedCar.setFeature2(true);
}
在F2Decorator
中:
public void setFeature1(boolean feat1)
{
this.decoratedCar.setFeature1(feat1);
}
public void setFeature2(boolean feat2)
{
this.decoratedCar.setFeature2(feat2);
}
public F1Decorator(Car car) {
super(car);
// Replace the assignment with this line.
decoratedCar.setFeature1(true);
}
public F2Decorator(Car car) {
super(car);
decoratedCar.setFeature2(true);
}
通过这些更改,现在输出为:
Price: 20000
Price: 21000
Price: 23000
我没有看到在getPrice
方法中使用feature2
。也许应该在那里用。对不起,输入错误。我已经编辑过了。只是复制粘贴错误。问题仍然存在。