Be suspicious of classes with only one instance

tl; dr - What does the lower quoted paragraph mean?

Be suspicious of classes that only have one instance. a single instance may indicate that the design is confusing objects with classes. Consider if you can just create an object instead of a new class. Can a variation of a derived class be represented in data and not as a separate class? The Singleton pattern is one notable exception to this tutorial.

McConnell, Steve (2004-06-09). Code Complete (2nd Edition)

Extended version:

I am currently reading Code Complete and I am having trouble understanding the above paragraph. For context, this is from chapter 6 according to the guidelines for inheritance. At first I thought this was a tip against using Singletons, but I was clearly wrong by the time I got to the end of the paragraph.

I just can't figure out that the author is trying to get through my fat skull. For example, I don't know what it means by confusing objects with classes, or what it means in the context of having a class with only one instance. Help!

+3


source to share


1 answer


The wording here is rather confusing, but I believe it means that sometimes a novice programmer can create a completely new type in order to instantiate a single object. As a particularly illustrative example:

struct Player1Name
{
    string data;
};

      

There we could just use string player1_name;

(or even a multiplayer aggregate) without creating an entirely new type, so it's confusing trying to use classes to model what new objects (new instances of existing types) can already do.

In such a case, a developer could spam a codebase with hundreds of new custom datatypes and possibly massive inheritance hierarchies without the ability to reuse one class outside of one instance for every new thing he wants to create when the existing classes are generally sufficient.



The real problem is not that classes are instantiated once, but their design is so narrowly applicable that it is only worth instantiating once.

Classes are usually designed to model a one-to-many relationship with their instances (objects). They should be at least more widely applicable outside of a single instance of this class. Roughly speaking, the class should simulate Dog

and not your neighboring dog Spark

. He had to simulate Rectangle

, not exact Rectangle42x87

, which is 4.2 meters by 8.7 meters. If you are developing objects that need to be created once, you are probably designing them too narrowly and probably have existing things that you can use instead.

The new datatype is usually designed to solve a class (category) of problems, so to speak, rather than one very precise one that requires only one instance of that class. Otherwise, your class projects will be one-off deals that just superficially create classes all over the place to solve very individual problems with no scope for wider application.

The syntax is an exception to the rule because it does not use classes in this common object-oriented fashion. There it is deliberately configured to create a single instance, lazy build and global hotspot. Thus, it is no coincidence and misunderstanding of object oriented design that a developer created a class designed to be instantiated once. This is a very deliberate and conscious design decision, so to speak, and not a misunderstanding of how to use the tools.

+4


source







All Articles