everLearningStudent
everLearningStudent

Reputation: 1095

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.

Upvotes: 6

Views: 2436

Answers (1)

dario_ramos
dario_ramos

Reputation: 7309

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.

Upvotes: 1

Related Questions