| |
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();
}
}
}
|
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.
|