English 中文(简体)
B. 关于如何重订以下 Java法的建议
原标题:Suggestions how to re-write the following Java code snippet
  • 时间:2012-04-20 08:29:10
  •  标签:
  • 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);

<代码>ContentSlotForPageModel和ContentSlotForTemplateModel>/code>均为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);

}

然而,许多方法没有在CMSRelation Model上界定,因此,我需要加以验证,以发出正确的呼吁。 我不得不在其超级阶级上界定这一方法。 是否有更nic的写法?

谢谢。

最佳回答

Do the following.
No instanceof, strongly typed, minimisation of duplicated code.

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

问题回答

Could you create a new superclass in between them?

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

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

Also, you could use method overloading instead of 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){
    //...
}

请尝试。

首先,你把从<条码>中转来的田地活下来,然后用<条码>的操作员等儿童类别把田地集中起来。

这里的例子

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签