Behavioral mode: Visitor mode

First article:
Behavioral mode: Visitor mode

Eleven of the eleven behavioral models: Visitor model.

brief introduction

Name: Visitor mode
English Name: Visitor Pattern
Values: visitors are guests and hospitality is
Personal introduction:
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Encapsulates some operations that act on elements in a data structure. It can define new operations that act on these elements without changing the data structure.
(from Zen of design patterns)

The story you want

First of all, the following stories are all made up...

Do we still remember when the anti-corruption campaign began N years ago, there was a time when officials were encouraged to invite only a few dishes and soup for dinner, which should not be exceeded. I remember when I was just a freshman at that meeting, the teacher of military theory talked about this problem and expressed some of his ideas. He thought it was more rigid. Today's story is about the banquet. Nowadays, Chinese enterprises are developing more and more, and they are assuming more and more responsibilities in society. The government also pays more and more attention to enterprises. It is common for officials to visit enterprises, and it has become particularly common for enterprises to entertain officials.

The background of the story is that enterprises entertain officials at all levels. The dishes served by officials at different levels are different, and the richness of dishes in each enterprise is also different. Our visitors here use Alibaba and Tencent, and our visitors use Guojia leaders and provincial leaders as examples. Both companies cooperate with Sheraton Hotels. Alibaba's cooperation plan is: ten dishes and one soup for provincial leaders and officials below; fourteen dishes and two soup for Guojia leaders and officials above; Tencent's cooperation plan is: eight dishes and one soup for the banquet of provincial leaders and officials below the provincial level, and 16 dishes and two soup for the banquet of Guojia leaders and officials above the provincial level.

Let's take a look at how to use the visitor pattern to implement the above story.

First, define an abstract class: enterprise. A common feature of enterprises is to accept visits from superior leaders.

/**
 * enterprise
 */
abstract class Company {

    public abstract void accept(Vistor vistor);

}

In the above story, we have taken two enterprises as examples, namely Alibaba and Tencent. Here we implement the banquet programs of these two companies and the method of receiving visitors.

Alibaba's banquet for Guojia's leaders and officials above is fourteen dishes and two soups, and the banquet for provincial leaders and officials below is ten dishes and one soup.

/**
 * Alibaba enterprise
 */
class AlibabaCompany extends Company {

    @Override
    public void accept(Vistor vistor) {
        vistor.visit(this);
    }

    public String entertainBelowProvincialLeader(String leader) {
        return "Alibaba reception" + leader + ": Ten dishes and one soup";
    }

    public String entertainAboveNationalLeader(String leader) {
        return "Alibaba reception" + leader + ": Fourteen dishes and two soups";
    }

}

Tencent entertained Guojia leaders and above with 16 dishes and two soups, and entertained provincial leaders and below with eight dishes and one soup.

/**
 * Tencent enterprise
 */
class TencentCompany extends Company {

    @Override
    public void accept(Vistor vistor) {
        vistor.visit(this);
    }

    public String entertainBelowProvincialLeader(String leader) {
        return "Tencent reception" + leader + ": Eight dishes and one soup";
    }

    public String entertainAboveNationalLeader(String leader) {
        return "Tencent reception" + leader + ": Sixteen dishes and two soups";
    }
}

The visitor interface is defined here. The visitor interface has two methods: accessing Alibaba enterprise and Tencent enterprise.

/**
 * Visitor interface
 */
interface Vistor {

    void visit(AlibabaCompany alibabaCompany);

    void visit(TencentCompany tencentCompany);

}

In the above story, there are two visitors, one is the leader of Guojia, the other is the provincial leader. Because different enterprises have different banquet plans for different visitors, visiting enterprises here needs to call the banquet methods of the corresponding enterprises.

When provincial leaders visit enterprises, they need to call the enterprise's banquet scheme for provincial leaders and officials below, which is entertainBelowProvincialLeader()

/**
 * Visit of provincial leaders
 */
class ProvincialLeaderVistor implements Vistor {

    @Override
    public void visit(AlibabaCompany alibabaCompany) {
        System.out.println(alibabaCompany.entertainBelowProvincialLeader("Provincial leaders"));
    }

    @Override
    public void visit(TencentCompany tencentCompany) {
        System.out.println(tencentCompany.entertainBelowProvincialLeader("Provincial leaders"));
    }
}

When Guo Jia's leaders visit the enterprise, they need to call the enterprise's banquet plan for Guo Jia's leaders, which is entertainAboveNationalLeader()

/**
 * Guojia leader's visit
 */
class NationalLeaderVistor implements Vistor {

    @Override
    public void visit(AlibabaCompany alibabaCompany) {
        System.out.println(alibabaCompany.entertainAboveNationalLeader("Provincial leaders"));
    }

    @Override
    public void visit(TencentCompany tencentCompany) {
        System.out.println(tencentCompany.entertainAboveNationalLeader("Guojia leader"));
    }
}

The above is the code of the visitor and the interviewee. Because the enterprise entertains the leaders in Sheraton Hotel, a hotel is also needed. The hotel has a list of enterprise cooperation and provides methods for entertaining the leaders.

/**
 * Hotel
 */
class Hotel {
    private List<Company> companies = new ArrayList<>();

    public void entertain(Vistor vistor) {
        for (Company company : companies) {
            company.accept(vistor);
        }
    }

    public void add(Company company) {
        companies.add(company);
    }
}

The test code is provided below to see how the results are.

public class VisitorTest {

    public static void main(String[] args) {
        AlibabaCompany alibabaCompany = new AlibabaCompany();
        TencentCompany tencentCompany = new TencentCompany();
        ProvincialLeaderVistor provincialLeaderVistor = new ProvincialLeaderVistor();
        NationalLeaderVistor nationalLeaderVistor = new NationalLeaderVistor();

        Hotel xilaideng = new Hotel();
        xilaideng.add(alibabaCompany);
        xilaideng.add(tencentCompany);

        xilaideng.entertain(provincialLeaderVistor);
        xilaideng.entertain(nationalLeaderVistor);
    }

}

Print results:
Alibaba Reception of provincial leaders: ten dishes and one soup
Tencent Reception of provincial leaders: eight dishes and one soup
Alibaba Reception Guojia leader: Fourteen dishes and two soups
Tencent Reception Guojia leader: Sixteen dishes and two soups

The complete visitor mode code has been presented. Take 1 minute to think about it. After understanding the whole code, let's take a look at the following summary.

summary

The visitor mode has good extensibility. Look at the visitor code. If we want to add a visitor: a city leader, we can just add a city leader human. Of course, it has its disadvantages, that is, it exposes the interviewees to the visitors, so that the visitors can directly understand all the things of the interviewees. Only by understanding the advantages and disadvantages can it be better used in practice. The general visitor mode is used in scenarios that require traversing multiple different objects.

Recommended reading

Behavioral mode: interpreter mode

Behavioral mode: observer mode

Behavioral mode: iterator mode

The series of articles on design patterns are continuously updated. Welcome to follow the official account LieBrother and exchange and learn together.
The background of official account replies to the "big gift package" to obtain tutorials such as Java, Python, IOS, etc
Add personal wechat notes "tutorial" to obtain tutorials such as architects and machine learning

Posted by Rangel on Mon, 30 May 2022 17:27:01 +0530