Welcome to Mike95.com
Home
WELCOME
ABOUT MIKE95.COM
CONTACT ME


Features
FUNNY JOKES
HUMAN FACTOR


C++
Library Source Code:
CGIPARSE
JSTRING
JVECTOR
JSTACK
JHASHTABLE


COM / ASP
MIKE95 COM SVR
- ASP STACK
- ASP QUEUE
- ASP VECTOR


Tutorials:
TEMPLATES
ALGORITHMS
DESIGN PATTERNS


Sample Work
Internet Based
TASK/BUG TRACKER


Visual C++ / MFC
FLIGHT PATH (C++)
MP3 PLAY LIST


Java
JAVA TALK
BAR GRAPH
WEB CAM


Personal
CONTACT ME
RESUME
PICTURES
TRIPS
Design Patterns
Model View Controler
Allows for loosely coupled interaction with objects.

A presentation layer shouldn't contain code related to the model, while the model shouldn't contain any code related to the presentation. To accomplish this, we use a mediator class for intercepting GUI events/changes from the presentation layer and then pass these events along to the corresponding methods in the model. Since the presentation layer is a representation of the model, the presentation layer registeres for change notifications issued by the model. When the model changes, it fires the change event notifing all registered presentation layers.

To simulate this, lets presume we have a simple list box collecting names through a textbox control with a button for submitting names. We first have to build the model which will maintain the state of the application.

public class Model
{
	public List<string> _names = new List<string>();
	public event EventHandler ModelChanged;
	
	//we can add to the list
	public void Add( string name )
	{
		_names.Add( name );
		
		//fire event
		if( ModelChanged != null )
			ModelChanged( this, EventArgs.Empty );
	}
	
	//we can remove from the list
	public void Remove( string name )
	{
		_names.Remove( name );
		
		//fire event
		if( ModelChanged != null )
			ModelChanged( this, EventArgs.Empty );
	}
	
	//allow a way to get the list of names
	public List<string> CurrentNames
	{
		get	{ return _names; }
	}
}

This simple class above now allows for the simulation of adding and removing names. As the names are changed, the model fires the ModelChanged event. Once a model changes, the viewer is notified of these changes and updates it's view accordingly.

public class View
{
	private Mediator _mediator;
	private Model _model;
	
	public View()
	{
		//need to have a model instance for the view
		_model = new Model();
		
		//create the mediator, informing the mediator
		//which model he will mediating with.
		_mediator = new Mediator( _model );
	
		//when the model changes, we need to update the view
		_model.ModelChanged += new EventHandler( UpdateView );
		
		//register GUI events
		buttonAdd.Click += new EventHandler( Add );
	}
	
	public void UpdateView( object sender, EventArgs args )
	{
		//pseudo code
		Listbox1.Items.Clear();
		
		foreach( string name in _model.CurrentNames )
		{
			Listbox1.Items.Add( name );
		}
	}
	
	public void Add( object sender, EventArgs args )
	{
		_mediator.Add( textbox1.Text );
	}
}

The mediator class is quite simple, it just takes care of interfacing between the presentation and the model. As UI events are captures, they are transferred to the Mediator, in this case via the Add method, so the mediator updates the model accordingly.

public class Mediator
{
	private Model _model;
	
	public Mediator( Model activeModel )
	{
		_model = activeModel;
	}

	public void Add( string name )
	{
		_model.Add( name );
	
	}
}

Here we now have three independent classes each with their own responsiblities.

  • The Visualization interprets model events for proper display
  • The Mediator translates GUI events into model changes.
  • And of course, the model itself.

(c)2024 Mike95.com / Site Disclaimer
Site Meter