建议如何重新编写以下Java代码段

时间:2012-04-20 08:29:11

标签: java

我试图将此代码转换为更优雅或更有效的代码编码方式。

final ContentSlotForPageModel rel = modelService.create(ContentSlotForPageModel.class);
rel.setUid("rel_1");
rel.setPosition("no");
rel.setCatalogVersion(catalogVersionModel);
rel.setPage(firstContentPage);
rel.setContentSlot(slot);
modelService.save(rel);

final ContentSlotForTemplateModel relTemplate = modelService.create(ContentSlotForTemplateModel.class);
relTemplate.setUid("relTemplate_1");
relTemplate.setPosition("no");
relTemplate.setCatalogVersion(catalogVersionModel);
relTemplate.setPageTemplate(template);
relTemplate.setContentSlot(slot);
modelService.save(rel);

其中ContentSlotForPageModelContentSlotForTemplateModelCMSRelationModel的子类型。所以我尝试创建一个方法来设置这些属性,使用它的超类型:

private void setRelationModel(final CMSRelationModel rel, final ContentSlotModel slot, final String id)
{
    rel.setUid(id);
    rel.setCatalogVersion(catalogVersionModel);

    if (rel instanceof ContentSlotForPageModel)
    {
        ((ContentSlotForPageModel) rel).setPage(firstContentPage);
        ((ContentSlotForPageModel) rel).setContentSlot(slot);
        ((ContentSlotForPageModel) rel).setPosition("no");
    }
    else if (rel instanceof ContentSlotForTemplateModel)
    {
        ((ContentSlotForTemplateModel) rel).setPageTemplate(template);
        ((ContentSlotForTemplateModel) rel).setContentSlot(slot);
        ((ContentSlotForTemplateModel) rel).setPosition("no");
    }

    modelService.save(rel);

}

但是,CMSRelationModel上没有定义很多方法,因此我需要创建一个验证来进行正确的调用。我不能在它的超类上定义这个方法。有没有更好的方式来写这个?

谢谢

3 个答案:

答案 0 :(得分:1)

执行以下操作 没有instanceof,强类型,重复代码的最小化。

private void setRelationModel(ContentSlotForPageModel rel, ContentSlotModel slot, String id) {
    rel.setPage(firstContentPage);
    rel.setContentSlot(slot);
    rel.setPosition("no");
    setCMSRelationModel(rel);
}

private void setRelationModel(ContentSlotForTemplateModel rel, ContentSlotModel slot, String id) {
    rel.setPageTemplate(template);
    rel.setContentSlot(slot);
    rel.setPosition("no");
    setCMSRelationModel(rel);
}

private void setCMSRelationModel(CMSRelationModel rel, String id) {
    rel.setUid(id);
    rel.setCatalogVersion(catalogVersionModel);
    modelService.save(rel);
}

我还从参数中删除了final以减少“代码噪音”。

答案 1 :(得分:0)

你能在它们之间创建一个新的超类吗?

public abstract class SuperContentSlotModel extends CMSRelationModel{
    private String position;
    private ContentSlotModel slot;
    //...
}

class ContentSlotForPageModel extends SuperContentSlotModel{
    private int page;
    //...
}

此外,您可以使用方法重载而不是instanceof

private void setRelationModel(final ContentSlotForPageModel rel, final ContentSlotModel slot, final String id){
    //...
}
private void setRelationModel(final ContentSlotForTemplateModel rel, final ContentSlotModel slot, final String id){
    //...
}

答案 2 :(得分:0)

请你试试这个..

首先,填充从CMSRelationModel继承的字段,然后根据您的子类填充字段,例如使用instanceof运算符。

这是样本

private void setRelationModel(final CMSRelationModel rel, final ContentSlotModel slot, final String id)
{
    rel.setUid(id);
    rel.setCatalogVersion(catalogVersionModel);
    rel.setContentSlot(slot); // assume this is inherited method from CMSRelationModel
    rel.setPosition("no"); // assume this is inherited method from CMSRelationModel

    if (rel instanceof ContentSlotForPageModel)
    {
        ((ContentSlotForPageModel) rel).setPage(firstContentPage);
    }
    else if (rel instanceof ContentSlotForTemplateModel)
    {
        ((ContentSlotForTemplateModel) rel).setPageTemplate(template);
    }

    modelService.save(rel);
}