Java Design Pattern: Visitor

10568 단어 design pattern

http://www.programcreek.com/2011/05/visitor-design-pattern-example/
import java.util.ArrayList;
 
interface Visitor {
	public void visit(City city);
	public void visit(Museum museum);
	public void visit(Park park);
}
 
class FirstTimeVisitor implements Visitor {
 
	@Override
	public void visit(City city) {
		System.out.println("I'm visiting the city!");
	}
 
	@Override
	public void visit(Museum museum) {
		System.out.println("I'm visiting the Museum!");
	}
 
	@Override
	public void visit(Park park) {
		System.out.println("I'm visiting the Park!");
	}
}
 
interface Element {
	public void accept(Visitor visitor);
}
 
class City implements Element {
 
	ArrayList<Element> places = new ArrayList<Element>();
 
	public City() {
		places.add(new Museum());
		places.add(new Park());
	}
 
	@Override
	public void accept(Visitor visitor) {
		System.out.println("City is accepting visitor.");
		visitor.visit(this);
 
		for (Element e : places) {
			e.accept(visitor);
		}
	}
}
 
class Museum implements Element {
	@Override
	public void accept(Visitor visitor) {
		System.out.println("Museum is accepting visitor.");
		visitor.visit(this);
	}
}
 
class Park implements Element {
	@Override
	public void accept(Visitor visitor) {
		System.out.println("Park is accepting visitor.");
		visitor.visit(this);
	}
 
}
 
public class TestVisitor {
	public static void main(String[] args) {
		FirstTimeVisitor visitor = new FirstTimeVisitor();
		City city = new City();
		city.accept(visitor);
	}
}

좋은 웹페이지 즐겨찾기