Originally posted by Sanjeev Arya:
Hi,
OOP experts, including the GoF, often recommend "programming to an interface" for benefits of decoupling and polymorphism. That makes sense. Does that mean it may be useful to refactor the design for some patterns to include an interface on top of abstract classes?
It seems Abstract classes may be useful in patterns such as Template Method, or Singleton (for subclasses). Since C++ does not have explicit interfaces, the GoF often use Abstract classes in their class diagrams for patterns where we'd use interfaces in Java. However, for these 2 patterns at least we would invariably end up having an abstract class in Java too. Hence my question, shall we further include an interface on top of the hierarchy? Can it be an overkill?
Maybe fellow patterners in this forum will throw some light... probably with reference to Singleton or Template Method patterns.
Thanks
Sanjeev
Mark Herschberg, author of The Career Toolkit
https://www.thecareertoolkitbook.com/
Originally posted by Mark Herschberg:
Why dod you need an abstract class for Singleton? I'm not usre I get it.
For a pattern like Singleton, I don't see the need for a new interface. Interfaces are used when multiple, distinct classes (i.e. not with the same lineage) all need to exhibit the same behavior. With the Singleton, there will generally be only one object of that class. It's unlikely that you'll have other classes with similar behaviors, so an extra interface is unnecessary.
However, that is not to say there may not be other patterns of of which your Singleton is a part, which would require an interface. But then the interface would be used for the other pattern and not for the Singleton pattern.
--Mark
hershey@vaultus.com
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Consider Paul's rocket mass heater. |