Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A Design Pattern Question

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to distinguish the State Manager in the State pattern from the Factory Pattern? Or they are essentially the same?
Another question is which feature of Java servers represent the State pattern?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First Factory is a creational pattern, whereas State Pattern in a behavioral pattern. In state there are classes that all implement the same interface, and those classes represent the Objects in different state, so that each behave differently.
In the GOF book, it gives an example of a TCP Connection Object. This Object could be in three different states. Opening, Closing, or Established. So the interface has three methods, one for each state. Then there are three objects that implement this interface, each with different implementations of the three methods. Because they should act differently depending on the state of the connection.
Now the Factory pattern simply creates Objects, there is no interface for state. You pass a parameter it simple creates an Object, there is no mention of State in the factory pattern. If you see states then you know you are talking about the State pattern.
Mark
 
Rufus BugleWeed
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been studying patterns lately. I paused when I realized it's the factory method pattern.
Patterns in Java Vol 1 by Grand from Wiley does a nice job explaining state.
In the Pet Shop there's a class called StateMachine. I have not looked at the source for the class, yet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic