1

所以现在,我有一个Preprocessor生成一堆实例变量映射的Service类,还有一个有setPreprocessor(Preprocessor x)方法的类,这样Service该类的实例就能够访问预处理器生成的映射。

目前,我的Service班级需要连续调用三个方法;为简单起见,我们称它们executePhaseOne为 、executePhaseTwoexecutePhaseThree。这三个方法中的每一个都实例化/修改Service实例变量,其中一些是指向Service实例Preprocessor对象的指针。

我的代码现在有这个结构:

Preprocessor preprocessor = new Preprocessor();
preprocessor.preprocess();
Service service = new Service();
service.setPreprocessor(preprocessor);
service.executePhaseOne();
service.executePhaseTwo();
service.executePhaseThree();

为了更好地组织我的代码,我想将每个executePhaseXXX()调用放在它自己单独的子类中Service,并为父类中的所有阶段保留公共数据结构Service。然后,我想execute()在父类中有一个方法Service可以连续执行所有三个阶段:

class ServiceChildOne extends Service {
    public void executePhaseOne() {
        // Do stuff
    }
}

class ServiceChildTwo extends Service {
    public void executePhaseTwo() {
        // Do stuff
    }
}

class ServiceChildThree extends Service {
    public void executePhaseThree() {
        // Do stuff
    }
}

编辑:

问题是,我如何在父类中编写我的execute()方法?Service我有:

public void execute() {
    ServiceChildOne childOne = new ServiceChildOne();
    ServiceChildTwo childTwo = new ServiceChildTwo();
    ServiceChildThree childThree = new ServiceChildThree();
    System.out.println(childOne.preprocessor); // prints null
    childOne.executePhaseOne();
    childOne.executePhaseTwo();
    childOne.executePhaseThree();
}

但是,我childOnechildTwo, 和childThree对象无法访问preprocessor存在于父类中的实例变量Service......我该如何解决这个问题?

4

4 回答 4

3

为您的实例变量使用protected修饰符,如下所示:PreprocessorService

public class Service {
    protected Preprocessor preprocessor;
}

然后每个子类Service都有一个this.preprocessor.

于 2013-08-01T00:40:04.350 回答
0

看起来您的问题是您没有一个,而是四个不同的实例Service- 每个实例都有自己的未初始化的基类变量副本。

目前我能想到的唯一解决方案是,首先,将 Service 成员变量设为静态的设计相当糟糕——这实际上意味着您一次只能拥有一个 Service 版本。在我看来,更好的解决方案是将处理阶段设为子类,而是将它们设为独立的类,将 的实例Service作为参数。

编辑:举个简单的例子,Service该类可能如下所示:

class Service
{
    public Service() { ... }
    public Preprocessor getPreprocessor() { ... }
    public void setPreprocessor(Preprocessor preprocessor { ... }
    public Type2 getVariable2() { ... }
    public void setVariable2(Type2 variable2) { ... }
        ...
}

阶段类可能类似于:

class ServicePhaseOne
{
    private Service m_dataHost;

    public ServicePhaseOne(Service dataHost)
    {
        m_dataHost = dataHost;
    }

    public void executePhaseOne()
    {
        // Do phase 1 stuff
    }
}

... 以此类推,用于第 2 阶段和第 3 阶段。

然后,execute() 方法将如下所示:

public void execute()
{
    ServicePhaseOne phaseOne = new ServicePhaseOne(this);
    ServicePhaseTwo phaseTwo = new ServicePhaseTwo(this);
    ServicePhaseThree phaseThree = new ServicePhaseThree(this);
    phaseOne .executePhaseOne();
    phaseTwo .executePhaseTwo();
    phaseThree .executePhaseThree();
}
于 2013-08-01T02:03:33.127 回答
0

preprocessor应该protectedpublic能够从孩子那里获得访问权。您可以在此处阅读有关修饰符的信息。

更新

new ServiceChildOne(new Preprocessor());
.....

class ServiceChildOne extends Service {

    public ServiceChildOne(Preprocessor preprocessor) {
       super.preprocessor = preprocessor;
    }

    public void executePhaseOne() {
        // Do stuff
    }
}
于 2013-08-01T00:39:39.880 回答
0

您可以提供一些方法,例如getPreprocessorInstance()在您的Service类中返回Preprocessor实例

于 2013-08-01T00:40:44.273 回答