mercredi 1 avril 2015

Design pattern for interface that determines order of method invocation

I want to create a Java interface with a number methods. But I want the user of the interface to only be able to invoke methods in the sequence or order that I define. For instance buyTicket() should not be called before reserveTicket(). Q: Is there a design pattern or any tips on how to go about this?


I considered:




  • A)



    • Interface is wrapped, only showing the next possible method. Each invocation of a method returns a new operation that can be called after it, and so on.

    • So ReserveTicketOperation has public BuyTicketOperation execute();

    • Then BuyTicketOperation has public RenderTicketOperation execute();




  • B)



    • Use come kind of context state machine that records the position of execution using enums and has a factory for obtaining the next operation.




Any thoughts or suggestions are greatly appreciated. Thanks


Aucun commentaire:

Enregistrer un commentaire