Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern for steping through a survey

Status
Not open for further replies.

SharkTooth

Programmer
Aug 12, 2004
29
US
Hi, I'm looking for a nice OOP pattern to handle the counter for stepping through a survey. As the user progresses through the survey decisions are made to determine what page the user sees next. I have to indicate at the top of each page what step the user in on, like: 1 of 6, 2 of 6... Since some of the answers will send the user to a different number of pages the current step will need to be adjusted. So a user may start on page 2 of 6 but because of the answers to the survey they skip step 3 of 6 and go right to 4 of 6.

I can do all of this with inline code on each page. Does anyone have a better idea on how to handle this and keep track of the steps? Is there a pattern that fits this? I thought I would use an object and pass it around in a session. This is the class I started to build.

Code:
public class Stepper{

		int _start=0, _end=0, _current=0;
		
		public Stepper(int start, int end, int current){
			_start=start;
			_end=end;
			_current=current;
		}

		public int Start{
			get{return _start;}
			set{_start = value;}
		}

		public int End{
			get{return _end;}
			set{_end = value;}
		}

		public int Current{
			get{return _current;}
			set{_current = value;}
		}

		public void AddStepEnd(){
			_end++;
		}

		public void SubtractStepEnd(){
			_end--;
		}

		public void AddStepCurrent(){
			_current++;
		}

		public void SubtractStepCurrent(){
			_current--;
		}
	
	}

Thanks for your thoughts.
 
The State pattern is the one you want. Basically you subclass the 'states' that your survey can be in. At the end of each step the state either changes or remains the same. It sounds more complex than it is - essentially you are trading all the nested IF statements that handle all the possible combinations for a set of subclasses. If you have a patterns book handy, check it out. If not Google 'state pattern' for a whole slew of examples.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top