views:

243

answers:

6

During my last project I noticed, that it is very convenient, to include design patterns names in class names. For example:

  • ContextLazyFactory
  • RunOnceMediator
  • ThirdPartyMediator
  • MyProjectCliFacade
  • BinaryGate

It makes the project easy to read. Additional benefit is that you will not use your own names like "RunOnceManager", "ContextDelayedConstruction", "ThirdPartyInterface", etc. which may have sharp meaning only for the author. On the other hand, I would not like to see classes like vector_container in the STL. How do you think?

My current view on this topic is: classes that are important nodes in class hierarchy should have their design pattern in their name, to emphasis the hierarchy structure and make the project much easier to read.

+7  A: 

It seems natural to me in many cases:

  • Design patterns are named to explain what they do
  • Classes are named to explain what they do

When the simplest way of explaining a class's purpose is in design pattern terms, why not use it?

On the other hand, when the design pattern is mostly incidental then exclude it. For example, a class may happen to be a singleton, but that's not its main purpose in life, so I wouldn't expect to see "Singleton" in the name. Compare that with a factory whose main purpose is to be a factory for other objects - "FooFactory" makes perfect sense.

Jon Skeet
Downvoters: please supply a comment to explain why you disagree with an answer when you downvote.
Jon Skeet
+1 completely agree.
eglasius
@Jon in this case I think it is fair to assume downvoters feel strong about either putting it in the name or not putting it at all, in either case I don't you think can do much with that info (other than get in a long discussion about it).
eglasius
Explaining which one they feel strongly about would be a start though :)
Jon Skeet
A: 

I agree with you. The only thing I'd add (which goes without saying really) is to make sure that the pattern name within the class name does use the actual pattern employed.

I once saw a class called XxxxFactory (or something like that) and it had no resemblence to a factory pattern whatsoever!

mdresser
A: 

I also use the design pattern name in the classnames to increase readability and easy to understand for other people.

Vectors means for me already a collection so no need for _collection. Btw container is not a designpattern.

PoweRoy
A: 

If the resulting name is clear enough, go ahead and use it.

But there can be circumstances that the resulting name is either clumsy or vague. In that case, use the most apropriate name.

Gamecat
+1  A: 

I think using design patterns in names is a great idea. For example in one of my games I have:

GameLevelAbstract
InGameArea
MainMenu

The second two classes (InGameArea and MainMenu) subclass GameLevelAbstract and implement it's pure virtual methods. I can see in my source view that that class is abstract and immediately know it is or isn't what i'm looking for.

Another example is my graphics facade:

GraphicsFacade
OpenGLGraphics
DirectXGraphics

Exactly the same setup as I had above, except that it deals with graphics.

I find using the design pattern in the name to be useful and informative when I am looking for a class. I do believe that some things should not be used in the name such as Singleton, however Flyweight, Factory or Facade are examples of design names I think fit well in the name of a class that implements those patterns.

I think that if you use good class names that help the programmer get a good grasp of what your class does at a glance is a good thing.

I don't think that stating something is a data structure or an algorithm is a good thing in most cases. "LinkedListDataStructure" or "BubbleSortAlgorithm" don't add anything to the programmer's understanding of the class as "LinkedList" is obvious in it's function.

Brock Woolf