Westward Java design patterns learning summary (28) --- Visitor pattern

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/a770794164/article/details/90749948

Visitor pattern (Visitor), indicates a role in the operation of each element of an object structure. It makes you can not change the definition of the class under the premise of each element acting on the operation of these new elements.
Westward from design patterns
Example:
Some holidays, the company will arrange for staff to go along with some outdoor games, each game, the task in the form of male and female colleagues are also different, such as blowing balloons, which require mouth blown male colleagues, female colleague with a pump; riding bicycle with people, male colleagues responsible for cycling, female colleague in charge of sitting in the back and so on. This scenario can be used to implement the Visitor pattern, male colleagues and female colleagues are two different elements, each game is a visitor.

  1. Element classes
abstract class Person {
    String name;
    public Person(String name) {
        this.name = name;
    }
    abstract void play(Game game);
}
// 男同事类
class ManWorkmate extends Person {
    public ManWorkmate(String name) {
        super(name);
    }
    @Override
    void play(Game game) {
        game.acceptManGame(this);
    }
}
// 女同事类
class WomanWorkmate extends Person {
    public WomanWorkmate(String name) {
        super(name);
    }

    @Override
    void play(Game game) {
        game.acceptWomenManGame(this);
    }
}
  1. Visitors, that a variety of games
abstract class Game {
    abstract void acceptManGame(Person person);
    abstract void acceptWomenManGame(Person person);
}
// 吹气球
class BallonGame extends Game {
    @Override
    void acceptManGame(Person person) {
        System.out.println("男同事:" + person.name + " ,吹气球啦!");
    }

    @Override
    void acceptWomenManGame(Person person) {
        System.out.println("女同事:" + person.name + " ,用打气筒吹气球啦!");
    }
}
// 骑自行车
class BikeGame extends Game {
    @Override
    void acceptManGame(Person person) {
        System.out.println("男同事:" + person.name + " ,准备骑自行车啦!");
    }

    @Override
    void acceptWomenManGame(Person person) {
        System.out.println("女同事:" + person.name + " ,准备坐在自行车后座啦!");
    }
}
  1. Object structure for different games to traverse colleagues involved in the game, get a different reaction
class ObjectStructure {
    List<Person> elements = new ArrayList<>();

    void add(Person person) {
        elements.add(person);
    }

    void del(Person person) {
        elements.remove(person);
    }
    
    void beginGame(Game game){
        for (Person person : elements) {
            person.play(game);
        }
    }
}
  1. The main program
class Test {
    public static void main(String[] args) {

        ObjectStructure o = new ObjectStructure();

        Person xiaoLi = new ManWorkmate("小李");
        Person xiaoWang = new WomanWorkmate("小王");
        Person xiaoZhang = new ManWorkmate("小张");
        Person xiaoZhao = new WomanWorkmate("小赵");

        o.add(xiaoLi);
        o.add(xiaoWang);
        o.add(xiaoZhang);
        o.add(xiaoZhao);

        System.out.println("进行第一项游戏:吹气球");
        Game game1 = new BallonGame();
        o.beginGame(game1);
        System.out.println("======");
        System.out.println("进行第二项游戏:骑自行车");
        Game game2 = new BikeGame();
        o.beginGame(game2);
    }
}
结果:
进行第一项游戏:吹气球
男同事:小李 ,吹气球啦!
女同事:小王 ,用打气筒吹气球啦!
男同事:小张 ,吹气球啦!
女同事:小赵 ,用打气筒吹气球啦!
======
进行第二项游戏:骑自行车
男同事:小李 ,准备骑自行车啦!
女同事:小王 ,准备坐在自行车后座啦!
男同事:小张 ,准备骑自行车啦!
女同事:小赵 ,准备坐在自行车后座啦!

If you need to add gameplay, just need to add a Gamesub-category, in the main program, call

Game game3 = new OtherGame();
o.beginGame(game3);

It can be achieved.

to sum up

Visitor pattern applied to the data structure is relatively stable system, and data structures that the coupling between the operation acting on the structure to be freed, so that the operation can be set relatively freely evolve.
Purpose is to make the visitor pattern is separated from the process data structure. If the system has a data structure relatively stable, but also easy to change the algorithm, then use of visitors is more appropriate, because the visitor pattern so as to increase arithmetic operation becomes easy.
The disadvantage is the increase in visitors to the structure of the new data structure becomes difficult. Author of a GoF once said: "Most of the time you do not need a visitor mode, but as soon as you need the visitor pattern that really need it." In fact, it is difficult to find the same data structure situation, so the opportunity to use the visitor pattern is also not too much.

Guess you like

Origin blog.csdn.net/a770794164/article/details/90749948