6

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.