It is a common use case to create an activity which returns a particular state for a specific amount of time and then revert back to its original state. For example you want to suspend a particular user on your website for say, 60 seconds (maybe because he’s submitting a form too fast – a strategy a lot of forms support). There are many such use cases possible especially when that application is using huge amount of cache. One way to do it would be with database which is not only highly in-efficient but way too cumbersome. If we could create a data structure that could retain its state for a specific time and then revert back, it would be killer.
I would present such a Boolean data structure that could be used to validate a condition for a specific time and then it would revert back to its original status.
One of the important design decisions has been to make this data structure immutable. One instance would represent just one state. Also, in this implementation I have used constructors rather than static factory method to return state objects, but nothing stops us from having a static factory method for our state objects.
Lets look at our class now:
The code is pretty self-explanatory. The main method gives away its usage pattern. There are several ways to extend this data structure and as we move forward we would try to improve and evolve this data structure.
I would present such a Boolean data structure that could be used to validate a condition for a specific time and then it would revert back to its original status.
One of the important design decisions has been to make this data structure immutable. One instance would represent just one state. Also, in this implementation I have used constructors rather than static factory method to return state objects, but nothing stops us from having a static factory method for our state objects.
Lets look at our class now:
|
The code is pretty self-explanatory. The main method gives away its usage pattern. There are several ways to extend this data structure and as we move forward we would try to improve and evolve this data structure.
No comments:
Post a Comment