为什么最佳实践的OOP静态类有什么不同? [英] Why best practices vary for Static classes in OOP?

查看:157
本文介绍了为什么最佳实践的OOP静态类有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在阅读有关Java的最佳实践,我发现,根据这本书我们必须的青睐静态类非静态的多的。我记得,在C#中的最佳实践,我们必须根据C#3.0,4.0和5.0丹尼斯Doomen编码准则避免这样的:

I'm currently reading about Java best practices and I found that according to this book we must Favor static classes over nonstatic. I've remembered that in C# best practices we must avoid such according to Coding Guidelines for C# 3.0, 4.0 and 5.0 by Dennis Doomen:

AV1008 - 避免静态类

通过扩展方法容器除外静态类经常导致严重设计的代码。他们也很困难,如果不是不可能的,单独进行测试,除非你愿意使用一些非常哈克的工具。注意:如果你真的需要一个静态的类,它标记为静态,这样编译器可以防止实例成员和实例类。这减轻您创建一个显式的私人构造。

With the exception of extension method containers static classes very often lead to badly designed code. They are also very difficult, if not impossible, to test in isolation unless you’re willing to use some very hacky tools. Note If you really need that static class, mark it as static so that the compiler can prevent instance members and instantiating your class. This relieves you of creating an explicit private constructor.

我发现这两个对的 C#的答案并的何时使用,避免静态类,但只是出于好奇Java的答案 - 无论是C#和Java OOP语言,为什么它的这个完整的差别那么最佳实践

I found these two for C# answer and Java answer when to use and avoid static classes, but just by curiosity - both C# and Java are OOP languages, why it's this complete difference then in best practices?

更新:
我无法从Java书复制在这里这么多页,但底线是:

Update: I can't copy so much pages here from the Java book, but bottom line is:

如果您声明并不需要访问一个
封闭
实例成员类,始终把static修饰符在其声明中,使其成为一个静态的
,而不是一个非静态成员类。如果省略此修改器,每个实例将
有多余的参考其外围实例。存储此参考费用
时间和空间,可能会导致封闭的实例保留时,
本来有资格进行垃圾回收(6项)。并应你曾经
需要没有封闭实例分配一个实例,您将无法做
所以,作为非静态成员类的实例都需要有一个封闭的实例。
的一个常见用途私有静态成员类是代表$ B $的组成部分b。通过其外围类表示的对象。

If you declare a member class that does not require access to an enclosing instance, always put the static modifier in its declaration, making it a static rather than a nonstatic member class. If you omit this modifier, each instance will have an extraneous reference to its enclosing instance. Storing this reference costs time and space, and can result in the enclosing instance being retained when it would otherwise be eligible for garbage collection (Item 6). And should you ever need to allocate an instance without an enclosing instance, you’ll be unable to do so, as nonstatic member class instances are required to have an enclosing instance. A common use of private static member classes is to represent components of the object represented by their enclosing class.

那么,这对性能只?

请的注意,这个问题更多的是静态类和OOP 的,而不是Java和C#之间difeerences。

Please note that this question is more about Static classes and OOP, not difeerences between Java and C#.

推荐答案

由JoshuaBloch给出的建议也适用于对C#给C#和建议也适用于Java的(截至因为他们谈论静态类的程度)。

Advice given by JoshuaBloch also applies for c# and advice given for c# also applies for java(upto an extent as they talk about static classes).

为什么要使用静态成员?

Why use static members?


  • 他们并不需要一个实例来调用它们

  • 在C#中,他们使用的通话操作码,这没有按'T需要检查(这是一个微型优化),而不是实例方法(它使用的 callvirt操作码)。我相信类似的事情会出现在java中也。

  • 他们不会阻止GC的东西,如果不使用实例。

  • 也没有开销掠过这个引用隐藏的所有方法。

  • They don't need an instance to call them
  • In c# they use call opcode, which doesn't needs to check for null(which is a micro optimization) as opposed to instance methods(which uses callvirt opcode). I believe similar thing will be there in java also.
  • They don't prevent something from GC if instance is not used.
  • Also no overhead of passing this reference to all the methods hidden.
  • 如果您在使用来 ReSharper的为Visual Studio的生产力工具,它会给出同样的意见,JoshuaBloch对Java给予,在C#话说, 方法可以做成静态 的这是在给定链路有道理的。

    If you are used to Resharper productivity tool for visual studio, It will give the same advice as JoshuaBloch given for java, in c# saying that Method can be made static which is justified in the given link.

    为什么不使用静态成员?

    Why not use static members?


    • 他们是不可测试(容易)。

    • 他们无法实现接口成员。

    • 它们无法通过依赖注入注入。

    • 他们不参加多态性(这是非常必要的面向对象的语言)。

    • 在C#中,静态类不能被传来传去作为参考。

    • They are not testable(easily).
    • They can't implement interface member.
    • They can't be injected via Dependency Injection.
    • They don't participate in polymorphism (which is very much needed in object oriented languages).
    • In c#, static classes can't be passed around as references.
    • 所以,这两个建议都好,如果你了解他们,他们申请这两种语言。使用他们时,他们是合适的,避免他们时,他们都没有。

      So, both advices are good if you understand them and they apply for both the languages. Use them when they are appropriate and avoid them when they are not.

      这篇关于为什么最佳实践的OOP静态类有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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