开发者

global static class object

I am creating a global static object which will be used throug开发者_Go百科hout the application. I will use the member functions inside delegates/threads. Is there any potential pitfalls in this approach?


If the object contains no mutable state it can be fine. I like using static functions that are side effect free. (Mockists might disagree with that since you can't mock static functions as easily as interface functions.)

But global mutable state is evil. You can find plenty of articles on the issues with global variables (and their other forms like singletons, static fields,...).

If you really need some kind of global state, don't write your program with that assumption in mind. Instead have DependencyInjection inject an instance of than global state object it into your other objects. That way you can switch to multiple instances easily once the need arises. For example when running tests it's really useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜