I am learning the State Design Pattern. I am using C# ASP.Net (not sure if the language is relevant).
Basically, my object has states A, B and C, and also actions to move it between different states: ActionAToB
, ActionBtoA
and ActionAtoC
.
What I am trying to do is tie this object to a screen, and have buttons trigger these actions, but the issue is: How do I only show the buttons relevant to the state that object is in, and show/hide buttons when the state changes?
I am looking for an elegant solution that goes with the state pattern, or any suggestions that would make the UI easy to maintain.
Use the Observer Pattern as well as the State Pattern. Have your Form class (or whatever you're using for your UI) be an Observer which observes your A-B-C objects. When those objects change their state, they will notify the view and she will update herself accordingly, hiding the appropiate buttons. I blogged about this, see here: Programate Algo Blog
Don't get scared if you see some Spanish, both my blog and I are bilingual :)
Edit: If you want your UI to be easier to mantain, you should also look into a pattern for separating View and Model. There are many, some are MVC, MVP and MVMM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With