Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor
SoftPilot.2000 - Software Outsourcing Ukraine. Articles. Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor
SoftPilot.2000 - Software Outsourcing Ukraine. Articles. Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor
SoftPilot.2000 - Software Outsourcing Ukraine. Articles. Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor
Software Outsourcing Ukraine : softPilot.2000 - Site Map Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor
Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet, ASP.NET. Behavioral Patterns. Visitor - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services
SoftPilot.2000 : IT Outsourcing Ukraine - Home SoftPilot.2000 : Software Outsourcing Ukraine - About SoftPilot.2000 : Software Outsourcing Ukraine - Company Profile Software Development Ukraine : SoftPilot.2000 - Case Study SoftPilot.2000 : Software Outsourcing Ukraine - Services SoftPilot.2000 : Software Outsourcing Ukraine - Partners SoftPilot.2000 : Software Outsourcing Ukraine - Contacts SoftPilot.2000 : Software Outsourcing Ukraine - E-mail SoftPilot.2000 : Software Outsourcing to Ukraine

SoftPilot.2000 - Software Outsourcing Ukraine.

Software development articles about design patterns implementation in C#.

Behavioral Patterns.




Design Pattern Visitor

Not far from here we've been talking about one of the methods to lower coupling between classes. Design pattern mediator was one of the right ways. Today we'll talking about another way, which is known as "Design Pattern Visitor".

For example, we have some storehouse class, which is the collection of the goods. This classes hierarchy is almost the same to our Prototype design pattern example. The question is: "How many operations needs to be implemented with the goods? Do you have well determinated use-cases for all of them beforehand?". For the moment, for example, wee need to print the tiny "Print Info Report" to A4 paper. But, after the month of development the list of reports required will grow like the mushrooms after the rain. In short, we needs to separate the operations from the data. It is obvious. And Design Pattern Visitor will guide us this way.

In turn to implement such behavior we should, implement two hierarchies of the classes: the "goods", which are our data, and the "visitors", which are our operations. The "goods" will have the "Operate" method which send the goods objects references to the Visitor.

Let's try to implement this in C#.

 

   
  Visitor Design Pattern
C# implementation:

using System;
using System.Collections;

namespace template_visitor
{
	//----------------------------------------------------
	// "Visitor"
	abstract class Visitor
	{
		// Methods
		abstract public void Process( GoodsProto gp );
	}
	//----------------------------------------------------
	// "ConcreteVisitor1"
	class PrintInfoVisitor : Visitor
	{
		// Methods
		public override void Process( GoodsProto gp )
		{
			Goods g = ((Goods)gp);
  			Console.WriteLine( "Print Info  Visitor: "+
				"{0} has prise: {1:C}", 
				g.Title, g.Prise );
		}
	}
	//----------------------------------------------------
	// "ConcreteVisitor2"
	class RisePriseVisitor : Visitor
	{
		public override void Process( GoodsProto gp )
		{
			Goods g = ((Goods)gp);
			// Provide 3 extra vacation days
			g.Prise += 2.5;
			Console.WriteLine( "Rise  Prise Visitor: "+
				"{0}'s prise will be increased by: {1}", 
				g.Title, 2.5 );
		}
	}
	//----------------------------------------------------
	// "Element"
	abstract class GoodsProto
	{
		// Methods
		abstract public void Operate( Visitor visitor );
	}
	//---------------------------------------------------
	// "ConcreteElement"
	class Goods : GoodsProto
	{
		// Fields
		private string		m_title;
		private double		m_prise;
	        
		// Constructor
		public Goods( string title, double prise)
		{
			m_title = title;
			m_prise = prise;
		}
	
		public double Prise
		{
			get { return m_prise; }
			set { m_prise = value; }
		}
		public string Title
		{
			get { return m_title;  }
			set { m_title = value; }
		}
		// Methods
		public override void Operate( Visitor visitor )
		{
			visitor.Process( this );
		}
		
	}

	//-------------------------------------------------
	// "ObjectStructure" - The Goods manager
	class StoreHouse
	{
		public Hashtable m_goods = new Hashtable();

		// Indexers
		public GoodsProto this[ string name ]
		{
			get{ return (GoodsProto)m_goods[ name ]; }
			set{ m_goods.Add( name, value ); }
		}
		public void Operate( Visitor visitor )
		{
			foreach( Goods g in m_goods )
				g.Operate( visitor );
		}
	}
	//-------------------------------------------------
	/// 
	///   TemplateVisitorExample test
	/// 
	public class TemplateVisitorExample
	{
		public static void Main( string[] args )
		{
			// Goods storehouse collection
			StoreHouse h = new StoreHouse();
			// Initialize with standard goods
			h[ "mobile motorola" ] = 
				new Goods( "mobile motorola", 20.1);
			h[ "mobile nokia" ] = 
				new Goods( "mobile nokia", 17.2);


			// Goods within the storehouse are visited
			foreach (DictionaryEntry e in h.m_goods)
			{
				GoodsProto gp  = (GoodsProto) e.Value;
				gp.Operate(new PrintInfoVisitor());
				gp.Operate(new RisePriseVisitor());
				gp.Operate(new PrintInfoVisitor());
			}
		
			Console.Read();
		}
	}
}

Design Patterns. Visitor. C#.



The results are below:


Print Info Visitor: mobile motorola has prise: 20,10.
Rise Prise Visitor: mobile motorola's prise will be increased by: 2,5
Print Info Visitor: mobile motorola has prise: 22,60.
Print Info Visitor: mobile nokia has prise: 17,20.
Rise Prise Visitor: mobile nokia's prise will be increased by: 2,5
Print Info Visitor: mobile nokia has prise: 19,70.

Link Partners:

  • Dating Software - MojoScripts online dating software.
  • broadband cable Offering the detailed list of isp on the net.
  • dsl providers Getting more information about isp is no more a problem.
  • DSL The maximum you can get about the working of isp is right here on connectedinternet.net.
  • Data Recovery Manchester Professional data recovery services from failed hard disk drives.On site and in-lab services available.
  • Testking Testking Affiliate Programs. Earn money with Testking
  • Testking Testking Forum. Discuss all that you need
  • Testking Real-testking is the ultimate source for providing the best exam questions
  • Testking Testking Guaranteed Products for 100% sure success
  • Testking Real Testking Exam questions are present and available
  • Testking Thousands of Testking exams to choose from. Success Guaranteed
  • Testking Testking Certified is the best source available on the internet for Passing the exams
  • 50-686 - Testking Cisco CCNA training materials and practice exams are the perfect supplement to your existing CCNA Exam education
  • wireless internet service providers
    Specializing in wireless network, wireless plan and wireless internet.



 
   
(C) SoftPilot.2000 All Rights Reserved. 2000 - 2006