如果"公用事业和QUOT;类是邪恶的,我在哪里把我的通用code? [英] If a "Utilities" class is evil, where do I put my generic code?
问题描述
我一般住的规则,全局变量/功能是邪恶的,每件code应该住在班到它涉及。
I generally live by the rule that Global variables / functions are evil and that every piece of code should live in the class to which it pertains.
这是一个非常简单的规则可循,而且我相信我还没有碰到这个规则的问题一直到现在。
This is a very easy rule to follow, and I believe that I haven't ever run into an issue with this rule until now.
不过,今天我需要一个功能添加到我的组装的,而不是一个特定的类。也就是说,几乎所有的类的可能的有这种特殊功能的使用。
Today, however, I need to add a function to my assembly rather than to a specific class. That is, almost all of my classes could have a use for this particular function.
我应该在哪里把这个功能(+1过载)?
如果我把它放在一个工具类,我觉得脏。如果我把它钉住到一个半相关的类,并让其他类直接调用它,我感觉更糟。
If I put it in a "Utilities" class, I feel dirty. If I tack it on to a semi-related class, and let other classes call it directly, I feel worse.
这个特殊的code的一块基本上扒一的IList<&的PointF GT;
成一个标准化的清单。我觉得现在是将其添加为对的IList℃的扩展方法的PointF>
可能是最好的选择...
This particular piece of code basically chops a IList<PointF>
into a normalized list. I feel right now that adding it as an extension method on IList<PointF>
may be the best bet...
推荐答案
如果这是在一个操作的IList&LT;的PointF&GT;
,那么它应该是一个扩展方法上的IList&LT;&的PointF GT;
。
If this is an operation on an IList<PointF>
, then it should be an extension method on IList<PointF>
.
一般情况下, utils的
,应避免使用助手
键入类。很多时候,你会发现,你可能的认为的是一个实用的方法,实际上是可能属于一类自身(就像你说的)一个相当具体的方法。但是,会有其中的Util
般班(其中组相关有用的方法)是有效的实体领域的具体情况。
Generally, Utils
and Helper
type classes should be avoided. More often than not, you will find that what you may think is a utility method, is actually a rather specific method that probably belongs in a class of its own (just like you say). However, there will be domain specific cases where Util
-like classes (classes which group related useful methods) are valid entities.
这篇关于如果&QUOT;公用事业和QUOT;类是邪恶的,我在哪里把我的通用code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!