I'm currently trying to implement a factory design pattern and somewhat confused as to how this is typically done in C++. I've been referring to this webpage.
So on that webpage, they show an example where they define a factory class called ComputerFactory. This class has a single NewComputer method which is static.
class ComputerFactory { public: static Computer *NewComputer(const std::string &description) { if(description == "laptop") return new Laptop; if(description == "desktop") return new Desktop; return NULL; } }; The fact that NewComputer is static means it's not associated with a single instance of ComputerFactory. Thus it is possible to call this method from your code by writing
ComputerFactor::NewComputer("description"); Now I'm curious as to why this needed to be wrapped within a class. Because it's static and really isn't meant to be associated with any class, it would make more sense to do something like
namespace ComputerFactory { static Computer *NewComputer(const std::string &description) { if(description == "laptop") return new Laptop; if(description == "desktop") return new Desktop; return NULL; } } So my questions:
- Is the namespace function I described a valid alternative?
- Would it ever make sense to define a class that only has static methods? It seems like it would make more sense to use a namespace.
Now I believe there are instances where it would make sense to have static methods (e.g. static 'how many of these classes currently exist'), but this doesn't appear to be a situation where it's necessary. Any thoughts?
Edit: So I just had a thought, the example I listed isn't a 'typical' factory method. In practice, it's possible that the factory function is more complex and requires calling other functions (private class methods). In this case, it would make complete sense to wrap the factory into a class such that you could access subfunctions. Is this why factory methods are typically wrapped into classes?