14. The Visitor Pattern

Visitor Pattern

First, application examples

The audience is divided into male and female, for evaluation of the singer, a singer after watching the show, the singer got their different evaluation (success, failure, etc.)

Second, the visitor pattern

1. Basic Introduction

1) acting on the package of some elements of the data structure of a certain operation, new operation without changing the definition of the data structure applied to these elements.

2) the data structure and operation of the separation data, and a data structure operatively coupled to solve the problem

3) The basic operating principle: to increase a reception of foreign visitors to provide interfaces to be accessed inside the class

2. The principle of class diagram

1) Visitor abstract visitor, for each object structure class declaration of a visit operation ConcreteElement

2) ConcreteVisitor: is a specific implementation of each operation with a visitor Visitor statement, the operation of each part is achieved.

3) ObjectStructure can enumerate its elements, can provide a high-level interface to allow visitors access to the elements

4) Element accept defines a method for receiving a visitor objects

5) ConcreteElement concrete elements to achieve accept method

Third, application examples visitors

1. Analysis of ideas

2. code implementation

/**
 * 抽象访问者
 */
public abstract class Action {
    //得到男性 的测评
    public abstract void getManResult(Man man);
    
    //得到女的 测评
    public abstract void getWomanResult(Woman woman);
}

/**
 * 具体访问者
 */
public class Fail extends Action {
    @Override
    public void getManResult(Man man) {
        System.out.println(" 男人给的评价该歌手失败 !");
    }

    @Override
    public void getWomanResult(Woman woman) {
        System.out.println(" 女人给的评价该歌手失败 !");
    }
}

public class Success extends Action {
    @Override
    public void getManResult(Man man) {
        System.out.println(" 男人给的评价该歌手很成功 !");
    }

    @Override
    public void getWomanResult(Woman woman) {
        System.out.println(" 女人给的评价该歌手很成功 !");
    }
}
/**
 * 抽象被访问者
 */
public abstract class Person {
    //提供一个方法,让访问者可以访问
    public abstract void accept(Action action);
}

//说明
//1. 这里我们使用到了双分派, 即首先在客户端程序中,将具体状态作为参数传递Woman中(第一次分派)
//2. 然后Woman 类调用作为参数的 "具体方法" 中方法getWomanResult, 同时将自己(this)作为参数
public class Woman extends Person{
    @Override
    public void accept(Action action) {
        action.getWomanResult(this);
    }
}
public class Man extends Person {
    @Override
    public void accept(Action action) {
        action.getManResult(this);
    }
}
//数据结构,管理很多人(Man , Woman)
public class ObjectStructure {
    //维护了一个集合
    private List<Person> persons = new LinkedList<>();
    
    //增加到list
    public void attach(Person p) {
        persons.add(p);
    }
    //移除
    public void detach(Person p) {
        persons.remove(p);
    }
    
    //显示测评情况
    public void display(Action action) {
        for(Person p: persons) {
            p.accept(action);
        }
    }
}
public class Client {
    public static void main(String[] args) {
        //创建ObjectStructure
        ObjectStructure objectStructure = new ObjectStructure();
        objectStructure.attach(new Man());
        objectStructure.attach(new Woman());
        
        //成功
        Success success = new Success();
        objectStructure.display(success);
        
        System.out.println("===============");
        // 失败
        Fail fail = new Fail();
        objectStructure.display(fail);
    }
}

Fourth, the visitor pattern Notes

advantage

1) Visitors single model in line with the principles of responsibility, so that the program has excellent scalability, flexibility

2) Visitor pattern can be unified function, you can make a report, UI, interceptors and filters for the data structure is relatively stable system

Shortcoming

1) Visitors concerned about other types of internal details, so Zhao into specific elements of change more difficult. Demeter is not recommended

2) contrary to the Dependency Inversion Principle. Visitors relies on specific elements, rather than abstract elements

Therefore, if a system has a relatively stable data structure , there are constantly changing functional requirements, then the visitor pattern is more suitable

Guess you like

Origin www.cnblogs.com/chao-zjj/p/11333787.html