简体   繁体   English

要怀疑只有一个实例的类

[英]Be suspicious of classes of which there is only one instance

tl;dr -- What does the below quoted paragraph mean? tl; dr - 以下引用的段落是什么意思?

Be suspicious of classes of which there is only one instance. 要怀疑只有一个实例的类。 A single instance might indicate that the design confuses objects with classes. 单个实例可能表示设计将对象与类混淆。 Consider whether you could just create an object instead of a new class. 考虑一下你是否可以创建一个对象而不是一个新类。 Can the variation of the derived class be represented in data rather than as a distinct class? 派生类的变体可以用数据表示而不是作为一个独特的类来表示吗? The Singleton pattern is one notable exception to this guideline. Singleton模式是本指南的一个值得注意的例外。

McConnell, Steve (2004-06-09). 麦康奈尔,史蒂夫(2004-06-09)。 Code Complete (2nd Edition) 代码完成(第2版)

Extended version: 扩大的视野:

I'm currently reading Code Complete , and I'm having trouble understanding the above mentioned paragraph. 我正在阅读Code Complete ,我无法理解上面提到的段落。 For context, it's from Chapter 6 under guidelines for inheritance. 对于上下文,它来自第6章的继承指南。 At first I thought this was advice against using Singletons, but I was obviously proven wrong by the time I got to the end of the paragraph. 起初我认为这是反对使用单身人士的建议,但当我到达段落末尾时,我显然被证明是错误的。

I simply can't grasp what the author is trying to get through my thick skull. 我根本无法理解作者试图穿过厚厚的头骨。 For example, I don't know what he means by design confusing objects with classes, nor what that means in the context of having a class with only one instance. 例如,我不知道设计是否将对象与类混淆是什么意思,也不知道在具有只有一个实例的类的上下文中它意味着什么。 Help! 救命!

The wording is pretty confusing there, but I believe what's meant is that sometimes a novice programmer might create a whole new type just to instantiate one object of it. 这里的措辞非常令人困惑,但我相信这意味着有时新手程序员可能会创建一个全新的类型来实例化它的一个对象。 As a particularly blatant example: 作为一个特别明显的例子:

struct Player1Name
{
    string data;
};

There we could just use string player1_name; 我们可以使用string player1_name; (or even an aggregate for multiple players) without creating a whole new type, hence the confusion of trying to use classes to model what new objects (new instances of existing types) can already do. (或者甚至是多个玩家的聚合)而不创建一个全新的类型,因此尝试使用类来模拟新对象(现有类型的新实例)已经可以做的混淆。

In such a case, a developer might spam the codebase with hundreds of new user-defined data types and possibly massive inheritance hierarchies with no potential for a single class to be reused beyond a single instance for every single new thing he wants to create when the existing classes generally suffice. 在这种情况下,开发人员可能会使用数百种新的用户定义数据类型以及可能的大规模继承层次结构来对代码库进行垃圾邮件处理,而对于他想要创建的每个新事物,单个实例都无法在单个实例之间重复使用。现有的课程通常就足够了。

The real problem is not that the classes are being instantiated once, but that their design is so narrowly applicable as to only be worth instantiating once. 真正的问题不是类被实例化一次,而是它们的设计非常适用,只值得实例化一次。

Classes are generally meant to model a one-to-many relationship with their instances (objects). 类通常用于模拟与其实例(对象)的一对多关系。 They're supposed to be at least somewhat more generally applicable beyond a single instance of that class. 除了该类的单个实例之外,它们应该至少在某种程度上更普遍适用。 Put crudely, a class should model a Dog , not your neighbor's specific pet dog, Spark . 粗略地说,一个班级应该塑造一只Dog ,而不是你邻居的特定宠物狗Spark It's supposed to model a Rectangle , not a precise Rectangle42x87 that is 4.2 meters by 8.7 meters. 它应该模拟一个Rectangle ,而不是一个4.2米乘8.7米的精确Rectangle42x87 If you're designing things to be instantiated one time, you're probably designing them too narrowly and probably have existing things you can use instead. 如果您正在设计要实例化的东西,那么您可能过于狭窄地设计它们,并且可能存在您可以使用的现有东西。

A new data type is typically meant to tackle a class (category) of problems, so to speak, rather than one very precise one that calls for only one instance of that class. 新的数据类型通常用于解决问题的类(类别),可以说,而不是一个只需要该类的一个实例的非常精确的数据类型。 Otherwise your class designs are going to be one-shot deals that are just superficially creating classes all over the place to solve very individual problems with no potential for broader application whatsoever. 否则,你的课程设计将是一次性的交易,只是在表面上创建课程,以解决非常个别的问题,没有任何潜在的更广泛的应用程序。

A singleton is an exception to the rule since it's not utilizing classes in this normal object-oriented kind of way. 单例是规则的一个例外,因为它没有以这种正常的面向对象的方式使用类。 There it's deliberately setting out to create a single instance, lazy constructed, and with a global point of access. 在那里它故意开始创建一个单独的实例,懒惰构造,并具有全局访问点。 So there it's not by some accident and misunderstanding of object-oriented design that the developer created a class designed to be instantiated one time. 因此,开发人员创建了一个旨在实例化一次的类,这不是因为面向对象设计的一些偶然和误解。 It's a very deliberate and conscious design decision, so to speak, rather than a misunderstanding of how to use the tools. 可以这么说,这是一个非常慎重和有意识的设计决策,而不是对如何使用这些工具的误解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM