Click here to Skip to main content
Click here to Skip to main content

Visitor Design Pattern

, 10 Sep 2009
Rate this:
Please Sign up or sign in to vote.
This article shows a case study about how we use the Visitor pattern in a daycare center reward system.

Background

Again, my favorite place - Elizabeth's daycare center.

From my previous article, I talked about how to use the Strategy Pattern to develop a system for the doctor visiting. In that case, I only focused on the Doctor objects to illustrate how those dynamic actions will be impacted when the strategy object (different doctors) changes.

In this article, I am going to focus on those visitable/Kid objects which are examined by the Doctors.

On Elizabeth's daycare calendar, it shows Dr. WANG (EyeDoctor) gives eye screens on the 15th each month for all the kids, Dr. Fong (SLP - Speech Language Pathologist) gives speech evaluations on the 28th each month. And parents may visit their kids at anytime.

For each visitor's visit, all the related status needs to be updated after the Doctor's visits. EyeDoctor will update EyeStatus only, and SLP updates SpeechStatus as well. Parents will update both EyeStatus and SpeechStatus.

Remember, Kids could be visited by different visitors. It could allow any types of visitors to visit, however, the visitors should know what status needs to be updated for the kid after their visits.

Introduction

The Visitor Pattern could help us to decouple the operation details for a particular visitable object from multiple visitor objects. We isolate the responsibilities in the separate visitor objects(such as EyeDoctor, SLP & Parent) when those objects need to implement their own logic for the visitableObject(Kid).

This article introduces an implementation of how we use the visitor pattern to our daycare center.

Visitor Design Pattern Structure

visitorS.JPG

Class Diagram

VisitorClass.JPG - Click to enlarge image

Implementation Code

Visitor Objects

Visitor

Visitor class is our base abstract class. One abstract public method (Visit) has been declared in here to allow the child class to implement the details. It takes a VisitableObject object as the parameter. It actually provides the ability to the child Visitor class to work around it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace www.askbargains.com
{
    namespace VisitorDesignPattern
    {
        public abstract class Visitor
        {
            abstract public void Visit(VisitableObject akid);
        }
    }
}

EyeDoctor

In my EyeDoctor class, a child Visitor class, it will only update the eyestatus for an incoming visitableObject.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace www.askbargains.com
{
    namespace VisitorDesignPattern
    {
        public class EyeDoctor : Visitor
        {
            //Visit the visitableObject and update the deserved information
            public override void Visit(VisitableObject akid)
            {
                Kid kid = (Kid)akid;
                kid.EyeStatus = "Status updated from EyeDoctor - 
		Message from Dr.WANG, Eye Infections found for " + kid.Name + ". 
		Please schedule an appointment asap";
            }
        }
    }
}

SLP

Same as EyeDoctor, SLP class will only update the Speechstatus for an incoming visitableObject.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace www.askbargains.com
{
    namespace VisitorDesignPattern
    {
        public class SLP : Visitor
        {

            public override void Visit(VisitableObject akid)
            {
                Kid kid = (Kid)akid;
                kid.SpeechStatus = "Status updated from SLP - This is Dr.FONG, " + 
			kid.Name + " did a good job on the speech exam.";
            }
        }
    }
}

Parent

Same as EyeDoctor, Parent class will update both Eyestatus and Speechstatus for an incoming visitableObject.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace www.askbargains.com
{
    namespace VisitorDesignPattern
    {
        public class Parent : Visitor
        {
            public override void Visit(VisitableObject akid)
            {
                Kid kid = (Kid)akid;
                kid.EyeStatus += Environment.NewLine + 
			"Status updated from Parent -- This is " + kid.Name + 
			"'s parent, I updated the EyeStatus.";
                kid.SpeechStatus += Environment.NewLine + 
			"Status updated from Parent -- This is " + kid.Name + 
			"'s parent, I updated the SpeechStatus.";
            }
        }
    }
}

VisitableObject Class

VisitableObject

VisitableObject class is an abstract base class which has one public method(AcceptVisitor). AcceptVisitor method is the key method in our visitor pattern. It takes one Visitor object as the parameter, which will call its Visit function to visit visitableObject itself (an instance of visitableObject) in order to achieve the visit pattern.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace www.askbargains.com
{
    namespace VisitorDesignPattern
    {
        public abstract class VisitableObject
        {
            public void AcceptVisitor(Visitor visitor)
            {
                //allow the incoming visitor to start visiting
                visitor.Visit(this);
            }
        }
    }
}

Kid Class

Kid class in here is a helper class that inherits from the VisitableObject. It holds personal information for each child.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace www.askbargains.com
{
    namespace VisitorDesignPattern
    {
        public class Kid : VisitableObject
        {
            public string Name { get; set; }
            public int Age { get; set; }

            public string EyeStatus { get; set; }
            public string SpeechStatus { get; set; }
        }
    }
}

Client App

From the client side, I create two kids as our visitableObject objects, and add them to the daycare. And I also create a visitor collection, it contains an EyeDoctor, a SLP and a Parent. For each kid, she/he will be visited by each visitor from the visitor collection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using www.askbargains.com.VisitorDesignPattern;

namespace www.askbargains.com
{
    namespace Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<kid> dayCare = new List<kid>();

                //Kid Elizabeth is created
                Kid Elizabeth = new Kid();
                Elizabeth.Name = "Elizabeth";
                Elizabeth.Age = 3;
                //add Elizabeth to daycare
                dayCare.Add(Elizabeth);

                //Kid Aimee is created
                Kid Aimee = new Kid();
                Aimee.Name = "Aimee";
                Aimee.Age = 4;
                //add Aimee to daycare
                dayCare.Add(Aimee);

                //create Visitors
                List<visitor> visitors = new List<visitor>();
                visitors.Add(new EyeDoctor());
                visitors.Add(new SLP());
                visitors.Add(new Parent());

                //start looping all the visitors
                foreach (Visitor visitor in visitors)
                {
                    //kid start to be visited for each visitor
                    foreach (Kid oneKid in dayCare)
                    {
                        oneKid.AcceptVisitor(visitor);
                    }
                }

                //display result
                foreach (Kid oneKid in dayCare)
                {
                    Console.WriteLine("Display Status for " + oneKid.Name);
                    Console.WriteLine(oneKid.EyeStatus);
                    Console.WriteLine(oneKid.SpeechStatus);
                    Console.ReadLine();
                }
                Console.ReadLine();
            }
        }
    }
}

Once we start our client app, you will see all that the correspond status has been updated after each visitor's visiting in the output window. Cool!

VisitorOutput.JPG

Conclusion

From this article, I demonstrated how we can use the Visitor Pattern to achieve the implementation for a Daycare visitor. I also use the daycare center for the Decorator Design pattern in my other article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Allen _ Wang
Architect
United States United States
Sr Software Architect.Microsoft Certified Solutions Developer (MCSD). owner of www.askbargains.com.

Comments and Discussions

 
GeneralAnd yet... PinmemberVitaly Tomilov23-Jun-12 13:08 
GeneralMy vote of 5 PinmemberAkram El Assas10-May-12 3:10 
Good article. Here[^] is a functional implementation sample.
QuestionWhat if... Pinmemberrichard.guzik15-Sep-09 4:11 
AnswerRe: What if... PinmemberSmartAllen15-Sep-09 5:42 
GeneralRe: What if... PinmemberKeithAMS15-Sep-09 6:38 
GeneralRe: What if... Pinmemberstevenlauwers2215-Sep-09 7:01 
GeneralRe: What if... PinmemberKeithAMS15-Sep-09 21:50 
GeneralRe: What if... Pinmemberrichard.guzik15-Sep-09 7:14 
GeneralRe: What if... PinmemberSmartAllen15-Sep-09 7:43 
Generalnice chapter following the previous one Pinmemberuwspstar200910-Sep-09 3:56 
GeneralGood Article - Good Series PinmemberHollerith10-Sep-09 3:26 
GeneralGood article PinmemberJani Giannoudis10-Sep-09 3:09 
GeneralAnother good read. Pinmembersxndave10-Sep-09 1:34 
GeneralMy vote of 2 PinmemberPriyank Bolia9-Sep-09 21:22 
GeneralRe: My vote of 2 PinmemberJakub Mller9-Sep-09 22:11 
GeneralRe: My vote of 2 Pinmembermajee10-Sep-09 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 10 Sep 2009
Article Copyright 2009 by Allen _ Wang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid