前面提到的Laptop类将被修改为使用静态工厂方法实现对象工厂。构造函数因此要为私有:
private Laptop (String reference){
this.reference = reference;
numLaptops = numLaptops + 1;
}
提供一个公共的getInstance方法,以确保计算机对象仅实例化一次。定义了一个静态哈希图LAPTOPS,以通过其引用来跟踪便携式计算机对象:
提供一个公共的getInstance方法,以确保计算机对象仅实例化一次。定义了一个静态哈希图LAPTOPS,以通过其引用来跟踪便携式计算机对象:
... LAPTOPS = new HashMap<String, Laptop>();
要求完成getInstance的方法:
要求完成getInstance的方法:
public static Laptop getInstance(String reference) {
// Code to insert here
}