在什么情况下静态方法是一种好的做法? [英] In what situations is static method a good practice?

查看:20
本文介绍了在什么情况下静态方法是一种好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读以下讨论:

私有辅助方法是否应该是静态的,如果它们可以是静态的,并且
如果它们的类没有成员变量,所有方法都应该是静态的

似乎人们普遍会接受静态方法,但对其有点怀疑,原因如下:

It seems that people in general would accept static methods, but are a little bit skeptical about it, for the following 2 reasons:

  1. 它们很难测试.
  2. 它们违反了面向对象原则.(他们是函数,而不是方法,表示人.)

最容易接受的静态方法是私有静态方法.那么为什么静态方法会存在,在什么情况下它们是优先采用的呢?

And the most acceptable static methods are private static ones. But then why do static methods exist at all, and in what situations they are the first priority to be adopted?

推荐答案

静态方法本身并不难测试.问题是其他调用静态方法的代码很难测试,因为你不能替换静态方法.

Static methods aren't hard to test in and of themselves. The problem is that other code calling the static method is hard to test, because you can't replace the static methods.

我认为静态方法在私有时都很好当它们是实用"方法时 - 例如做字符串转义.当您对想要在测试中模拟或以其他方式替换的事物使用静态方法时,问题就出现了.工厂方法也很有用,尽管依赖注入通常是一种更好的方法 - 同样,这在一定程度上取决于您是否希望能够替换测试中的功能.

I think static methods are fine either when they're private or when they're "utility" methods - e.g. to do string escaping. The problem comes when you use static methods for things that you want to be able to mock out or otherwise replace within tests. Factory methods can be useful too, although dependency injection is generally a better approach - again, it partly depends on whether you want to be able to replace the functionality in tests.

至于不是面向对象"——并非您用一般面向对象语言编写的所有内容都必须是纯"面向对象.有时,非 OO 路线更实用,并导致代码更简单.Eric Lippert 有一篇关于此的很棒的博客文章,不幸的是我现在找不到.但是,在 这篇文章 是相关的.讲的是扩展方法而不是静态方法,但原理是一样的.

As for not being "OO" - not everything you write in a generally OO language has to be "pure" OO. Sometimes the non-OO route is simply more pragmatic and leads to simpler code. Eric Lippert has a great blog post about this, which unfortunately I can't find right now. However, there's a comment in this post which is relevant. It talks about extension methods rather than static methods, but the principle is the same.

扩展方法经常受到批评因为不够OOP".这似乎对我来说是把车放在前面马的.OOP的目的是为结构化提供指导由编写的大型软件项目不需要的人的团队了解每个的内部细节别人的工作,以便成为富有成效.C#的目的是一种有用的编程语言使我们的客户能够高效工作在我们的平台上.显然,OOP 是两者兼而有之有用且受欢迎,我们已经因此试图使它容易在 C# 中以 OOP 风格编写程序.但该C# 的目的不是成为 OOP语言".我们评估基于特征的关于它们是否对我们有用客户,而不是基于他们是否严格遵守一些抽象什么使一个学术理想语言面向对象.好愉快地从oo中获取想法,功能性、程序性、命令性、声明性的,无论如何,只要我们可以做出一致的、有用的产品这使我们的客户受益.

Extension methods are often criticized as being "not OOP enough". This seems to me to be putting the cart in front of the horse. The purpose of OOP is to provide guidelines for the structuring of large software projects written by teams of people who do not need to know the internal details of each other's work in order to be productive. The purpose of C# is to be a useful programming language that enables our customers to be productive on our platforms. Clearly OOP is both useful and popular, and we've therefore tried to make it easy to program in an OOP style in C#. But the purpose of C# is not "to be an OOP language". We evaluate features based on whether they are useful to our customers, not based on whether they conform strictly to some abstract academic ideal of what makes a language object-oriented. We'll happily take ideas from oo, functional, procedural, imperative, declarative, whatever, so long as we can make a consistent, useful product that benefits our customers.

这篇关于在什么情况下静态方法是一种好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆