This is an interesting question because there are not enough details to really say definitively. I do believe there is an odor coming off this code, and it's the stench of procedural trying to look like object-oriented programming. It's funny that the method is entitled 'process' and the types are either strings or generic object like names. What is doing the processing? Surely not a controller object, I am guessing. At some point, you want to have objects that have responsibilities. So let's assume that the base of this was the concept of an Application (e.g. an application for a job). You might want to pull the method process up into that type, and then use inheritance to augment functionality by introducing different Application types.
Another option would be to introduce a Chain of Responsibility pattern. The problem I see with that is you seem to be saying that there are only 2 links in the potential chain. The advantage of the chain approach is that the handlers don't have to know anything about each other.
A third option would be to make the successors implement an interface. This would be something like the Command Pattern, then the actual 'processing' is really just a trigger and each thing is responsible for its own behavior.
IMHO it is not smelly because of overloading. If these weren't overloads, but different methods, it would be code smell anyway. From what you write, it seems that the code does not follow the open/closed principle. If don't provide more details on what you are trying to accomplish with the code, we cannot suggest any solution.
I can think of a couple of possibilities that explain this kind of overloading quite rationally:
Only one of these functions actually does the work, and the others are merely presented for convenience and internally call the 'worker' method. It could even be that none of these methods do any work and they all call an internal (private) method that does the heavy lifting. I don't see any problem with that pattern and use it myself, there is no replication.
It could be that what the overloaded methods are trying to achieve is just not possible generically due to syntactical limitations or design decisions.
As has been previously mentioned, you don't provide information on what these methods are trying to achieve. You may well be able to refactor and eliminate all but one variation of those methods but before I can recommend whether or not you should do it or how you would do it you need to specify what the purposes of the methods are and how they differ from each-other. The name 'process' doesn't tell us much about them.
发布评论
评论(3)
这是一个有趣的问题,因为没有足够的细节来真正明确地说。我确实相信这段代码散发出一种气味,这是程序试图看起来像面向对象编程的恶臭。有趣的是,该方法的标题是“process”,类型要么是字符串,要么是通用对象(如名称)。正在做什么处理?我猜肯定不是控制器对象。在某些时候,您希望拥有具有责任的对象。因此,我们假设其基础是应用程序的概念(例如工作申请)。您可能希望将方法过程拉入该类型,然后使用继承通过引入不同的应用程序类型来增强功能。
另一种选择是引入责任链模式。我看到的问题是你似乎在说潜在链中只有 2 个链接。链式方法的优点是处理程序不必相互了解任何信息。
第三种选择是让后继者实现一个接口。这类似于命令模式,然后实际的“处理”实际上只是一个触发器,每个事物都对自己的行为负责。
This is an interesting question because there are not enough details to really say definitively. I do believe there is an odor coming off this code, and it's the stench of procedural trying to look like object-oriented programming. It's funny that the method is entitled 'process' and the types are either strings or generic object like names. What is doing the processing? Surely not a controller object, I am guessing. At some point, you want to have objects that have responsibilities. So let's assume that the base of this was the concept of an Application (e.g. an application for a job). You might want to pull the method process up into that type, and then use inheritance to augment functionality by introducing different Application types.
Another option would be to introduce a Chain of Responsibility pattern. The problem I see with that is you seem to be saying that there are only 2 links in the potential chain. The advantage of the chain approach is that the handlers don't have to know anything about each other.
A third option would be to make the successors implement an interface. This would be something like the Command Pattern, then the actual 'processing' is really just a trigger and each thing is responsible for its own behavior.
恕我直言,它不会因为超载而发臭。如果这些不是重载,而是不同的方法,那么无论如何都会有代码味道。从您所写的内容来看,代码似乎不遵循开放/封闭原则。如果不提供有关您尝试使用代码完成的任务的更多详细信息,我们无法建议任何解决方案。
IMHO it is not smelly because of overloading. If these weren't overloads, but different methods, it would be code smell anyway. From what you write, it seems that the code does not follow the open/closed principle. If don't provide more details on what you are trying to accomplish with the code, we cannot suggest any solution.
我可以想到几种可能性来相当合理地解释这种重载:
正如前面提到的,您没有提供有关这些方法试图实现的目标的信息。您很可能能够重构和消除这些方法的所有变体(除了一种变体),但在我可以建议您是否应该这样做或如何做之前,您需要指定这些方法的目的是什么以及它们与彼此。 “进程”这个名字并没有告诉我们太多关于它们的信息。
I can think of a couple of possibilities that explain this kind of overloading quite rationally:
As has been previously mentioned, you don't provide information on what these methods are trying to achieve. You may well be able to refactor and eliminate all but one variation of those methods but before I can recommend whether or not you should do it or how you would do it you need to specify what the purposes of the methods are and how they differ from each-other. The name 'process' doesn't tell us much about them.