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.
It's hard to tell from your rather abstract problem description, but you might want to take a look at the Null Object pattern.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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.
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.