Friday, 10 January 2014

Visitor Design pattern - Implementation [Kids]



Click here to watch in Youtube : https://www.youtube.com/watch?v=P8ScJp-p1So

Click the below Image to Enlarge
Visitor Design pattern - Implementation [Kids]


Visitor Design pattern - Implementation [Kids] - Class Diagram































Visitable.java


public interface Visitable
{
public void accept(Visitor visitor);
}

Child.java

public class Child implements Visitable
{

private String name;

private String healthStatus;

public Child( String name )
{
super();
this.name = name;
}

public String getName()
{
return name;
}

public String getHealthStatus()
{
return healthStatus;
}

public void setHealthStatus( String healthStatus )
{
this.healthStatus = healthStatus;
}

@Override
public void accept( Visitor visitor )
{
visitor.visit(this);
}

}

Visitor.java

public interface Visitor
{
public void visit(Visitable visitable);
}

ChildSpecialistDoctor.java

public class ChildSpecialistDoctor implements Visitor
{

private String name;

public ChildSpecialistDoctor( String name )
{
super();
this.name = name;
}

public String getName()
{
return name;
}

@Override
public void visit( Visitable visitable )
{
Child child = (Child) visitable;
child.setHealthStatus("Bad");
System.out.println("ChildSpecialistDoctor: '" + this.getName() + "' did the checkup of the child: '"
               + child.getName()
               + "' and found health is not good so updated the health status as 'Bad'\n");

}

}

School.java

import java.util.ArrayList;

public class School
{

static ArrayList<Child> childList;
static
{
childList = new ArrayList<Child>();

Child john = new Child("John");
Child steve = new Child("Steve");
Child rohit = new Child("Rohit");
Child sonu = new Child("Sonu");

childList.add(john);
childList.add(steve);
childList.add(rohit);
childList.add(sonu);
}
public static void doHealthCheckup()
{
ChildSpecialistDoctor childSpecialistDoctor = new ChildSpecialistDoctor("Dr.Smith");
for( Child child : childList )
                {
               child.accept(childSpecialistDoctor);
                }
}

}

Client.java

public class Client
{

public static void main( String[] args )
{
School.doHealthCheckup();
}

}

Output

ChildSpecialistDoctor: 'Dr.Smith' did the checkup of the child: 'John' and found health is not good so updated the health status as 'Bad'

ChildSpecialistDoctor: 'Dr.Smith' did the checkup of the child: 'Steve' and found health is not good so updated the health status as 'Bad'

ChildSpecialistDoctor: 'Dr.Smith' did the checkup of the child: 'Rohit' and found health is not good so updated the health status as 'Bad'

ChildSpecialistDoctor: 'Dr.Smith' did the checkup of the child: 'Sonu' and found health is not good so updated the health status as 'Bad'


See also:

  • Visitor Design pattern - Introduction
  • Visitor Design pattern - Real time Example [Kids]
  • Visitor Design pattern - Real time Example [Products]
  • Visitor Design pattern - Class Diagram
  • Visitor Design pattern - Implementation [Products]
  • Visitor Design pattern - KeyPoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge