有人可以帮助新手程序员了解他的解决方案是否正确吗?
我的问题类似于以下两个:
问题:我想拥有仅在初始化方法上有所不同的子类。但是,我还想防止在没有初始化的情况下实例化这些类。换句话说,我想确保在子类实例化之后总是会调用一些“initialize()”方法:
public abstract class Data {
protected Parameter dataSource;
Data(parameter1){
this.dataSource = parameter1;
loadData(); // should be called to initialise class fields and ensure correct work of other class methods
}
protected abstract loadData(){
... //uses dataSource
}
}
所以我决定在构造函数上执行初始化。它有效(现在我知道这是一个非常糟糕的做法),直到我创建了一个子类,其中初始化方法使用了一些额外的参数:
public class DataFromSpecificSources extends Data {
private Parameter dataSource2;
public DataFromSpecificSources(parameter1, parameter2){
this.dataSource2 = parameter2; // I can't put it here because the constructor is not called yet
super(parameter1); // this, of course, will not work
}
@Override
private void loadData(){
... // uses both dataSource 1 and 2
// or just dataSource2
}
}
当然,这是行不通的。然后我开始寻找正确的模式......在阅读了之前发布的问题的答案后,我决定使用工厂并将子类构造函数的可见性限制在包中:
我的解决方案:
// factory ensures that loadData() method will be called
public class MyDataFactory(){
public Data createSubClass(parameter1,parameter2){
Data subClass;
if (parameter2 != null){
subClass = new DataFromSpecificSources(parameter1, parameter2);
subClass.loadData();
} else {
subClass = new AnotherSubClass(parameter1);
subClass.loadData()
}
return subClass;
}
}
public abstract class Data {
protected Parameter dataSource;
Data(parameter1){
this.dataSource = parameter1;
}
// I don't call it in constructor anymore - instead it's controlled within the factory
protected abstract loadData(){
... //uses dataSource
}
}
public class DataFromSpecificSources {
private Parameter dataSource2;
protected DataFromSpecificSources(){}
// now this constructor is only visible within package (only for the factory in the same package)
DataFromSpecificSources(parameter1, parameter2){
super(parameter1); // it does not initialise data anymore
this.dataSource2 = parameter2;
}
@Override
protected void loadData(){
... // uses dataSources 1 and 2
}
}
现在工厂确保子类将被初始化(将加载数据)并且在其他包中不允许子类的实例化。其他类无法访问子类的构造函数,并且被迫使用工厂来获取子类的实例。
我只是想问一下我的解决方案是否正确(逻辑上),并且子类构造函数可见性仅限于包的工厂方法在这里是正确的选择吗?!还是有其他更有效的模式来解决这个问题?!