lundi 6 août 2018

Java - how would you go about persisting an object which implements the State Design Pattern?

I have not really found a useful answer on here, I hope someone can help.
I am fairly new to Java and OOP, please be patient :-)
I am currently making a courier service webpage - a customer can give up an order - the order's state can be changed by a courier (- new, accepted, inDelivery, delivered, canceled). I am using the state design pattern to depict this, as it made the most sense to me.
I would like to save all the orders in a relational database (I am using H2 and Hibernate/JPA) - but I can't figure out how to get the state back when loading the object.
Two methods come to mind:

  1. Create a table for every single state and save the object in the table corresponding to its state (I can't find out how (if it's even possible) to realize that).
  2. Give the order class an attribute which corresponds to the state it's in - and then set the internal state of the object equal to the relevant state when loading the object again. But wouldn't that destroy the purpose of the state design pattern? Isn't the target to minimize if/else-statements and switch-walls?

What should I do? Is there a better way to solve this?

Any help is highly appreciated :-)
Kind regards

Aucun commentaire:

Enregistrer un commentaire