Re: Opinions on the Law Of Demeter
From: Roger L. Cauvin (roger_at_deadspam.com)
Date: 11/09/04
- Next message: bazad: "Re: Code browser?"
- Previous message: Laurent Bossavit: "Re: agile/xp question (formal analysis)"
- In reply to: Mark Nicholls: "Re: Opinions on the Law Of Demeter"
- Next in thread: Mark Nicholls: "Re: Opinions on the Law Of Demeter"
- Reply: Mark Nicholls: "Re: Opinions on the Law Of Demeter"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: bazad: "Re: Code browser?"
- Previous message: Laurent Bossavit: "Re: agile/xp question (formal analysis)"
- In reply to: Mark Nicholls: "Re: Opinions on the Law Of Demeter"
- Next in thread: Mark Nicholls: "Re: Opinions on the Law Of Demeter"
- Reply: Mark Nicholls: "Re: Opinions on the Law Of Demeter"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|