Writing D:/inetpub/webs/vincenzodevivocom/wiki/data/cache/8/80e45f009fc44d1ecec6c48e3897434a.i failed
Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.
Writing D:/inetpub/webs/vincenzodevivocom/wiki/data/cache/8/80e45f009fc44d1ecec6c48e3897434a.i failed
Unable to save cache file. Hint: disk full; file permissions; safe_mode setting.
Writing D:/inetpub/webs/vincenzodevivocom/wiki/data/cache/8/80e45f009fc44d1ecec6c48e3897434a.xhtml failed

Visitor

public interface PlaceVisitor {
    void visit(Museum museum);
    void visit(Church church);
    void visit(City city);
}
 
public interface Place {
    void accept(PlaceVisitor visitor);
    String getName();
}
 
public class Museum implements Place{
 
    private String name;
    private String street;
 
    public Museum(String name, String street) {
        this.name = name;
        this.street = street;
    }
 
    @Override
    public void accept(PlaceVisitor visitor) {
        visitor.visit(this);
    }
    @Override
    public String getName() {
        return name;
    }
 
    public String getStreet() {
        return street;
    }
}
 
public class Church implements Place{
    private String street;
    private String name;
 
    public Church(String name, String street) {
        this.name = name;
        this.street = street;
    }
 
    @Override
    public void accept(PlaceVisitor visitor) {
        visitor.visit(this);
    }
 
    @Override
    public String getName() {
        return name;
    }
 
    public String getStreet() {
        return street;
    }
}
 
public class City implements Place {
    private String name;
    private String country;
    private Place[] places;
 
    public City(String country, String name) {
        this.country = country;
        this.name = name;
        places = new Place[]{
                new Museum("Museum-1", "street n.1"),
                new Church("Church-1", "street n.2")
        };
    }
 
    @Override
    public void accept(PlaceVisitor visitor) {
        visitor.visit(this);
        for (Place place : places) {
            place.accept(visitor);
        }
    }
 
    public String getCountry() {
        return country;
    }
 
    @Override
    public String getName() {
        return name;
    }
}
 
public class PlaceVisitorImpl implements PlaceVisitor {
 
    public PlaceVisitorImpl() {
    }
 
    @Override
    public void visit(Museum museum) {
        print(museum.getName() + ", " + museum.getStreet());
    }
 
    @Override
    public void visit(Church church) {
        print(church.getName() + ", " + church.getStreet());
    }
 
    @Override
    public void visit(City city) {
        print(city.getName() + "(" + city.getCountry() + ")");
    }
 
    private void print(String str) {
        System.out.println("Visiting " + str);
    }
}
 
public class Trip {
    public static void main(String[] args) {
        City city = new City("Italy","Naples");
        PlaceVisitorImpl visitor = new PlaceVisitorImpl();
        city.accept(visitor);
    }
}

Output

Visiting Naples(Italy)
Visiting Museum-1, street n.1
Visiting Church-1, street n.2
 
code/java/design_pattern/visitor.txt · Last modified: 2012/05/31 21:46 by Vincenzo De Vivo
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki