通过接口定义抽象类的全部功能

时间:2019-03-17 12:55:07

标签: java object-oriented-analysis

我正在用Java写一个关于云计算的模拟。我的代码执行以下操作:

  1. 创建部署
  2. 将其传递给仿真
  3. ???
  4. 利润。

现在有不同类型的部署(它们确实具有遵循不同统计分布的属性),但是它们都有共同的属性和方法(开始,结束,大小和更新方法)。自然,我将部署建模为抽象。

但是,我不能在模拟部分中引用它(这是一件好事,因为我希望模拟不知道它正在处理哪种部署类型(尽管有一个模块必须执行类型检查))。但是,我必须调用部署方法,因此我的抽象类现在实现了一个称为deployable的接口。有了这个模型,我可以做类似的事情:

// In a cluster class of the simulation
for(deployable d : deployments){
    d.updateOnTimestep();
}

一段时间后,我的界面越来越大。最重要的是,我必须实现一个信息对象,以便访问部署的属性(同样,每个部署还具有其他属性)。示例:

// In a class that calculates some probabilties
public void evaluate(deployable d){
    // retrieve deployment information
    d.getInfo().getSpecificParam();
}

现在我想到的是,如果我不将部署建模为抽象,则不需要接口,代码看起来会更漂亮。但是我确实确实认为,部署必须是抽象的(例如,我不希望其他程序员在没有开始的情况下初始化部署)。

您对此有何看法? -我尝试查找一些设计模式,但是我不是一位经验丰富的程序员,并且有点迷茫。任何帮助表示赞赏。

编辑:根据建议,我想提供更多代码段

public abstract class Deployment implements deployable{
    // Attributes
    int size;
    int start;
    //...
    // Interface methods, that all deployments use (only a snippet)
    public void removeScaleOut(ScaleOut sc){
        // ...
    }
    public void decreaseSize(int decreasement){
        this.size -= decreasement;
    }
    public void reassignID(String id){
        this.id = id;
    }
}

现在界面可部署:

public interface deployable {

public void updateOnTimeStep();
public void updateOnAccept();
public void updateOnSpawn(ScaleOut sc);
public void updateOnDeath();
public DeploymentInformation getInformation();
public void spawnScaleOut();
public void reassignID(String id);
public void increaseSize(int increasement);
public void decreaseSize(int decreasement);
public void addActiveScaleOut(ScaleOut scaleOut);
public void removeActiveScaleOut(ScaleOut scaleOut);
public void removeScaleOut(ScaleOut scaleOut);

}

最后是扩展抽象类的类:

public class GammaDeployment extends Deployment {

// Gamma Prior variables
private double spawnShape = 0; 
// many more...

// Variables associated with the computation of the conjugate priors.
private int pastRequests;
private int pastSpawnSizeSum;
private int overallDeadJobs = 0;
private int sumRealizedJobLifeTime =0;

// Update methods from the interface deployable.
@Override
public void updateOnTimeStep() {
    for(ScaleOut sc : activeScaleOuts) {
        sumRealizedJobLifeTime += sc.getVms().size();
    }
    age++;
}

@Override
public void updateOnSpawn(ScaleOut sc) {
    pastRequests += sc.getVms().size();
    pastSpawnSizeSum =+ sc.getSize();
}

@Override
public void updateOnDeath() {
    overallDeadJobs++; // virtual machines death.
}

//  The info object only holds attributes
@Override
public DeploymentInformation getInformation() {
    GammaDeploymentInformation gdi = new GammaDeploymentInformation(start,age,id,size,scaleOuts,this);
    gdi.setAge(age);
    gdi.setSize(size);
    gdi.setScaleOuts(scaleOuts);
    gdi.setActiveScaleOuts(activeScaleOuts);
    gdi.setSizeScale(getSizeScale());
    gdi.setSizeShape(getSizeShape());
    gdi.setSpawnScale(getSpawnScale());
    gdi.setSpawnShape(getSpawnShape());
    gdi.setDeathScale(getDeathScale());
    gdi.setDeatRate(getDeatRate());
    // Id has also to be set, since it may change over the course of the simulation.
    gdi.setId(id);
    return gdi;
}

}

现在是一个模拟部分用法的示例,在这里让我感到困扰 我需要将许多函数链接在一起才能获得size属性。

public void assign(deployable deployment, Cluster cluster) {
    int deploymentSize = deployment.getInformation().getScaleOuts().get(0).getSize();
    // Update sizes...

    // Add scale out to active scale outs. 
    deployment.addActiveScaleOut(deployment.getInformation().getScaleOuts().get(0));
    // Remove initial request.
    deployment.removeScaleOut(deployment.getInformation().getScaleOuts().get(0));
}

这会导致某些类中的if语句很长:

// Check if deployment is dead
if(currentDeployment.getInformation().getActiveScaleOuts().isEmpty()
                && currentDeployment.getInformation().getScaleOuts().isEmpty()) {

0 个答案:

没有答案