dimanche 8 mai 2016

What is a good java design pattern for similar objects with branching logic

I want to refactor an existing class of almost 5000 lines but I'm having difficulty with the constructor. Right now it's something like the following(methods here are in reality 10-30 blocks of code )

public MyClass( MyObject o ) {
  if ( o.name.equalsIgnoreCase("a") ) {
    doSomething()
   } else  {
     doSomethingElse() 
   }
   commonCode()
   if (o.name.equalsIgnoreCase("a") ) {
     doSecondThing()
   } else {
     doOtherSecondThing() //almost identical to doSecondThing but with some extra steps that absolutely have to be done in this sequence
  }
 // more of the same

}

I considered using inheritance and breaking things up into functions that would be overridden where necessary but that feels messy to me. Is there a pattern that fits this use case? Incidentally any advice on refactoring legacy code would be more than welcome.

Aucun commentaire:

Enregistrer un commentaire