Saturday, September 28

Think from a visitors perspective for a while ---A person comes from US to visit India ... Visitor want to visit certain places .. He has the list of places he want to visit . So he provide the same list to his travel organizer and ask him to prepare a suitable plan for him to make him travel all the places available in the list.Organizer takes the list and start contacting his site contact persons . He ask his agents at all places visitor want to visit to accept and welcome the visitor and make him Visit the place. So here entire task is divided in to few sub tasks :1. Visitor -- Ask visitor to plan . Visit the places.2. Organizer -- activate his place specific agents 3. Place agents --- Accept the visitor and make him Visit.

Now lets understand this scenario in Object oriented world of java technology . How design pattern deals with this situation.As usual , I would be providing all steps involved in the process in system.out.println statements. Code below can be copied as it is and executed in any java environment . System.out,print statements will print all the steps involved in process on console as well==============================================Visitor class represents the visitor . He creates the list of places to be visited and invoke schedule method on organizer . Before invoking schedule method Visitor creates instance of Organizer and while constructing instance it uses one argument constructor to initiate Organizer instance with a state having list of Place instances.

package incredibleIndia;

import java.util.ArrayList;

import java.util.List;

publicclass Visitor implements IVisotor {

/**

* @param args

*/

publicstaticvoid main(String[] args) {

List places = new ArrayList();

places.add(new IndiaGate());

places.add(new Tajmahal());

places.add(new BirSujra());

places.add(new RedFort());

places.add(new JamaMasjid());

Organizer org = new Organizer(places);

System.out

.println("Visitor ask Organizer to plan for his visit. Visitor provides Organizer the list of places he is interested in ");

This is contract interface which is helping Visitor to visit the places available in the list. Visitor implements this interface and implement are overloaded methods each referring to one single element /place

interface IVisotor {

void visit(IndiaGate indiaGate);

void visit(Tajmahal tajMahal);

void visit(BirSujra birsujra);

void visit(RedFort redFort);

void visit(JamaMasjid jamaMasjid);

}

This is interface for Organizer to implement . It has only schedule method

interface IOrganize {

publicvoid schedule(IVisotor visitor);

}

Organizer takes the list of places in constructor . In schedule method It iterates over Place instances and invoke their accept method to make the places accept the visitor . in accept() method it passes the Visitor as parameter. Each Place instance being iterated here refer to once actual Place / class implementing Place interface

class Organizer implements IOrganize {

Listplaces = new ArrayList();

public Organizer(List places) {

this.places = places;

}

publicvoid schedule(IVisotor visitor) {

System.out

.println("Organizer get through the process to initiate the visit and ask place coordinators to welcomes the visitor ");

for (Place place : places) {

place.accept(visitor);

}

}

}

Interface for place . Has only one method . Accept . This needs to be implemented by all Place classes

interface Place {

void accept(IVisotor visitor);

}

One of the class implementing Place interface and defining the logic of accept method . Accept method will accept the visitor and then on visitor instance invoke visit method and pass the current place instance to the visitor . Visitor can use this instance to visit the place

class IndiaGate implements Place {

publicvoid accept(IVisotor visitor) {

System.out.println("IndiaGate accept the visotr and welcomes him");

System.out.println("IndiaGate visit is started");

visitor.visit(this);

}

}

One of the class implementing Place interface and defining the logic of accept method . Accept method will accept the visitor and then on visitor instance invoke visit method and pass the current place instance to the visitor . Visitor can use this instance to visit the place

class Tajmahal implements Place {

publicvoid accept(IVisotor visitor) {

System.out.println("Tajmahal accept the visotr and welcomes him");

System.out.println("Tajmahal visit is started");

visitor.visit(this);

}

}

One of the class implementing Place interface and defining the logic of accept method . Accept method will accept the visitor and then on visitor instance invoke visit method and pass the current place instance to the visitor . Visitor can use this instance to visit the place

class RedFort implements Place {

publicvoid accept(IVisotor visitor) {

System.out.println("RedFort accept the visotr and welcomes him");

System.out.println("RedFort visit is started");

visitor.visit(this);

}

}

-->

One of the class implementing Place interface and defining the logic of accept method . Accept method will accept the visitor and then on visitor instance invoke visit method and pass the current place instance to the visitor . Visitor can use this instance to visit the place

class JamaMasjid implements Place {

publicvoid accept(IVisotor visitor) {

System.out.println("JamaMasjid accept the visotr and welcomes him");

System.out.println("JamaMasjid visit is started");

visitor.visit(this);

}

}

One of the class implementing Place interface and defining the logic of accept method . Accept method will accept the visitor and then on visitor instance invoke visit method and pass the current place instance to the visitor . Visitor can use this instance to visit the place

Subscribe via email

About Me

Mobile NO : 7503454801
Email address : manojbharal2@gmail.com
About Blog
Blog consist of posts on java j2ee technical topics. It covers all range of post starting with basic java questions up to advanced j2ee architecture. I am owning this blog. Content being published here is authentic and original . This is growing blog and visitors can expect huge relevant and useful information to enhance their knowledge on subject.
About me
I have total of around 8 years of experience in java j2ee technologies and am working on multiple business domain technical projects in all range of industries .