Re: Opinions on the Law Of Demeter

From: Roger L. Cauvin (roger_at_deadspam.com)
Date: 11/09/04


Date: Mon, 8 Nov 2004 17:08:13 -0600


"Mark Nicholls" <Nicholls.Mark@mtvne.com> wrote in message
news:bdf4e330.0411080157.79dde128@posting.google.com...
>>
>>> the point is that I can control the singleton nature
>>> of a product without even telling the client...
>>
>> Actually, the client *does* need to know whether the
>> product is a singleton, or else it can't know whether it
>> can modify the product without affecting other clients.
>
> No. Not to me anyway. Any given client is oblivious of
> the nature of the product in this manner (assuming it was
> created by from a IFactory interface), if it is not 'safe' for
> the singleton to be exposed i.e. there is a possibility of
> contract breach to itself or any client then it should not be
> exposed in that manner.

In general, it will be possible for multiple clients to access the same
singleton. With your CSingletonFactory class, clients accessing the object
through the IFactory interface indeed do not know whether it is a singleton
or a new instance. If the singleton contains mutator methods, then the
client cannot know whether it can safely call mutator methods without
affecting other clients. Therefore, if the singleton contains mutator
methods, there will in general be a possibility of "contract breaches",
multithreading problems, and unexpected side effects.

>> > the problem (for me) with the singleton pattern is
>> > that the notion of being a singelton is a shared
>> > responsibility between client and service.
>>
>> I agree. And that's precisely why the design based
>> on the CSingletonFactory class is flawed. The fact
>> that it violates the Law of Demeter flags it as a
>> suboptimal design that could stand improvement.
>
> ? lets say CSingletonFactory returns a readonly clock
> object ?

The read-only nature of the object would render moot any problems with
shared access to a mutable resource. I wonder, though, why use Abstract
Factory pattern in that case? Instead of passing around an instance of the
IFactory interface, why not just pass around a previously-created instance
of the IProduct instance?

> given the IFactory implementation, I can change the
> implementation of the clock to be a singleton or not, I
> do not need to involve the clients at all, because all
> they create via a IFactory interface. With the singleton
> pattern this is implicit in the interaction between client
> and singleton.
>
> To me with the clock example, there is no problem, and
> I think no violation of LoD (but I am unsure).

I, too, am unsure. A strict literal reading of the LoD seems to entail that
the clock example is a violation. However, perhaps the example doesn't
violate the spirit of the LoD.

-- 
Roger L. Cauvin
nospam_roger@cauvin.org (omit the "nospam_" part)
Cauvin, Inc.
http://www.cauvin-inc.com


Relevant Pages

  • Re: Multi-threaded Worker Objects
    ... your clients call a method available thru this remoteable object ... Singleton does not mean that the calls are single as well. ... If you can elaborate how your remote object return values to ... "Robson Siqueira" wrote: ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Managing multiple users
    ... You could as an example use a WCF service over TPC/IP between the client and service on a LAN. ... You could use a singleton object on the backend, or with WCF, you could set the number of connections to one so that only one client can open/close the connection and consume the service, making other clients wait until the connection is closed and the service becomes available again. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# ServicedComponent Singleton
    ... interop adapter class is installed in COM+ and exposed to non-.net clients. ... Here is a complete sample of how to expose a .NET singleton to COM clients. ... public class MySingleton ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: .NET Remoting SAO & CAO
    ... Ansonsten wird ein SAO Singleton von allen Clients geteilt, während bei CAO ...
    (microsoft.public.de.german.entwickler.dotnet.framework)
  • Re: Synchronizing "pushed" multicasts
    ... Or, do they rely on huge buffers in their clients? ... The buffer just compensates for variations in the transport service. ... if your clock is slightly *faster* than the producer's ... In most audio streaming protocols the data in each packet partially ...
    (comp.arch.embedded)