Note that there are some explanatory texts on larger screens.

plurals
  1. POJava OO concept better when flatten or include unnecessary process to simplify the usage?
    text
    copied!<p>Say, I hava Json parser class for type DataObject as following:</p> <pre><code>class JsonDataParser{ public DataObject parseData(String data){ // Parse data return dataObject; } } </code></pre> <p>Now I have to connect to server to get the Json, so I have this:</p> <pre><code>class DataRetriever{ public String getData(){ // Get data from server using URL return jsonDataString; } } </code></pre> <p>The question is that, is it better to code as follow in the main class (Method-A)</p> <pre><code>DataObject d = new JsonDataParser().parseData(new DataRetriever().getData()); </code></pre> <p>Or modify the DataRetriever to include Json parsing inside(Method-B) as follow:</p> <pre><code>class DataRetriever{ public DataObject getData(){ // Get data from server using URL return new JsonDataParser().parseData(jsonDataString); } } </code></pre> <p>The result would be the same in both case but conceptually, is it better to code using Method-A or Method-B?</p> <p>I normally would create a controller class to follow Method-A but I'm not sure if this is efficient or not.</p> <p><strong>EDIT:</strong><br> In fact, I'm thinking of using GenericType and Interface when parsing Json. So I prepared the following:</p> <pre><code>public interface IJsonParser&lt;T&gt; { public T parseJSON(String jsonString); public T parseJSON(JSONObject jsonObject); } </code></pre> <p>then add it to DataRetriever's constructor as follow:</p> <pre><code>class DataRetriever&lt;T&gt;{ private IJsonParser&lt;T&gt; parser; public DataRetriever(IJsonParser&lt;T&gt; parser){ this.parser = parser; } public DataObject getData(){ // Get data from server using URL return new JsonDataParser().parseData(jsonDataString); } } </code></pre> <p>Ultimately, I was thinking of allowing multiple type of parser so each time we can supply our own parser to add flexibility to it. Is this considered malpractice of sort?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload