我目前正在重构一些旧代码。我正在寻找有关在此使用的最佳设计模式的方向。 我在考虑工厂模式,但我不确定这是否是最好的方法。如何重构此代码
所以这里是伪码的简要概述。 Foo类拥有核心业务逻辑。
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());
}
}
让我知道你怎么看待这个代码需要重构的可维护性和重用或者是罚款,是什么?
感谢您的输入。
我的Java是生锈的,但ArrayList需要两个类型的参数?究竟是什么? – 2010-08-03 17:15:44
我的不好。我试图从实际代码中创建一个伪代码,并输入一个列表而不是一张地图。 – CoolBeans 2010-08-03 17:19:18
无法理解您的代码。也似乎有很多编码错误 – YoK 2010-08-03 17:19:24