I am currently refactoring some old code. I am looking for directions on the best design pattern to use here.
I am thinking of the factory pattern but I am not sure if that’s the best way to go or not.
So here is a brief outline of the pseudocode. Class Foo has the core business logic in it.
Class Foo{
private List<Things> stuff;
private static Integer count;
//getter setter for stuff
public Foo(List<Things> stuff){
this.stuff = stuff;
this.count=1;
}
//the following 3 methods are 90% similar
public List<Newthings> doSomeThingFirst(){
//uses the list stuff and also increments the static member count for each entry in List<NewThings> based on certain conditions
}
public List<Newthings> doSomethingSecond(){
//uses the list stuff and also increments the static member count for each entry in List<NewThings> based on certain conditions
}
public List<Newthings> doSomethingThird(){
//uses the list stuff and also increments the static member count for each entry in List<NewThings> based on certain conditions
}
//in the future there may be doSomethingFourth(), doSomethingFifth() ... etc.
}
The caller of class Foo looks something like below.
Class SomeServiceImpl{
public List<Things> getAllFoo(List<Things> stuff){
Map<Integer,NewThings> fooList = new HashMap<Integer,NewThings>();
Foo foo = new Foo(stuff);
fooList.put(1,foo.doSomeThingFirst());
fooList.put(2,foo.doSomeThingSecond());
fooList.put(3,foo.doSomeThingThird());
return new ArrayList<Things>(fooList.values());
}
}
Let me know how do you think this code should be refactored for maintainability and reuse or is it fine as is?
Thanks for your inputs.
Looks like the
BUILDERpattern calling the methods in order.link http://en.wikipedia.org/wiki/Builder_pattern
Probably the
STRATEGYpattern to remove the 90% duplicationslink http://en.wikipedia.org/wiki/Strategy_pattern