Change in vdsm[master]: Introduce a singleton decorator

smizrahi at redhat.com smizrahi at redhat.com
Tue May 29 22:55:57 UTC 2012


Saggi Mizrahi has posted comments on this change.

Change subject: Introduce a singleton decorator
......................................................................


Patch Set 2:

State is the _instanceLock() field.


Singletons are evil because they are objects without scoping or clear relationships.

major problem with gloablly scoped objects are:
* Side effects -. Just like with globals; functions modifying variables other than their own locals can cause surprises of all sorts; if pass-by-reference is used then aliasing can occur when it isn't expected.
* Indiscriminate access - The programmer cannot prevent sub-procedures from modifying the values of a local variable's procedures.
* Vulnerability - New declarations may be interposed between when a variable is declared in an outer scope and when it is used in an inner scope.
* No overlapping definitions - It is difficult to control shared access to variables especially in multi threaded applications.

If you need a singleton you are usually doing something wrong, cutting corners or having to handle relationships between singletons which require more singletons.

The only downside to banning singletons is parameter bloat but you can pass "context objects" instead of each tiny objects individually. It will usually serve the added benefit of proving the object relationship.

For instance if a method needs the resourceManager, taskManager, and connectionManager, passing the HSM is better because it prevents parameter bloat and stresses that you don't actually want any random combination but the instances related to the context of that HSM thus strengthening the relationship.

More about it from the famous paper "Global Variables Considered Harmful".
http://www.cs.cmu.edu/~Compose/Globalvariable_wulf73.pdf

--
To view, visit http://gerrit.ovirt.org/4839
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: I17c1b7be60a50cdaa731ee88f29d2017ea6c3f2b
Gerrit-PatchSet: 2
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Mark Wu <wudxw at linux.vnet.ibm.com>
Gerrit-Reviewer: Dan Kenigsberg <danken at redhat.com>
Gerrit-Reviewer: Federico Simoncelli <fsimonce at redhat.com>
Gerrit-Reviewer: Mark Wu <wudxw at linux.vnet.ibm.com>
Gerrit-Reviewer: Royce Lv <lvroyce at linux.vnet.ibm.com>
Gerrit-Reviewer: Saggi Mizrahi <smizrahi at redhat.com>
Gerrit-Reviewer: Shu Ming <shuming at linux.vnet.ibm.com>
Gerrit-Reviewer: Xu He Jie <xuhj at linux.vnet.ibm.com>


More information about the vdsm-patches mailing list