Java OO concept better when flatten or include unnecessary process to simplify the usage?

StackOverflow https://stackoverflow.com/questions/14976884

سؤال

Say, I hava Json parser class for type DataObject as following:

class JsonDataParser{
   public DataObject parseData(String data){
      // Parse data 
      return dataObject;
   }
}

Now I have to connect to server to get the Json, so I have this:

class DataRetriever{
   public String getData(){
      // Get data from server using URL 
      return jsonDataString;
   }
}

The question is that, is it better to code as follow in the main class (Method-A)

DataObject d = new JsonDataParser().parseData(new DataRetriever().getData());

Or modify the DataRetriever to include Json parsing inside(Method-B) as follow:

class DataRetriever{
   public DataObject getData(){
      // Get data from server using URL 
      return new JsonDataParser().parseData(jsonDataString);
   }
}

The result would be the same in both case but conceptually, is it better to code using Method-A or Method-B?

I normally would create a controller class to follow Method-A but I'm not sure if this is efficient or not.

EDIT:
In fact, I'm thinking of using GenericType and Interface when parsing Json. So I prepared the following:

public interface IJsonParser<T> {
    public T parseJSON(String jsonString);
    public T parseJSON(JSONObject jsonObject);
}

then add it to DataRetriever's constructor as follow:

class DataRetriever<T>{
   private IJsonParser<T> parser;

   public DataRetriever(IJsonParser<T> parser){
        this.parser = parser;
   }

   public DataObject getData(){
      // Get data from server using URL 
      return new JsonDataParser().parseData(jsonDataString);
   }
}

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?

هل كانت مفيدة؟

المحلول

Here you should not be seeing the efficiency as both will be equally efficient. But rather than doing this you should think about the future extensibility of the class.

In that matter I think method B would be good as you can change the parser so some other one in future.

class DataRetriever{
private DataParser dataParser;

public DataRetriever(){
    dataParser = new JsonDataParser(); //tomorrow this can be changed to something else
}

public DataObject getData(String data){
      // Get data from server using URL 
      return dataParser.parseData(data);
   }
}

UPDATE: If you are looking for a proper OOP solution for this, then you should go with factory pattern which will return you the instance of the parser based on the type you provide it.

class ParserFactory{

    //get parser
    public DataParser createParser(int type){
         //creates the new parser based on type and returns it
    }

}

this would be better as the creation of the parser would be the responsibility of the factory and that would make more sense IMO.

Link for Factory Pattern

نصائح أخرى

Depends on how often you call the method. Generally you would put the new JsonDataParser() somewhere the object would stay alive till the next parseData(...). For example as an object Property of DataRetriever.

EDIT: Which in turn would also require your DataRetriever object to stay alive. In case this is for a website (e.g. Tomcat or other Servlet container), you should look into tomcat's feature to put objects into the servlet or application context.

That depends on you usage of getData() method.In the first case you are retrieving the String from the method and later using JsonDataParser object to convert it.In the second case, you are tightly coupling your DataRetriever class with JsonDataParser object, which is not considered as a good practice.Suppose you want to change your JsonDataParser class later.You have to change code DataRetriever as well in that case.SO from design perpective, second approach is better

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top