To present an operation to be performed on elements of an object structure. The visitor pattern allows for the addition of a new operation without changing the classes of the elements on which it is to operate.
Visitor 模式是要講述關於在類別方法中的設計觀念,他允許了新增一個新的method實作內容,但卻不用變更目前實際在使用中的物件類別。
可以舉個例子來看,假設我們有個組裝車,裏頭放置了有各式各項的組成聚合物(DVD player , GPRS, air condition, ...),有一天我需要知道車內各項聚合物的耗電量時,就得一個一個聚合的元素慢慢找( ex: getEnergyUsage() , 當然前提是這些聚合物都時做同樣的介面)
- 允許對合成結構加入新的操作,而無須改變結構本身。
- 想要加入新的操作是輕而易舉的事情。
- 參觀者所進行的操作,程式碼是被集中在一起的。
- Head First Design Pattern說到,當想要為一個 Aggregation class 增加新的功能,且封裝並不是那麼重要時,可以直接這麼做。

[caption id="attachment_282" align="alignnone" width="600" caption="Visitor Pattern 2"]
[/caption]
package visitorpattern;
interface CarElementVisitor {
void visit(Wheel wheel);
void visit(Engine engine);
void visit(Body body);
void visit(Car car);
}
interface CarElement {
void accept(CarElementVisitor visitor); // CarElements have to provide
// accept().
}
class Wheel implements CarElement {
private String name;
public Wheel(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}
class Engine implements CarElement {
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}
class Body implements CarElement {
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}
class Car implements CarElement {
CarElement[] elements;
public CarElement[] getElements() {
return elements.clone(); // Return a copy of the array of references.
}
public Car() {
this.elements = new CarElement[] { new Wheel("front left"), new Wheel("front right"), new Wheel("back left"),
new Wheel("back right"), new Body(), new Engine() };
}
public void accept(CarElementVisitor visitor) {
for (CarElement element : this.getElements()) {
element.accept(visitor);
}
visitor.visit(this);
}
}
class CarElementPrintVisitor implements CarElementVisitor {
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");
}
}
class VisitorDemo implements CarElementVisitor {
public void visit(Wheel wheel) {
System.out.println("Kicking my " + wheel.getName() + " wheel");
}
public void visit(Engine engine) {
System.out.println("Starting my engine");
}
public void visit(Body body) {
System.out.println("Moving my body");
}
public void visit(Car car) {
System.out.println("Starting my car");
}
}
public class TestVisitorDemo {
public static void main(String[] args) {
Car car = new Car();
car.accept(new CarElementPrintVisitor());
car.accept(new VisitorDemo());
}
}
沒有留言:
張貼留言