Psuedo-Backwards Builder Pattern?

Posted by Avid Aardvark on Stack Overflow See other posts from Stack Overflow or by Avid Aardvark
Published on 2010-04-29T04:29:07Z Indexed on 2010/04/29 4:37 UTC
Read the original article Hit count: 536

In a legacy codebase I have a very large class with far too many fields/responsibilities. Imagine this is a Pizza object.

It has highly granular fields like:

  • hasPepperoni
  • hasSausage
  • hasBellPeppers

I know that when these three fields are true, we have a Supreme pizza. However, this class is not open for extension or change, so I can't add a PizzaType, or isSupreme(), etc. Folks throughout the codebase duplicate the same "if(a && b && c) then isSupreme)" logic all over place. This issue comes up for quite a few concepts, so I'm looking for a way to deconstruct this object into many subobjects, e.g. a pseudo-backwards Builder Pattern.

PizzaType pizzaType = PizzaUnbuilder.buildPizzaType(Pizza); //PizzaType.SUPREME

Dough dough = PizzaUnbuilder.buildDough(Pizza);

Is this the right approach? Is there a pattern for this already?

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about design-patterns