Forum Stats

  • 3,733,040 Users
  • 2,246,686 Discussions
  • 7,856,490 Comments

Discussions

Good Java Design pattern/implementation approach suggestion for the below case.

user10494518
user10494518 Member Posts: 2

I have the question regarding the code snippet below.

<span class="kwd" style="color: #00008b;">interface</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">Component</span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="kwd" style="color: #00008b;">boolean</span><span class="pln" style="color: #000000;"> isRelevant</span><span class="pun" style="color: #000000;">();</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="pun" style="color: #000000;">}</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="kwd" style="color: #00008b;">public</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">abstract</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">class</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">AbstractComponent</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">implements</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">Component</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="typ" style="color: #2b91af;">String</span><span class="pln" style="color: #000000;"> componentType</span><span class="pun" style="color: #000000;">;</span>
<span class="pun" style="color: #000000;"></span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">List</span><span class="pun" style="color: #000000;"><</span><span class="typ" style="color: #2b91af;">String</span><span class="pun" style="color: #000000;">></span><span class="pln" style="color: #000000;"> data</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">     </span><span class="kwd" style="color: #00008b;">public</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">AbstractComponent</span><span class="pun" style="color: #000000;">(</span><span class="typ" style="color: #2b91af;">String</span><span class="pln" style="color: #000000;"> componentType</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">List</span><span class="pun" style="color: #000000;"><</span><span class="typ" style="color: #2b91af;">String</span><span class="pun" style="color: #000000;">></span><span class="pln" style="color: #000000;"> data</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">     </span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> </span><span class="com" style="color: #808080;">//assign values. </span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">}</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="pun" style="color: #000000;">}</span>

<span class="kwd" style="color: #00008b;">class</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">ComponentA</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">implements</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">Component</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> </span> <span class="pln" style="color: #000000;"></span><span class="com" style="color: #808080;">//Constructor with super method to pass type and data to parent class. </span><span class="pln" style="color: #000000;"> </span> <span class="pln" style="color: #000000;"></span><span class="kwd" style="color: #00008b;">public</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">boolean</span><span class="pln" style="color: #000000;"> isRelevant</span><span class="pun" style="color: #000000;">()</span> <span class="pun" style="color: #000000;"></span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> </span><span class="com" style="color: #808080;">//Use data to check relevance. </span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">}</span><span class="pln" style="color: #000000;"> </span> <span class="pln" style="color: #000000;"></span><span class="pun" style="color: #000000;">}</span><span class="pln" style="color: #000000;"> </span> <span class="pln" style="color: #000000;">and so on components</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">  </span> <span class="pln" style="color: #000000;"></span><span class="com" style="color: #808080;">//Main program to Test </span><span class="pln" style="color: #000000;"> </span> <span class="pln" style="color: #000000;"></span><span class="typ" style="color: #2b91af;">List</span><span class="pun" style="color: #000000;"><</span><span class="typ" style="color: #2b91af;">Component</span><span class="pun" style="color: #000000;">></span><span class="pln" style="color: #000000;"> components</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"> </span><span class="com" style="color: #808080;">//Initialize all Components with type and data.Ex: new Component("CompA", Data);</span>

Iterate through the Component and run isRelevance over components.

<span class="kwd" style="color: #00008b;">for</span><span class="pln" style="color: #000000;"> each component </span>
<span class="pln" style="color: #000000;"></span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="kwd" style="color: #00008b;">boolean</span><span class="pln" style="color: #000000;"> flag </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> comp</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">isRelevant</span><span class="pun" style="color: #000000;">()</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="com" style="color: #808080;">//HOW DO I Check Component Type here that need to be used in next section as i explained?</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="pun" style="color: #000000;">}</span>

Secondly based on the combination of Components with true/false, we will arrive with name.

Ex: CompA is relevant, B is relevant and C is not. ===> "Good"

CompA is not relevant, B is relevant and C is not. ===> "Better"

I was thinking of creating a Map with 1/0 bit flags combination and assigning the name.

Say "110" => Good.

Can i have some suggestions on giving a better design/implementation approach for this?

Sign In or Register to comment.