How to design state which is used in a Map
Here is a dumbed down example of what I am trying to achieve. Let's say we have some sort of application coming from a user. This application can be in different states.
Let's say that our Application can be in these different states:
Submitted | Accepted | Confirmed | Processed | Finished
We can think of Application as a
sealed trait and the states are all
case classes which have some different information (compared to each other)
Let's say that we have some sort of state machine which describes the workflow of our application processing. There is some initial state which has
Map[UserId, Submitted]. Here we don't use
Application but restrict ourselves to
Submitted type only, because well in our initial state there are no other possible types of application.
And now for the actual question. Let's say that we validate submitted applications and change our state to
ConfirmingApplications where we have
Map[UserId, Accepted], but the problem here is that we start our state with applications being
Accepted but we don't move to the next (
ProcessingApplications) until all applications are
How can I approach the design of this case? First thing that I thought was about having two different maps in
ConfirmingApplications state where one map is our input map
Map[UserId, Accepted] and the other one is
Map[UserId, Confirmed]. It seems ok in this small example but what I have is a bigger amount of application types and in one particular state the type of application can change 4 times, should I create four different maps for that?
The other approach is to bring in some other
traits which will somehow mark those application types which are allowed to be in a particular state. For example in our
ConfirmingApplications state we could have
Map[UserId, AcceptedOrConfrimed] where
trait AcceptedOrConfirmed is extended by both
Confirmed application types.
What do you think about these design approaches. Is there any other way to design it? Something better? Something on the same level? Something more type safe?
Submitted December 16, 2017 at 06:44PM by faorien
via reddit http://ift.tt/2jbeKfH