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?

+2


a source to share


2 answers


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();

      

+3


a source


I think it would be a good option for extension functions that are not in java. Instead, the simplest solution in my mind is to create a class and add methods that will be extensions to that utility class as static methods; PizzaUtils.isSupreme(Pizza pizza)

...



0


a source







All Articles