2010-11-23 9 views
3

第1に、私の擬似コードは、私はこの場合完全なコードより読みやすいと思います。擬似コード内のプロパティは実際には、ArticleElementを除いて、ゲッターの&セッターメソッドを持つフィールドであると仮定してください。ここでは、直接ゲッターメソッドまたは2ステップゲッターメソッド(すなわち、getArticleSource().getName())。優雅なオブジェクトの階層

ArticleTemplate 
    Long id; 
    String name; 
    String description; 
    Integer amount; 
    Schedule schedule; 

と、別の日に多くの潜在的な子エンティティを作成する(そのスケジュールを経由して)使用されています:いくつかの子供たちエンティティが作成されていない

Article 
    Long id; 
    String name; 
    String description; 
    Integer amount; 
    Date date; 
    Boolean complete; 
    ArticleTemplate template; 

は、私は、テンプレートの実体を持っていると言います親からは、スタンドアロンにすることができます(テンプレートはヌルにすることができます)。親エンティティから
a)の潜在的な子実体
b)の前に、親エンティティから
Cを作成し、実際の子どものエンティティ)孤児の子実体作成スタンド:私のUIのための

私はソート&がのリストをマージ作成したいです一人で

しかし、私は要素間の差異を決定するために、このリストの要素にいくつかのプロパティを追加する必要があります。

ArticleElement 
    // actual value if from Article, null if from potential from ArticleTemplate 
    Long id; 
    // actual value if from Article or ArticleTemplate 
    String name; 
    // actual value if from Article or ArticleTemplate 
    String description; 
    // actual value if from Article or ArticleTemplate 
    Integer amount; 
    // actual value if from Article, simulated if from potential from ArticleTemplate 
    Date date; 
    // actual value if from Article, false if from potential from ArticleTemplate 
    Boolean complete; 
    // actual value (nullable) if from Article, self if from potential from ArticleTemplate 
    ArticleTemplate template; 
    // false if from Article, true if from potential from ArticleTemplate 
    Boolean templateSimulation; 
    // once the list is sorted, a running tally of this.amount is to be stored on this object 
    Integer runningTally; 
    // would be type of interface if Article and ArticleTemplate implement same 
    Object source; 

私は少なくとも3つのクラスを持っていますが、インターフェイスなどでいくつかのアプローチがあります。

可能な限り複製とプロパティのコピーを避け、有益なところで継承を使用したいと思います。

提案が高く評価されました。

p。

+2

いくつかの用語を明確にする必要があります。 '子供と親の関係はどういう意味ですか? 'コンテナコンテンツ'または 'クリエーター結果'? 「潜在的」とはどういう意味ですか? – khachik

+0

「可能性」は明確な説明が必要です。 –

+0

申し訳ありませんが、 'child-parent'は' creator-result'つまり、テンプレート(親)から作成された記事(子)を意味します。'潜在的に'私はまだ作成されていない/まだ永続化されているが、テンプレートによって '示唆されている '記事を意味する - それは潜在的に記事である...それをより明確にする希望ですか? – pstanton

答えて

0

は、ここに私の現在のソリューションだ、と私はそれを好きでわからないんだけど、私はまだもっと良いものが出ていない:

まず、私は一人で条及びArticleTemplateを残しておきます。私は彼らに類似点を説明するインターフェースを実装させることができますが、このケースではそれほど多くの利点はありません。

public interface UiElement<T> 
{ 
    T getSource(); 
    Class<T> getType(); 
    // redundant - refer to source 
    // Long getId(); 
    String getName(); 
    String getDescription(); 
    Integer getAmount(); 
    Date getDate(); 
    Boolean getComplete(); 
    // redundant - not needed anymore 
    // ArticleTemplate getTemplate(); 
    // redundant - replaced by getType() 
    // Boolean getTemplateSimulation(); 
    Integer getRunningTally(); 
} 

は記事の実装を作成するUI契約を作成する - ほとんどのプロパティ

のソースオブジェクトに委託呼び出しを通過
public class ArticleUiElement implements UiElement<Article> 
{ 
    private Article source; 
    private Integer tally; 

    public ArticleUiElement(Article source) { 
     this.source = source; 
    } 

    public Article getSource() { 
     return source; 
    } 

    public Class<Article> getType() { 
     return Article.class; 
    } 

    public String getName() { 
     return source.getName(); 
    } 

    public String getDescription() { 
     return source.getDescription(); 
    } 

    public Integer getAmount() { 
     return source.getAmount(); 
    } 

    public Date getDate() { 
     return source.getDate(); 
    } 

    public Boolean getComplete() { 
     return source.getComplete(); 
    } 

    public String getRunningTally() { 
     return tally; 
    } 

    public void setRunningTally(String tally) { 
     this.tally = tally; 
    } 
} 

ArticleTemplateの実装を作成する - ソースへの委託の呼び出しを通過ほとんどのプロパティのオブジェクト

public class ArticleTemplateUiElement implements UiElement<ArticleTemplate> 
{ 
    private ArticleTemplate source; 
    private Integer tally; 
    private Date date; 

    public ArticleTemplateUiElement(ArticleTemplate source) { 
     this.source = source; 
    } 

    public ArticleTemplate getSource() { 
     return source; 
    } 

    public Class<ArticleTemplate> getType() { 
     return ArticleTemplate.class; 
    } 

    public String getName() { 
     return source.getName(); 
    } 

    public String getDescription() { 
     return source.getDescription(); 
    } 

    public Integer getAmount() { 
     return source.getAmount(); 
    } 

    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    public Boolean getComplete() { 
     return false; 
    } 

    public String getRunningTally() { 
     return tally; 
    } 

    public void setRunningTally(String tally) { 
     this.tally = tally; 
    } 
} 

誰かが改善を提供することができますか、完全に優れたソリューションですか?

+0

誰もより良い解決策を持っていない場合、これを答えとしてマークする必要があります... – pstanton

関連する問題