lundi 22 juillet 2019

Design Pattern: How to structure the following scenario into proper classes?

This is more like a code "architecture" question. I have two classes One and Two. They both need data and styles, so I use two methods getData and getStyles implemented in a parent class. In the class Two, I perform some extra "transformations" or processing on both data and styles, then return it.

Is this a good approach? Is there a way to make it better? For instance, I am using two same lines getData() and getStyles() in both class One and Two.

public abstract class Base {
   protected Data getData() {
       // some code to get data and return it
   }

   protected Styles getStyles() {
       // some code to get styles and return it
   }
}

public class One extends Base {
   public String generateResult() {
       Data data = getData();
       Styles styles = getStyles();
       return getStreamedResult(data, styles);
   }
}

public class Two extends Base {
   public String generateResult() {
       Data data = getData();
       Styles styles = getStyles();
       Transforms transformedResult = performTransform(data, styles);
       return getStreamedResult(transformedResult);
   }
}

Aucun commentaire:

Enregistrer un commentaire