Can I add other methods to the classic Singleton Implementation?

  softwareengineering

I’ve seen Singleton design pattern represented like below. Always with just getInstance() method.
My doubt is: once I get the instance of the object I need, if I would like to modify it don’t I need to add other methods to the Singleton class? Like setField( Type something) ? Or is against the pattern? In this case which is the right way to get a single instance and modify it?

singleton

1

The UML diagrams you find about design patterns usually only illustrate the bare minimum of what you need to have in order to implement the pattern. It usually does not include any “payload”.

Your class which follows the Singleton pattern would of course require some instance methods which actually do something useful.

Firstly, the usual caveat: A singleton is a global variable (especially in languages where everything must be a member of a class). Global variables end up making your program harder to understand. You probably can do whatever you want to do via a singleton by just only newing your class once.

Yes, you are expected to have other members of your particular singleton type. Those are the members that are common to all singletons.

7

LEAVE A COMMENT