Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State design pattern User Interface

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.

like image 232
everLearningStudent Avatar asked Nov 13 '22 22:11

everLearningStudent


1 Answers

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.

like image 145
dario_ramos Avatar answered Nov 23 '22 23:11

dario_ramos