R
Roedy Green
Idempotent, indempotent: Refers to an operation that produces the same
results no matter how many times it is performed.
see http://mindprod.com/jgloss/idempotent.html
Idempotent, indempotent: Refers to an operation that produces the same
results no matter how many times it is performed.
jlowery05 said:Let's see if I got this right:
Monostate -- multiple instances, mostly shared data (can have
instance-specific data)
Singleton -- single instance, shared data (no instance specific data)
I would agree Monostate is generally preferrable (and I'll start using
it), but in cases where you have many members with default values,
statics just don't give you control over when initialization occurs.
I
may not want to initialize a slew of object members if I never use them
in a particular execution path; it could be that Monostate statics
would get initialized by the JVM even if a logic path never
instantiated one.
Ed said:I liked the quote in some article I've just read about it, "I find this
to be a delightfully twisted pattern. No matter how many instances of
MonoState you create, they all behave as though they were a single
object."
Interesting ...
Patrick said:What about the Flyweight pattern requires the use of a Singleton
exposing a getInstance() method?
Ed said:Thanks, Patrick, for that reference to the MonoState pattern; I have't
seen that pattern before.
I liked the quote in some article I've just read about it, "I find this
to be a delightfully twisted pattern. No matter how many instances of
MonoState you create, they all behave as though they were a single
object."
Interesting ...
Thomas said:I can think of different words to describe it. "Pointless", perhaps. Or
"perverse".
Prefer static creation methods over naked constructors. To use a
constructor where you don't actually mean it... lost for words.
Andrew said:Flyweight is completely different to singleton/monostate
Alex said:Nothing at all. But I'm interested in what your alternative method(s)
would be of ensuring re-use of existing instances that are appropriate.
You might propose a factory that returns instances (and caches
references to already created instance for possible re-use). But then
what is the essential difference between knowing that you must use
getInstance() for a singleton class and knowing that you shouldn't
create instances of an object directly, but use the factory instead?
Both require special knowledge outside of the normal constructor mechanism.
Patrick said:I agree, but would add that another problem with the
getInstance() method is that it exposes too much about the
implementation of the class. Clients of the class shouldn't care if
there is exactly one instance, one instance per thread, multiple
instances, or a clerk named Edna behind the scenes.
jlowery05 said:Let's see if I got this right:
Monostate -- multiple instances, mostly shared data (can have
instance-specific data)
Singleton -- single instance, shared data (no instance specific data)
I would agree Monostate is generally preferrable (and I'll start using
it), but in cases where you have many members with default values,
statics just don't give you control over when initialization occurs. I
may not want to initialize a slew of object members if I never use them
in a particular execution path; it could be that Monostate statics
would get initialized by the JVM even if a logic path never
instantiated one.
I think JVMs may be smarter about when to initialize statics nowadays,
so perhaps the point is moot.
Alex Hunsley said:Nothing at all. But I'm interested in what your alternative
method(s) would be of ensuring re-use of existing instances that are
appropriate.
Alex Hunsley said:I still don't see the attraction of MonoState. It seems purposefully
wasteful - potentially loads of instances, all behaving the same,
the same state.
Why bother? So you can just always call a normal constructor and not
have to worry about calling getInstance or a factory method
somewhere else? It's a trade-off, certainly.
Alex Hunsley said:I know. What I am pointing out is that when you use a pattern like
flyweight, you are using it because you have finely grained classes
and want to use common instances of these classes in different
object compositions. MonoState would offer many different instances
that share state, which is still not ideal for flyweight - the idea
of flyweight is that the object(s) contain compositions share
references to the same object. Not to many different instances: even
though those instances may share state via MonoState, it's still
wasteful...
John C. Bollinger said:Oooh! I vote for Edna!
Ed said:I liked the quote in some article I've just read about it, "I find this
to be a delightfully twisted pattern. No matter how many instances of
MonoState you create, they all behave as though they were a single
object."
Patrick said:I would use a Singleton, but I wouldn't expose that design
decision to users of the class. That means that the interface would
not expose a getInstance() method. The implementation of the class
might look like a MonoState or it might use the envelope-letter
technique. Clients would simply instantiate an instance of the class.
The important point is that the code of clients of the class is
not littered with getInstance() calls -- the Singleton is used like
any other class. This not only keeps the code cleaner, it decouples
the clients from the Singleton so that it could be changed to a
ThreadSingleton or even a non-Singleton in the future with no impact
to existing clients.
Andrew McDonagh said:Flyweight is completely different to singleton/monostate
Alex Hunsley said:I'm completely with you on this one. Being able to use a normal
constructor to construct an object that has only static members seems,
well, just silly.
I don't think the pattern name MonoState is very well chosen either - it
sounds like it means "one state", when in fact a MonoState object can
have any amount of states
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.