mercredi 30 mars 2016

Which design pattern is recommended when implementations only differ in a single method?

I have an interface with 6 methods used to manage datasets. The only method that differs between implementations is getSerializedVersion() and the constructor that is able to parse the serialization string.

public interface DataSets {
  public void addEntry(...);
  public void  removeEntry(...);
  public void manipulateEntry(...);
  public SomeType getEntry(...);
  public List<SomeType> getAllEntries();
  // This differs:
  public String getSerializedVersion()
}

I can't change the Interface.

My first idea was to generate an abstract class and implement the first five methods. For the concrete implementations (e.g. DataSetsXML, DataSetsYAML, ...) I only have to implement getSerializedVersion() and the constructor that that is able to read the String and initialize the object.

To make it more testable a different design might be better (http://ift.tt/1qiSOk1) but which one?

Answers might be subjective, but I think there are some general rules or a least (objective) advantages and disadvantages of the different approaches,...

Aucun commentaire:

Enregistrer un commentaire