是全球性的静态类和方法不好? [英] Are global static classes and methods bad?

查看:160
本文介绍了是全球性的静态类和方法不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它一般在全球的东西,严重依赖同意要避免。难道使用静态类和方法是一回事吗?

It's generally agreed upon that relying heavily on global stuff is to be avoided. Wouldn't using static classes and methods be the same thing?

推荐答案

全局数据是坏的。然而,许多问题就可以避免与静态方法工作。

Global data is bad. However many issues can be avoided by working with static methods.

我要带丰富希基的就这一个,并解释是这样的:

I'm going to take the position of Rich Hickey on this one and explain it like this:

要建立最可靠的系统在C#中使用静态方法和类,而不是全球性的数据。例如,如果你的手在一个数据对象为静态方法,而静态方法不访问任何静态数据,那么你大可放心,因为输入数据的函数的输出将始终是相同的。这是采取的二郎神,Lisp语言,Clojure的位置,和所有其他函数式编程语言

To build the most reliable systems in C# use static methods and classes, but not global data. For instance if you hand in a data object into a static method, and that static method does not access any static data, then you can be assured that given that input data the output of the function will always be the same. This is the position taken by Erlang, Lisp, Clojure, and every other Functional Programming language.

使用静态方法可以大大简化多线程编码,因为,如果编程正确,只有一个线程将可使用一组给定数据在一个时间。这真的是什么它归结为。具有全局数据是坏的,因为它是随时可以由谁知道线程被改变的状态下,和。然而,静态方法允许非常干净code,可以以较小的增量进行测试。

Using static methods can greatly simplify multi-threaded coding, since, if programmed correctly, only one thread will have access to a given set of data at a time. And that is really what it comes down to. Having global data is bad since it is a state that can be changed by who knows what thread, and any time. Static methods however allow for very clean code that can be tested in smaller increments.

我知道这将是激烈的辩论,因为它飞在C#中的面向对象的思维过程的脸,但我发现,我用的是比较静态的方法中,更清洁,更可靠的我的code是

I know this will be hotly debated, as it flies in the face of C#'s OOP thought process, but I have found that the more static methods I use, the cleaner and more reliable my code is.

此影片解释它比我好的,但说明了如何不可改变的数据,和静态方法可以产生一些非常线程安全code。

This video explains it better than I can, but shows how immutable data, and static methods can produce some extremely thread-safe code.

让我澄清一点的一些问题与全球数据。恒(或只读)的全球数据是几乎​​没有大的问题,作为可变(读/写)的全球数据。因此,如果是有意义的有数据的全局缓存,使用全局数据!在一定程度上,它使用一个数据库中的每个应用程序都会有,因为我们可以说,所有的一个SQL数据库是存放数据的一个庞大的全局变量。

Let me clarify a bit more some issues with Global Data. Constant (or read-only) global data isn't nearly as big of an issue as mutable (read/write) global data. Therefore if it makes sense to have a global cache of data, use global data! To some extent every application that uses a database will have that, since we could say that all a SQL Database is one massive global variable that holds data.

所以,做一个毯子声明像我上面可能有点强。相反,让我们说,有全局数据引入了许多问题,可避免其本地数据来代替。

So making a blanket statement like I did above is probably a bit strong. Instead, let's say that having global data introduces many issues that can be avoid by having local data instead.

有些语言,如二郎通过在处理该数据的所有请求一个单独的线程缓存来解决这个问题。这样,你知道,所有的请求和修改数据将是原子和全局高速缓存将不会被留在一些未知的状态。

Some languages such as Erlang get around this issue by having the cache in a separate thread that handles all requests for that data. This way you know that all requests and modifications to that data will be atomic and the global cache will not be left in some unknown state.

这篇关于是全球性的静态类和方法不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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