Quantcast
Viewing all articles
Browse latest Browse all 11

Answer by CodeART for Alternatives to the singleton pattern

How about using IoC container? With some thought you can end up with something on the lines of:

Dependency.Resolve<IFoo>(); 

You would have to specify which implementation of IFoo to use at a startup, but this is a one off configuration which you can easily replace later. Lifetime of a resolved instance can be normally controlled by an IoC container.

Static singleton void method could be replaced with:

Dependency.Resolve<IFoo>().DoSomething();

Static singleton getter method could be replaced with:

var result = Dependency.Resolve<IFoo>().GetFoo();

Example is relevant to .NET, but I'm certain very similar can be achieved in other languages.


Viewing all articles
Browse latest Browse all 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>