-1

我从这个链接中获取了工厂模式示例:http: //howtodoinjava.com/2012/10/23/implementing-factory-design-pattern-in-java/

但是,当我将代码复制到自己的 IDE 中时,我收到一条警告,说我的构造函数中有一个可覆盖的方法调用。我明白这意味着什么,我只是认为工厂模式应该解决这个问题?教程有问题吗?我应该做一些不同的事情吗?

我只包括了一种汽车类型,只是为了节省我粘贴的代码量:

Car

package FactoryPattern;

public abstract class Car {



    public Car(CarType model){
        this.model = model;
        arrangeParts();
    }

    private void arrangeParts(){
        //Do one time processing herer
    }

    //Do subclass level processing in this method

    protected abstract void construct();
    private CarType model = null;
    public CarType getModel(){
        return model;
    }

    public void setModel (CarType model){
        this.model = model;
    }


}

CarFactory

package FactoryPattern;


public class CarFactory {

    public static Car buildCar(CarType model){
        Car car = null;
        switch (model) {
            case SMALL:
                car = new SmallCar();
                break;
            case SEDAN:
                car = new SedanCar();
                break;
            case LUXURY:
                car = new LuxuryCar();
                break;
            default:
                //throw an exception
                break;        
        }
        return car;
    }    
}

FactoryPattern

package FactoryPattern;

public enum CarType {

    SMALL, SEDAN, LUXURY

}

package FactoryPattern;

public class LuxuryCar extends Car {

    LuxuryCar(){
        super(CarType.LUXURY);
        construct();
    }

    @Override
    protected void construct(){
        System.out.println("Building Luxury Car");
    }

}

CarFactoryTest

package FactoryPattern;

public class CarFactoryTest {
    public static void main(String[] args) {

        CarFactory.buildCar(CarType.SMALL);
        CarFactory.buildCar(CarType.SEDAN);
        CarFactory.buildCar(CarType.LUXURY);
    }

}
4

1 回答 1

1

警告来自这里

LuxuryCar(){
    super(CarType.LUXURY);
    construct();
}

construct()方法是protected并且因此可以在 的子类中被覆盖LuxuryCar。如果不知情的开发人员重写了该方法并尝试使用父类中尚未初始化的字段,那么很多事情都会变糟。这就是 IDE 警告您的原因。

例如,

public class LuxuryCar extends Car {
    protected Televesion tv;

    LuxuryCar(){
        super(CarType.LUXURY);
        construct(); // will call the overriden method in the sub class
    }

    @Override
    protected void construct(){
        tv = new Television();
    }
}

public class BetterLuxuryCar extends LuxuryCar {
    BetterLuxuryCar(){
        super();
    }

    @Override
    protected void construct(){
        tv.upgrade(); // oops NullPointerException
    }
}

请注意,类似的方法construct()似乎属于工厂本身。此外,Car类不需要CarType字段,子类型已经知道它是什么类型。

于 2013-11-12T13:45:54.467 回答