mardi 19 mai 2015

alternative to singleton in case of frequent reference

Very often singletons are a bad thing (e.g. see here and here) and I want to avoid them. A typical use case for singletons I don't have an alternative for yet is when you have multiple objects which have to reference some sort of parent/global object.

Example 1: you have some sort of a tree and every leaf-object has to know the root object. (which is bad design but it's just for the example)

Example 2: you have a 'global' configuration object typically accessed like global_config::get_instance()->get_item(name)

Question: for the given examples - are there any alternative concepts to singletons apart from extensive dependency passing and per-instance-storing (aka Dependancy Injection)?

Aucun commentaire:

Enregistrer un commentaire