0

Using Dart in the client part of a web app where I have an application manager that contains a List of application instances and references to services that are used by each application as in the following diagram

diagram

As you can see the application manager is a singleton, so are the services that it contains which are used by all apps, to me it makes sense because

  • I need to have only one instance of the ApplicationManager and each service
  • Easier to access services without the need to pass references
  • I need to preserve the state of each service
  • Almost all services have a cache

I found ways to avoid having the services as singletons which is by making them private to the library which is a feature in dart, so I initialize the services only once from the application manager, but I can't make the ApplicationsManager private because its at the top most level of the library, I would appreciate your thoughts for a way to avoid having the application manager as a singelton.

3
  • 4
    the way to hell is paved with singleton managers Dec 22, 2014 at 16:30
  • In english the pronoun "I" is always capitalized. Dec 22, 2014 at 19:53
  • @user61852 its alright ,its my 3rd language and i never capitalize usually even in my first language sorry if it bothers you Dec 22, 2014 at 20:00

1 Answer 1

4

i need to have only one instance of the ApplicationManager and each service

Then create one instance. shrug

easier to access services without the need to pass references

Then provide a global variable to access that one instance you created previously. If you don't like global variables for this, you can't like singletons, because singletons share all the same drawbacks, but are somehow more complex. In that case, just pass things along to whatever needs it.

2
  • 3
    Many programmers seem to think Singletons are better than global variables because they have been told that global variables are bad, whereas Singleton is a Design Pattern, and Design Patterns are good, right? ;) Dec 22, 2014 at 16:41
  • Unfortunately you're right, some people don't consider the flip side of the design pattern coin. Luckily though, many people are aware of issues with the singleton. But it's sad to think design patterns are being applied "mindlessly" :/ Dec 22, 2014 at 19:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.