question about monostate pattern
hi,
i've been reading up on the monostate pattern, but theres one thing i dont understand. what is the point in even instantiating an object at all? why not just make the functions static too?
thanks for any help.
It's a matter of perspective. The objects that use a monostate do not know it's a monostate. Thus allowing you to later change its implementation.
In theory, by having the instantiated objects from the beginning, if you later change things so that it *isn't* a Monostate, you don't have to go around all the rest of the code changing static method calls to instantiate objects and make object method calls.
In practice, when you determine that you want different objects of the class, you're probably going to have to go around anyway to change *how* the objects are instantiated.
That is: if you cached stuff before to avoid writing "new Foo()." all over the place, then that caching may no longer be valid; conversely, you may need certain instances to be the same after the change, whereas it didn't matter before if you made a new Foo() each time (because of the Monostate-ness).
In practice, when you determine that you want different objects of the class, you're probably going to have to go around anyway to change *how* the objects are instantiated.
That is: if you cached stuff before to avoid writing "new Foo()." all over the place, then that caching may no longer be valid; conversely, you may need certain instances to be the same after the change, whereas it didn't matter before if you made a new Foo() each time (because of the Monostate-ness).
Actually Zahlman, they shouldn't know that it is a monostate. If they do, then you have defeated the entire point of using a monostate and should just go ahead and straight out use a singleton.
They shouldn't know, indeed, but I don't see what policy you use, in general, for creating the instances such that the code is resistant to a change away from (or towards?) Monostate-ness.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement