1

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

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);

WhereContentSlotForPageModelContentSlotForTemplateModel是 的子类型CMSRelationModel。所以我尝试创建一个方法,通过使用它的超类型来设置这些属性,如下所示:

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 上没有定义许多方法,因此我需要创建一个验证来进行正确的调用。我不能在它的超类上定义这个方法。有没有更好的方法来写这个?

谢谢

4

3 回答 3

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从参数中删除以减少“代码噪音”。

于 2012-04-20T11:04:50.547 回答
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){
    //...
}
于 2012-04-20T08:36:47.973 回答
0

你能试试这个吗..

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

这是样本

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);
}
于 2012-04-20T08:57:35.387 回答