views:

474

answers:

2

I'm thinking of implementing the state design pattern on an ASP .NET Webform.

The state would be determined by the query string passed to the page. Based on the state of the page, any action would call the method on the concrete implementation.

What I am trying to achieve is a page that can handle any number of different implementations of the same general functionality.

My questions are: Will this work? Does this sound like the right approach?

+2  A: 

Using a state pattern is an approach you can take for this, but honestly what your describing is part of what the MVC framework was designed to accomplish.

Edit:
MVP/MVC

Since the MVC Framework isn't an option then I would take a look at Model View Presenter pattern (MVP) with either the passive view approach or superviser approach as described here: http://www.martinfowler.com/eaaDev/SupervisingPresenter.html

We found that the passive view approach worked with a little adaptation for our legacy code to work out good for us.

Edit: Patterns:

In that case then which pattern you choose really depends upon what the business needs are.

State pattern:

State pattern is typically used for when you need to change the behavior of an object based upon its current state or the state of a relation to the object. A common usage of this pattern is with games when the behavior of the object depends upon which mouse cursor button is pressed.

http://en.wikipedia.org/wiki/State_pattern

Strategy pattern:

This pattern is good for when you need different implementation based upon a configuration. For example say you are defining an email system and you need to have a different implimentation based upon which email provider is being used to send the email.

http://en.wikipedia.org/wiki/Strategy_pattern

So State pattern could definetly be the right direction it just comes down to what the objective is and what behavior's your trying to meet.

What you'll often find with patterns is that they work well with eachother and you'll use multiple patterns in conjuction with eachother.

David Yancey
MVC isn't an option for me, I need to fit this in to a legacy code base.
Dan
What different type of implimentations are you looking at? Are you looking at more from a presenter view?
David Yancey
I agree with David, any sort of MVP/MVC pattern, or deviation of that pattern, is probably the way you should go.
Jason Heine
Not looking to change the presentation - presentation will not change depending on state, only the functionality for a specific type of action(s).
Dan
State pattern could be the right direction it just depends on the business need. I've edited my original answer again to describe a couple of patterns and common usages of them.
David Yancey
Is there a good way to implement MVP with Webforms? Links to examples?
Dan
MVP can be applied to Webforms, Winforms, Mobile, any type of presenter. Its taking the business logic out of the codebehind and binding the logic to the view via a presenter.http://www.c-sharpcorner.com/UploadFile/rmcochran/PassiveView01262008091652AM/PassiveView.aspxHere is a link of usage of MVC (pattern not framework) with Winforms. Keep in mind that MVP / MVC in the pattern description are very similar.
David Yancey
+1: Nice answer
Binary Worrier
Thank you Binary
David Yancey
A: 

I think what you are suggesting would be a sound approach. The only advice I can really offer is to not get hung up on implementing the State Pattern perfectly. I think it would be perfectly acceptable to just have a switch which calls a method based on a query string value.

Rob