preface

Visitor pattern belongs to behavior pattern. This type of design pattern summarizes the classical interaction between classes and objects, decoupled the behavior and use of classes and objects, and used the behavior of objects to complete the function in a specific scene.

Visitor pattern

Usage scenario: The visitor pattern can be used to decouple the operation of data from the structure of the data, which is stable, but the operation of the data is variable.

Understanding: This is a classic interaction between classes and objects that decouples the behavior and usage of classes and objects. Define new operations on these data structures without changing the existing ones. Visitors are powerful enough to extend functionality to a class or object without breaking the structure of that class or object. It exposes the details of the interior, but it also has excellent extensions.

namespace action_mode_07 {

    interface ITeacherVisitor {
        visit(student: IStudent): void;
    }

    interface IStudent {
        type: string
        accep(techer: ITeacherVisitor): void;
    }

    Interviewer: Teacher A
    class TecherA implements ITeacherVisitor {

        name: string

        constructor (name: string) {
            this.name = name
        }

        visit(student: IStudent): void {
            if (student.type === 'Good student') {
                console.log(this.name + 'Very happy, chatting with the students' parents. ')}else if (student.type === 'Bad student') {
                console.log(this.name + 'It is embarrassing that parents who comfort bad students should spend more time with their children. ')}else {
                console.log(this.name + 'Communicate normally with parents. ')}}}/ / good students
    class GoodStudent implements IStudent {

        name: string
        type: string
        
        constructor (name: string) {
            this.name = name
            this.type = 'Good student'
        }

        accep(techer: ITeacherVisitor): void {
            techer.visit(this)}}/ / bad student
    class BadStudent implements IStudent {

        name: string
        type: string

        constructor (name: string) {

            this.name = name
            this.type = 'Bad student'
        }

        accep(techer: ITeacherVisitor) {
            techer.visit(this)}}// Ordinary students
    class NormalStudent implements IStudent {

        name: string
        type: string

        constructor (name: string) {

            this.name = name
            this.type = 'Ordinary student'
        }

        accep(techer: ITeacherVisitor) {
            techer.visit(this)}}// Visitor A
    const techerA = new TecherA('Lady Teacher')

    const goodStudent = new GoodStudent('Good Student - Xia Xue')
    const badStudent = new BadStudent('Bad student - Liu Xing')
    const normalStudent = new NormalStudent('Ordinary Student - Summer Rain')

    // Extends behavior directly through visitors without changing the data structure
    goodStudent.accep(techerA)
    badStudent.accep(techerA)
    normalStudent.accep(techerA)

    // One more
    class TecherB implements ITeacherVisitor {
        name: string

        constructor (name: string) {
            this.name = name
        }

        visit(student: IStudent): void {
            if (student.type === 'Good student') {
                console.log(this.name + 'Very happy, chatting with the students' parents. ')}else if (student.type === 'Bad student') {
                console.log(this.name + 'Very happy, bad students are very talented and promising in mathematics. ')}else {
                console.log(this.name + 'I was very happy to communicate with the parents. ')}}}// Visitor B
    const techerB = new TecherB('Golden Teacher')
    goodStudent.accep(techerB)
    badStudent.accep(techerB)
    normalStudent.accep(techerB)

}
Copy the code