Where Have All the Singletons Gone?
Join the DZone community and get the full member experience.Join For Free
in singletons are pathological liars we discussed the problems of having singletons in your code. let’s build on that and answer the question “if i don’t have singletons how do i ensure there is only one instance of x and how do i get x to all of the places it is needed?”
an oo application is a graph of objects. there are three different kinds of graphs i think of when i design an application
- collaborator graph : t graph of objects that would be emitted if you serialized your application. this shows which objects are aware of which others. (through object’s fields)
- construction graph : this graph shows which object created which other ones.
- call graph : this graph shows . a stack-trace would be a single slice through this graph.
if the new operators are mixed with application logic (see: how to think about the new operator ) then the constructor graph and the collaborator graph tend to be one and the same. however, in an application which uses dependency injection the two graphs are completely independent. lets have a closer look at our creditcardprocessor example. suppose this is our collaborator graph which we need to execute a request.
the above shows the application collaborator graph. the letter (s/r) in the corner designates object lifetime; either singleton or request scope. now, just to be clear, there is nothing wrong with having a single instance of a class. the problem arises only when the singleton is available through a global “instance” variable as in singleton.getinstance().
the http request would come to authenticatorpage which would collaborate with authenticator to make sure the user is valid and forward a valid request onto chargepage which would then try to load the user from userrepository and create the credit card transaction which would be processed by credicardprocessor. this in turn would collaborate with offlinequeue to get the work done.
now, in order to have a testable codebase we have to make sure that we don’t mix the object construction with application logic. so all of the above objects should rarely call the new operator (value objects are ok). instead each of the objects above would declare its collaborators in the constructor. authenticatorpage would ask for chargepage and authenticator. chargepage would ask for creditcardprocessor and userrepository. and so on. we have moved the problem of construction elsewhere.
in our tests it is now easy to instantiate the graph of objects and substitute test-doubles for our collaborators. for example if we would like to test the authenticatorpage, we would instantiate a real authenticatorpage with mock chargepage and mock authenticator. we would than assert that a request which comes in causes appropriate calls on authenticator and chargepage only if authentication is successful. if the authenticatorpage were to get a reference to authenticator from global state or by constructing it, we would not be able to replace the authenticator with a test-double. (this is why it is so important not to mix object construction with application logic. in the unit-test what you instantiate is a sub-set of the whole application. hence the instantiation logic has to be separate from application logic! otherwise, it’s a non-starter.)
so now the problem is, how do we construct the graph of objects?
in short we move all of the new operators to a factory. we group all of the objects of similar lifetime into a single factory. in our case all of the singletons end up in applicationfactory and all of the pages end up in requestfactory. the main method of our application instantiates an applicationfactory. when we call build() the applicationfactory in turn instantiates its list of objects (database, offlinequeue, authenticator, userrepository, creditcardprocessor and requestfactory). because each of the objects declares its dependency, the applicationfactory is forced to instantiate the objects in the right order. in our case it must instantiate the database first and than pass the reference to userrepository and offlinequeue. (the code will simply not compile any other way.)
notice that when we create a requestfactory we must pass in references to the authenticator, userrepository and creditcardprocessor. this is because when we call build() on requestfactory it will try to instantiate authenticatorpage which needs the authenticator. so we need to pass the authenticator into the constructor of requestfactory and so on.
at run-time an http request comes in. the servlet has a reference to requestfactory and calls build(). the servlet now has a reference to the authenticatorpage and it can dispatch the request for processing.
important things to notice:
- every object only has references to what it needs directly! no passing around of objects which are not directly needed by the code. there is no global state at all. dependencies are obvious since each object only asks for what it needs.
- if an object needs a reference to a new dependency it simply declares it. this change only affects the corresponding factory, and as a result, it is very isolated.
- all of the new operators end up in the factories; application logic is devoid of new operators.
- you group all of the objects with the same lifetime into a single factory (if the factory gets too big you can break it up into more classes, but you can still think of it as a single factory)
- the problem of “how do i ensure that i only have one of something” is nicely sidestepped. you instantiate only a single applicationfactory in your main, and as a result, you only instantiate a single instance of all of your singletons.
Opinions expressed by DZone contributors are their own.