本文介绍了如果/ else /执行代码分支的长列表的最佳设计模式/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遗产代码,我想重构。

代码基本上是对服务器进行远程调用,并获得回复。然后根据回复执行相应的操作。

代码框架示例:

  public Object processResponse String responseType,Object response){
if(responseType.equals(CLIENT_REGISTERED)){
// code
// code ...
}
else if( responseType.equals(CLIENT_ABORTED)){
// code
// code ....
}
else if(responseType.equals(DATA_SPLIT)){
// code
// code ...
}
etc

问题是,有许多if / else分支和每个if中的代码如果不是微不足道的。

所以很难维护。

我想知道什么这是最好的模式吗?

一个以为我是创建一个单一的对象,方法名称与responseType相同,然后在processResponse内部使用反射调用与responseType相同名称的方法。

T他会清理processResponse,但是它会将代码移动到一个具有很多/很多方法的单个对象,我认为反射会导致性能问题。

有没有一个很好的设计方法/模式来清理它? p>

解决方案

两种方法:


  1. 策略模式

  2. 创建字典,其中key是元数据(在您的情况下,元数据是responseType),value是一个函数。

例如:



将其置于构造函数



answers = new HashMap< string,SomeAbstraction>();
responses.Put(CLIENT_REGISTERED,new ImplementationForRegisteredClient());
responses.Put(CLIENT_ABORTED,new ImplementationForAbortedClient());



其中 ImplementationForRegisteredClient ImplementationForAbortedClient 实现 SomeAbstraction



并调用这个字典通过
answers.get(responseType).MethodOfYourAbstraction(SomeParams);



如果你想遵循DI的原则,您可以在客户端类中注入该字典。


I have a "legacy" code that I want to refactor.
The code basically does a remote call to a server and gets back a reply. Then according to the reply executes accordingly.
Example of skeleton of the code:

public Object processResponse(String responseType, Object response) {
    if(responseType.equals(CLIENT_REGISTERED)) {
       //code
       //code ...
    }
    else if (responseType.equals(CLIENT_ABORTED)) {
       //code
       //code....
    }
    else if (responseType.equals(DATA_SPLIT)) {
      //code
      //code...
   }
   etc

The problem is that there are many-many if/else branches and the code inside each if is not trivial.
So it becomes hard to maintain.
I was wondering what is that best pattern for this?
One thought I had was to create a single object with method names the same as the responseType and then inside processResponse just using reflection call the method with the same name as the responseType.
This would clean up processResponse but it moves the code to a single object with many/many methods and I think reflection would cause performance issues.
Is there a nice design approach/pattern to clean this up?

解决方案

Two approaches:

  1. Strategy pattern http://www.dofactory.com/javascript/strategy-design-pattern
  2. Create dictionary, where key is metadata (in your case metadata is responseType) and value is a function.

For example:

Put this in constructor

responses = new HashMap<string, SomeAbstraction>();responses.Put(CLIENT_REGISTERED, new ImplementationForRegisteredClient());responses.Put(CLIENT_ABORTED, new ImplementationForAbortedClient());

where ImplementationForRegisteredClient and ImplementationForAbortedClient implement SomeAbstraction

and call this dictionary viaresponses.get(responseType).MethodOfYourAbstraction(SomeParams);

If you want to follow the principle of DI, you can inject this Dictionary in your client class.

这篇关于如果/ else /执行代码分支的长列表的最佳设计模式/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:05