user1399653
user1399653

Reputation: 193

State design pattern implementation query

I am developing an application that gets n number of PENDING records from the database and processes the records. The state while processing is "PROCESSING" and would either mark the records as either "ERROR" or "SUCCESS". If all the records are successfully processed, the state needs to be updated to "SUCCESS" in the database. If some of the records failed to process, I need to update those as "ERROR" and insert the reason for error in errorlog table, while updating the remaining as success. I was thinking of implementing this using State design pattern.

My question - I understand that it would make sense to implement it using State design pattern if I am dealing with one record at a time. Would it make sense to implement with State pattern if dealing with bulk records. If not, any alternatives?

Upvotes: 1

Views: 93

Answers (1)

Rob
Rob

Reputation: 11733

This is actually not a good case for the State Pattern. State is about making it easy for an object to behave differently based on the state it's in. For state to apply, you want to have an object that implements a protocol, but implements it differently based on what State it's in. The example they use in The Gang of Four is for a socket class. I used State recently in a case where if a device was in use, I wanted to have it handle certain basic functions differently than if it wasn't. So in this case, what you do is you implement two state handlers (that are implementing the same interface) and you simply swap them when the event arrives saying the device either went into or out of use.

For your case, you need to just model the states of a given object. You should read a bit about state machines, which are making a comeback now with the rise of Reactive Programming.

Found this gentle introduction, which is quite good.

Upvotes: 1

Related Questions