Science Fair Projects Ideas - Visitor pattern

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Visitor pattern

In software engineering, the visitor design pattern is a way of separating an algorithm from an object structure.

The idea is to use an object structure of element classes, each of which has has an accept method that takes a visitor object as an argument. The visitor is an interface that has a different visit method for each element class. The accept method of an element class calls back the visit method for its class. Separate concrete visitor classes can then be written that perform some particular operations.

(One of these visit methods of a concrete visitor can be thought of as methods not of a single class, but rather methods of a pair of classes: the concrete visitor and the particular element class. Thus the visitor pattern simulates double dispatch in a conventional single dispatch object-oriented language such as Java or C++.)

The visitor pattern also specifies how iteration occurs over the object structure. In the simplest version, where each algorithm needs to iterate in the same way, the accept method of a container element, in addition to calling back the visit method of the visitor, also passes the visitor object to the accept method of all its constituent child elements.

Contents

Examples

Python

The following example uses the Python programming language:

class Wheel:
    def __init__(self,name):
        self.name = name
    def accept(self,visitor):
        visitor.visitWheel(self)

class Engine:
    def accept(self,visitor):
        visitor.visitEngine(self)

class Body:
    def accept(self,visitor):
        visitor.visitBody(self)

class Car:
    def __init__(self):
        self.engine = Engine()
        self.body   = Body()
        self.wheels = [ Wheel("front left"), Wheel("front right"), Wheel("back left") , Wheel("back right") ]
    def accept(self,visitor):
        visitor.visitCar(self)
        self.engine.accept( visitor )
        self.body.accept( visitor )
        for wheel in self.wheels:
            wheel.accept( visitor )

class PrintVisitor:
    def visitWheel(self,wheel):
        print "Visiting "+wheel.name+" wheel"
    def visitEngine(self,engine):
        print "Visiting engine"
    def visitBody(self,body):
        print "Visiting body"
    def visitCar(self,car):
        print "Visiting car"

car = Car()
visitor = PrintVisitor()
car.accept(visitor)

Java

The following example uses the Java programming language:

interface Visitor {
    void visit(Wheel wheel);
    void visit(Engine engine);
    void visit(Body body);
    void visit(Car car);
}

class Wheel {
    private String name;
    Wheel(String name) {
        this.name = name;
    }
    String getName() {
        return this.name;
    }
    void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

class Engine {
    void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

class Body {
    void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

class Car {
    private Engine  engine = new Engine();
    private Body    body   = new Body();
    private Wheel[] wheels 
        = { new Wheel("front left"), new Wheel("front right"),
            new Wheel("back left") , new Wheel("back right")  };
    void accept(Visitor visitor) {
        visitor.visit(this);
        engine.accept( visitor );
        body.accept( visitor );
        for(int i=0; i<wheels.length; ++i)
            wheels[i].accept( visitor );
    }
}

class PrintVisitor implements Visitor {
    public void visit(Wheel wheel) {
        System.out.println("Visiting "+wheel.getName()
                            +" wheel");
    }
    public void visit(Engine engine) {
        System.out.println("Visiting engine");
    }
    public void visit(Body body) {
        System.out.println("Visiting body");
    }
    public void visit(Car car) {
        System.out.println("Visiting car");
    }
}

public class VisitorDemo {
    static public void main(String[] args){
        Car car = new Car();
        Visitor visitor = new PrintVisitor();
        car.accept(visitor);
    }
}

See also

External links

10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice