jeudi 11 mai 2023

Versioning for child class implementation after factory design pattern

I got in to a design problem where I have different implementation for saving the data and processing other things for the different tools. I used a factory design for saving the data and same in getting the data from the database.

class SaveDataService {
  public void saveData(Data data) {
  }

  public List<Tool> getTools() {
    return List.of();
  }
}

class FishToolSaveDataService extends SaveDataService {
  @Override
  public void saveData(Data data) {
  }

  @Override
  public List<Tool> getTools() {
    return List.of(Tool.FISH);
  }
}

class DogSaveDataService extends SaveDataService {
  @Override
  public void saveData(Data data) {
  }

  @Override
  public List<Tool> getTools() {
    return List.of(Tool.DOG);
  }
}
class Data {
  JsonNode data;
  int version;
}

enum Tool {
  FISH,
  DOG
}

So, My Factory mapper is based on the enum Tool

Now I have a version for each tool to maintain but the saving implementation changes for each version. How do I maintain this without using if else checks in the saveData(Data data) method implementation.

I tried simple if/else check for version the saveData method but it will prolonged to long if I have five different version and two different tools have same logic of saving for the different version. That is duplicating my code.

How can I design this version such that I can reuse my code?

I have 15 tools implementation in to total and I am starting with the version 2 of 3 tools in total but in future more versioning and more tools will come.

Aucun commentaire:

Enregistrer un commentaire