out put :
Instance Id=org.sri.design.ptrn.singleton.Singleton@5b8c5b8c
Instance Id=org.sri.design.ptrn.singleton.Singleton@5b8c5b8c
Instance Id=org.sri.design.ptrn.singleton.Singleton@3a6c3a6c
Instance Id=org.sri.design.ptrn.singleton.Singleton@3b4c3b4c
'Design Patterns for Java' trainer
'Design Patterns for Java' trainer
Thorin Potjes wrote: it is a very simple and powerful pattern, as long as developers are aware of possible consequences.
'Design Patterns for Java' trainer
Pat Farrell wrote:
Thorin Potjes wrote: it is a very simple and powerful pattern, as long as developers are aware of possible consequences.
It is a simplistic and fatally flawed pattern. It appears simple. It is grossly misused. It is rarely justified, and even then, it is no longer simple. There are alternatives that work better and are no additional work.
Thorin Potjes wrote:- the same class can also be used in your unit tests
Pat Farrell wrote:
Thorin Potjes wrote:- the same class can also be used in your unit tests
By definition, if testing class A requires class B to behave a certain way, you have weakened or broken your unit test.
The testable solution is dependency injection
'Design Patterns for Java' trainer
Thorin Potjes wrote:I might as well make Singletons of those classes (so they can be shared between multiple C instances, like a Flyweight pattern).
Pat Farrell wrote:
Thorin Potjes wrote:I might as well make Singletons of those classes (so they can be shared between multiple C instances, like a Flyweight pattern).
No, either you are missing the point, or you are just being argumentative.
I and many other maintain that the Singleton pattern is grossly overused by Java programmers. This is a specific pattern and programming idiom. There is no reason to use this Singleton Pattern in your example. Just define the classes for your states and be done.
Actually, your wording is a bit strange, normally a "state class" has tons of state, the finite state machine is in a specific state when the FMS in using the state class. It is, in essence only the state. You are correct that a normal state in a FSM has no instance variables. But then, while an FSM is a wonderful and useful construct, it often is hard to express cleanly in most OO languages.
In the typical implementation of a set of state classes, each class only has one method, typically called something like transition() or fire() or event() which moves the FSM to the next state, whatever that is.
'Design Patterns for Java' trainer
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|