Pseudo-reverse builder pattern?
In legacy codebase, I have a very large class with too many fields / responsibilities. Imagine this is a Pizza object.
It has very granular fields such as:
- hasPepperoni
- hasSausage
- hasBellPeppers
I know that when these three fields are correct, we have a Supreme Pizza. However, this class is not open to extension or modification, so I cannot add PizzaType or isSupreme () etc. People throughout the codebase duplicate the same logic if(a && b && c) then isSupreme)
throughout the place. This problem comes from several concepts, so I am looking for a way to deconstruct this object in many sub-objects, for example. pseudo-reverse pattern Builder.
PizzaType pizzaType = PizzaUnbuilder.buildPizzaType(Pizza); //PizzaType.SUPREME
Dough dough = PizzaUnbuilder.buildDough(Pizza);
Is this the correct approach? Does this template already exist?
a source to share
How about an adapter template?
Basically, it's a wrapper class that has all the functionality you really want, which can easily move back and forth in the Pizza class.
MenuPizza myPizza = new MenuPizza(pizza);
PizzaType pizzaType = myPizza.getPizzaType();
DoughType doughType = myPizza.getDoughType();
And you can provide reverse functionality ...
MenuPizza otherPizza = new MenuPizza(PizzaType.SUPREME, DoughType.SOUR);
Pizza pizzaPOJO = otherPizza.getPizzaPOJO();
a source to share