Recently I came across a moderately large python codebase with lots of MyClassAbstractFactory
, MyClassManager
, MyClassProxy
, MyClassAdapter
etc. classes.
While on the one hand those names pointed me to research and learn the corresponding patterns, they were not very descriptive of what the class does.
Also, they seem to fall within the forbidden list of words in programming: variable
, process_available_information
, data
, amount
, compute
: overly broad names, that don’t tell us anything about the function when used by themselves.
So should there be CommunicationManager
or rather PortListener
? Or maybe I do not understand the problem at all…?
4
-
AbstractFactory
is indeed a poor choice for a name. There is no way to know what is created by this factory, and when you’ll look for an entity which createsAnimal
s, you’ll never find the corresponding factory by name. -
AnimalAbstractFactory
is not a wise choice neither, since in most languages, it would be redundant with theabstract
keyword in the signature.This being said, there are several good reasons, highlighted by the comments, to actually include
Abstract
in the name: not only there are several contexts where you don’t have the full signature, but just the name, but also, keepingAnimalFactory
for an interface may be a wise choice (unless, unfortunately, the convention of the language/framework is to prefix interfaces withI
). -
AnimalCreationUtility
would also be a bad choice: if it’s a factory, make things easier for people who will read code, and call it a factory. -
abstract AnimalFactory
is ok. It doesn’t have redundancy, and is clear that it is an abstract factory which delegates the creation of animals to its children.
So yes, including the name of the design pattern is a good idea, but it should be only a part of the name, and shouldn’t be redundant with the other parts of the signature.
7
The whole point of using pattern names in classes is to make it easy to understand what the class does. If you name the class AnimalFactory it’s obvious that the class creates Animal instances. If the name of your class includes a name of a pattern and it does not describe what it does you’ve either chosen a wrong pattern or implemented it incorrectly.
Depends on the specific example. The Builder pattern is almost always best served by naming your class *Builder, while a Singleton doesn’t usually need to be named as such.
If you don’t put the pattern name in your class name, and maybe even if you do, you should generally put a comment in the class which explains that it implements a specific pattern.
1
I think it can work really well. For example:
// Command for retrying card entry with CVN.
public class RetryCardEntryWithCVNCommand { ... }
// Query for getting expired accounts
public class GetExpiredAccountsQuery { ... }
// Decorator for logging exception. Implies that it's an additional
//mechanism for logging exceptions.
public class LogExceptionToDbDecorator { ... }
// Factory for creating account filters
public class AccountFilterFactory { ... }
2