Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A SetState() method would have advantages in extending orders with further states, as well as instrumenting changes -- but I'd not recommend it. The <a href="http://en.wikipedia.org/wiki/State_pattern" rel="nofollow noreferrer">State pattern</a> is about collecting behaviours specific to distinct states in separate classes, not about how to present stateful interfaces to other classes. </p> <p>For Orders, think about the business events which come naturally (e.g. Confirmation, Acknowledgement, Shipping Notice, Shipping, Invoice &amp;c) and design an explicit interface around them. Exactly how you design the interface depends on how your application logic is structured, and how it is used from other layers. The classic answer is to define abstract methods for each business event (e.g. Confirm(), Acknowledge(), ShipDateChanged()). If you're using e.g. C# you might decide to go with incoming and outgoing events from your Order objects. Or you could try try some mixture or combination thereof. The main point is that a SetOrderState() interface isn't very descriptive, and is likely to lead to a clumsy implementation (large methods in each of your OrderState classes). </p> <p>Then again, a SetState() method internal to your classes (called from each of your specific methods or events) could be OK if you don't have a lot of code around the different state changes: but I wouldn't expose that as an external interface. The drawback is that you may get some overlap between the methods of your internal IOrderState interface and the externally exposed Order interface. </p> <p>This is a judgement call, but if I were you, I'd go with your instinct not to expose the details of your State implementation to clients. Code that uses your Order class should be readable and comprehensible. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload