Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What pattern solves this?

 
T Ray
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have an application where a certain object is used by many JSPs or Servlets. While using objects, its hard to keep track of the status of that object. Because of that, JSPs and Servlets end up using that objects when its state is not valid to be referenced and throws expception. is there any Pattern that might help keep track the state of objects? do i necessarily use a Pattern in situation like this? I would appreciate if you could provide some suggestion in regards to how to deal with this situation. Thanks.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to tell from your rather abstract problem description, but you might want to take a look at the Null Object pattern.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you mentioned state, look at state pattern. The state-sensitive object can change its behavior based on its state. A method might return computed results in StateA, throw an exception in StateB and return a default value in StateC.

Ilja's null object suggestion might be very similar if you're getting the state-sensitive object from a factory ... you might get back a fully functional object or one that just returns harmless values.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a pattern, but there is something that solves this: use of the "object creation is initialization" paradigm. If you always have the constructor or factory function return an object that is already initialized and ready to use, and all of the object's public methods continue to leave it in a state that's ready to use, that solves your problem.

In formal terms, object creation should set up the object's invariant, and all public methods should continue to maintain the invariant.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this book "Professional SCWCD Certification". There is "Designing and Developing Thread-Safe Servlets" chapter. I suspect that your application may suffer from these kind of problems. In short there might be synchronization problems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic