什么时候应该使用公共静态方法

发布于 2024-09-12 16:00:40 字数 280 浏览 4 评论 0原文

有很多人反对使用“公共/私有”静态方法。我到处搜索,但没有运气,并试图找到任何提倡充分使用静态方法的人。

假设这些方法始终是内聚的,那么使用公共静态方法的可接受区域在哪里?这些方法在 Java 和 .NET 之间是否有所不同(也就是说,其中一种方法比另一种方法更容易被接受)?

最近的这篇 SO 帖子激起了我对这个主题的愤怒/兴趣。

There are a lot of people out there against the use of "public/private" static methods. I have search around,with no luck, and tried to find anyone that advocates good use of static methods.

Assuming the methods will always be Cohesive where are the acceptable areas to use public static methods? Do these methods vary between Java and .NET (aka is it more acceptable in one then the other)?

This recent SO post spurred my anger/interest in this topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你的笑 2024-09-19 16:00:40

如果方法可以被视为一个单元,并且可以对其自身进行有效测试,则使用公共静态方法。在使用静态方法的类型上实现依赖注入或模拟是很困难的。

我将静态方法用于具有很少/没有依赖项以及定义良好的输入/输出的实用程序方法。

Use public static methods if the method can be considered a unit, and can be effectively tested on its own. It's plain hard to implement dependency injection or mocks on types that use static method.

I use static methods for utility methods with few/no dependencies, and well defined input/output.

输什么也不输骨气 2024-09-19 16:00:40

静态方法通常不应该

  • 访问其参数之外的任何状态(因为这会导致难以更改的耦合)
  • 修改其参数(因为如果这样做,为什么它不是一个实例方法该参数的?)

相反,纯函数(即没有副作用)可以提供良好的静态方法。

当然,这不应该被视为绝对的教条。

A static method generally shouldn't:

  • Access any state outside its parameters (because that results in hard-to-change coupling)
  • Modify its parameters (because if it does, why isn't it an instance method of that parameter?)

Conversely, pure functions (i.e. no side effects) make for good static methods.

Of course, this should not be taken as absolute dogma.

坏尐絯℡ 2024-09-19 16:00:40

我想说,在返回保存静态方法的类型的简单工厂之类的场景中使用它们是公平的。 并不是一个巨大的飞跃

string.Empty;

说 if:是合法的静态属性 then:

string.GenerateRandomString();

是合法的静态方法 。然而即使如此,我也可以看到,纯 OO 程序员可能更喜欢 StringFactory 或 RandomStringGenerator 类,但我想这完全取决于您的界限。

I would say it is fair to use them in simple factory like scenarios where the type that holds the static method is returned. It isn't a huge leap to say that if:

string.Empty;

is a legitimate static property then:

string.GenerateRandomString();

is a legitimate static method. However even then I can see that a pure OO programmer may prefer a StringFactory or RandomStringGenerator class, but I guess it all depends on where you draw the line.

眼前雾蒙蒙 2024-09-19 16:00:40

将方法标记为静态会告诉使用者您不会更改传递到该方法中的任何对象的状态。静态方法应该对其传递的参数执行操作,并且不应该依赖任何内部字段。

我相信您引用的帖子中给出的具体建议与静态方法所在的位置有关,而不是反对使用静态方法的建议。

Marking a method as static tells consumers that you won't change the state of any objects passed into the method. A static method should perform an operation on the parameters it is passed and shouldn't have a reliance on any internal fields.

I believe the specific advice given in the post you reference is to do with where the static method was located - rather than advice against using a static method.

〆一缕阳光ご 2024-09-19 16:00:40

一种典型用途是实现单例模式。

one typical use is to implement Singleton pattern.

垂暮老矣 2024-09-19 16:00:40

我将静态/共享用于不属于类实例的方法,或者如果我正在更新“共享”变量(请记住使它们成为线程安全的)。

通常,这意味着我最终将这些方法放在包含它们的不同“Manager”类中,我还将构造函数标记为私有,以确保它们无法实现。

附带说明:静态/共享方法稍快,我记得一些关于他们的CLR 中的实现与 OOP 无关,并且不允许在接口中实现,这与 Java 固有的 OOP 语言(至少是 .NET)的设计缺陷有关 - 此处讨论

I use Static/Shared for methods that are not part of the instance of the class, or if I am updating "shared" variables (remember to make them thread safe).

Usually, this means I end up having these methods in a different "Manager" class that contains them, I mark the constructor as private as well to make sure they can't be implemented.

On a side note: static/shared methods are slightly faster and I remember something about their implementementation in the CLR being non-OOP related and not allowed in interfaces which has something to do with a design flaw in OOP languages (.NET at least) inherent from Java - Discussed Here.

撩动你心 2024-09-19 16:00:40

静态方法与全局对象实例上的实例方法具有相同的语义。除非您对全局对象实例很熟悉,否则您不应该使用静态方法。也就是说,这在很大程度上取决于具体情况。有时,令人讨厌的黑客攻击是正确的做法。

A static method has the same semantics as an instance method on a global object instance. Unless you are cool with a global object instance, you shouldn't want to use a static method. That said, it depends a lot on the situation. Some times, a nasty hack is the right thing to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文