Java Description Design Mode (23): Visitor Mode

Source code for this article: GitHub. Click here || GitEE. Click here

1. Life Scene

1. Scene description

Electrical competition is a sport that reaches the level of "competition" in a game.Intelligent antagonistic movement between people using electronic devices as sports equipment.Through competition, people's reaction ability, coordination ability and team spirit can be improved.However, different groups of people have different perspectives on competition. Some people think that competition is just addiction to the network and hold an opposing attitude, while others agree with it.The scenario is described below based on the visitor pattern.

2. Scene illustrations

3. Code implementation

public class C01_InScene {
    public static void main(String[] args) {
        DataSet dataSet = new DataSet() ;
        dataSet.addCrowd(new Youth());
        dataSet.addCrowd(new MiddleAge());
        CrowdView crowdView = new Against() ;
        dataSet.display(crowdView);
        crowdView = new Approve() ;
        dataSet.display(crowdView);
    }
}
/**
 * Double Assignment, Different Population Management
 */
abstract class Crowd {
    abstract void accept(CrowdView action);
}
class Youth extends Crowd {
    @Override
    public void accept(CrowdView view) {
        view.getYouthView(this);
    }
}
class MiddleAge extends Crowd {
    @Override
    public void accept(CrowdView view) {
        view.getMiddleAgeView (this);
    }
}
/**
 * Management of Different Population Ideas
 */
abstract class CrowdView {
    // Young people's ideas
    abstract void getYouthView (Youth youth);
    // Middle-aged Ideas
    abstract void getMiddleAgeView (MiddleAge middleAge);
}
class Approve extends CrowdView {
    @Override
    public void getYouthView(Youth youth) {
        System.out.println("Young people agree with the competition");
    }
    @Override
    public void getMiddleAgeView(MiddleAge middleAge) {
        System.out.println("Middle-aged people agree with the competition");
    }
}
class Against extends CrowdView {
    @Override
    public void getYouthView(Youth youth) {
        System.out.println("Young people oppose competition");
    }
    @Override
    public void getMiddleAgeView(MiddleAge middleAge) {
        System.out.println("Middle-aged people oppose competition");
    }
}
/**
 * Provide a data collection
 */
class DataSet {
    private List<Crowd> crowdList = new ArrayList<>();
    public void addCrowd (Crowd crowd) {
        crowdList.add(crowd);
    }
    public void display(CrowdView crowdView) {
        for(Crowd crowd : crowdList) {
            crowd.accept(crowdView);
        }
    }
}

2. Visitor Mode

1. Basic concepts

Visitor mode is the behavior mode of the object. It encapsulates the operations on the elements of the data structure, and there is no correlation between the operations.Different operations on these elements can be defined without changing the data structure.It mainly separates data structure from data operation, solves the core principle of data structure and operation coupling problem: the class being accessed plus external interface for hosting visitors.

2. Pattern Diagram

3. Core Role

  • Abstract Visitor Role

Declare multiple method operations, specific interfaces that the visitor role needs to implement.

  • Specific Visitor Role

Implementing an interface declared by an abstract visitor is an individual access operation.

  • Abstract Node Role

Declares that the operation is accepted and accepts the visitor object as a parameter.

  • Specific Node Roles

Implement the specific operations specified by the abstract node.

  • Structural Object Role

Enumerating all elements in a structure provides a high-level interface that allows visitor objects to access each element.

4. Source implementation

public class C02_Visitor {
    public static void main(String[] args) {
        ObjectStructure obs = new ObjectStructure();
        obs.add(new NodeA());
        obs.add(new NodeB());
        Visitor visitor = new VisitorA();
        obs.doAccept(visitor);
    }
}
/**
 * Abstract Visitor Role
 */
interface Visitor {
    /**
     * NodeA Access operations for
     */
    void visit(NodeA node);
    /**
     * NodeB Access operations for
     */
    void visit(NodeB node);
}
/**
 * Specific Visitor Role
 */
class VisitorA implements Visitor {
    @Override
    public void visit(NodeA node) {
        node.operationA() ;
    }
    @Override
    public void visit(NodeB node) {
        node.operationB() ;
    }
}
class VisitorB implements Visitor {
    @Override
    public void visit(NodeA node) {
        node.operationA() ;
    }
    @Override
    public void visit(NodeB node) {
        node.operationB() ;
    }
}
/**
 * Abstract Node Role
 */
abstract class Node {
    /**
     * Receive Visitors
     */
    abstract void accept(Visitor visitor);
}
/**
 * Specific Node Roles
 */
class NodeA extends Node{
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
    public void operationA(){
        System.out.println("NodeA.operationA");
    }
}
class NodeB extends Node{
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
    public void operationB(){
        System.out.println("NodeB.operationB");
    }
}
/**
 * Structural Object Role Class
 */
class ObjectStructure {
    private List<Node> nodes = new ArrayList<>();
    public void detach(Node node) {
        nodes.remove(node);
    }
    public void add(Node node){
        nodes.add(node);
    }
    public void doAccept(Visitor visitor){
        for(Node node : nodes) {
            node.accept(visitor);
        }
    }
}

3. Spring Framework Application

1. Access to Bean Structure

BeanDefinitionVisitor class, which traverses the properties of a bean; interface BeanDefinition, which defines various information about a bean, such as attribute values, construction methods, parameters, and so on.This encapsulates the relevant methods for manipulating the bean structure, but does not alter the structure of the bean.

2. Core Code Blocks

public class BeanDefinitionVisitor {
    public void visitBeanDefinition(BeanDefinition beanDefinition) {
        this.visitParentName(beanDefinition);
        this.visitBeanClassName(beanDefinition);
        this.visitFactoryBeanName(beanDefinition);
        this.visitFactoryMethodName(beanDefinition);
        this.visitScope(beanDefinition);
        if (beanDefinition.hasPropertyValues()) {
            this.visitPropertyValues(beanDefinition.getPropertyValues());
        }
        if (beanDefinition.hasConstructorArgumentValues()) {
            ConstructorArgumentValues cas = beanDefinition.getConstructorArgumentValues();
            this.visitIndexedArgumentValues(cas.getIndexedArgumentValues());
            this.visitGenericArgumentValues(cas.getGenericArgumentValues());
        }
    }
}

4. Summary of Modes

1. Advantage Description

(1) The visitor mode conforms to the principle of single responsibility and makes the program scalable and flexible;

(2) Visitor mode is suitable for scenarios where data structure is relatively stable due to common functions such as interceptors and filters;

2. Defect Description

(1) Visitors pay attention to the internal details of other classes, rely heavily on them, and violate Dimitt's law, which causes difficulties in updating specific elements;

(2) Visitors rely on specific elements, not abstract elements, and program in detail, which violates the principle of inversion of dependency;

5. Source code address

GitHub·address
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·address
https://gitee.com/cicadasmile/model-arithmetic-parent

Keywords: Programming github network Spring Attribute

Added by powlow on Tue, 26 Nov 2019 01:50:02 +0200