Damon Julian
Damon Julian

Reputation: 3989

modelling status of an Order

What is the best way to model the status of an Order?Currently I am doing it the dirty way,by hardcoding like below

class Order{
   ...
   String orderStatus;
   ...

   public Order(){
     ...
     orderStatus = "pending";
   }
}

Later on when the status is changed to say confirmed,I would

myorder.setOrderStatus("confirmed");

But,I begin to smell it is not the right way..What should be the correct way of modelling it?Should I use Enumerations?..

Upvotes: 1

Views: 522

Answers (3)

Bruce
Bruce

Reputation: 1766

enum is good for the situation that changes very infrequently. But it will very be very convenient to change is you keep them in datebase.

Upvotes: 0

brain
brain

Reputation: 5546

Using an Enum would be a lot better than using random strings yes.

I would look into the State design pattern, otherwise I think you might well end up with lots of code that is conditional on the current state variable which is also pretty smelly.

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160261

An enum and a state machine with well-defined transitions.

Upvotes: 1

Related Questions