Design Pattern (13) Visitor Pattern

What is the visitor pattern

Visitor mode (Visitor) is an operation that operates on a set of objects. Its purpose is not to change the definition of the object, but it allows different visitors to be added to define new operations.

Core: separate data structure from processing

example

File and Folder Handling

File Structure
Define file handling, using recursive

package BehavioralPattern.VisitorMode;

import java.io.File;

/**
 * 文件结构
 */

public class FileStructure {
    
    
    // 根目录
    private File path;

    public FileStructure(File path) {
    
    
        this.path = path;
    }

    public void handle(Visitor visitor) {
    
    
        scan(this.path, visitor);
    }

    private void scan(File file, Visitor visitor) {
    
    
        if(file.isDirectory()){
    
    
            // 访问者处理文件夹
            visitor.visitDir(file);
            // 递归处理子文件夹
            for(File sub : file.listFiles()){
    
    
                scan(sub,visitor);
            }
        } else if (file.isFile()) {
    
    
            visitor.visitFile(file);
        }
    }
}

visitor interface

package BehavioralPattern.VisitorMode;

import java.io.File;

/**
 * 访问者
 */

public interface Visitor {
    
    

    void visitDir(File dir);

    void visitFile(File file);
}

java file visitor

package BehavioralPattern.VisitorMode;

import java.io.File;

/**
 * java文件访问者
 */

public class JavaFileVisitor implements Visitor{
    
    
    @Override
    public void visitDir(File dir) {
    
    
        System.out.println("Visit dir: " + dir);
    }

    @Override
    public void visitFile(File file) {
    
    
        if(file.getName().endsWith(".java")){
    
    
            System.out.println("Found java file: " + file);
        }
    }
}

class file visitor

package BehavioralPattern.VisitorMode;

import java.io.File;

/**
 * class文件访问者
 */

public class ClassFileVisitor implements Visitor{
    
    
    @Override
    public void visitDir(File dir) {
    
    
        System.out.println("Visit dir: " + dir);
    }

    @Override
    public void visitFile(File file) {
    
    
        if(file.getName().endsWith(".java")){
    
    
            System.out.println("Found class file: " + file);
        }
    }
}

Main

package BehavioralPattern.VisitorMode;

import java.io.File;

/**
 * Main
 */

public class Main {
    
    
    public static void main(String[] args) {
    
    
        FileStructure fs = new FileStructure(new File("D:\\demo\\hello-security"));
        fs.handle(new JavaFileVisitor());
        fs.handle(new ClassFileVisitor());
    }
}

Result
insert image description here
ps: There is no .class ending file in this path, so it will not be displayed

You can see the above example, the file structure is separated from the specific operation, and the specific operation is left to the incoming visitor to decide

Summarize

In fact, the file traversal method has been implemented in Files, using the visitor mode

    public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor)
        throws IOException
    {
    
    
        return walkFileTree(start,
                            EnumSet.noneOf(FileVisitOption.class),
                            Integer.MAX_VALUE,
                            visitor);
    }

The visitor pattern is to abstract operations that act on a set of complex objects, and subsequent operations can be added without making any changes to the existing object structure.

Guess you like

Origin blog.csdn.net/weixin_43636205/article/details/130294025