Behavioral patterns - visitor pattern (X)

The project Source Address: https://github.com/ggb2312/JavaNotes/tree/master/design-pattern (design patterns relevant code and notes)

1. Definitions

Operation of the elements acting on an encapsulating data structure (e.g., List / Set / Map, etc.) (may be based on the premise of each element is not changed, the definition of the role of these operating elements)

2. Applicable scene

  • A data structure such as (List / Set / Map, etc.) contain many types of objects
  • Separate data structure data manipulation

3. The class diagram and role

Class Diagram

Visitor : an interface or an abstract class that defines the behavior of each element (Element) accessible elements of its argument is that we can access, number theory, its methods and the number of elements is the same, therefore, the visitor pattern group classes required to stabilize the element, if often added, removed element class, will inevitably lead to frequent modifications visitor interface, if such is not suitable for the use of visitors.
ConcreteVisitor1, ConcreteVisitor2 : specific access class that needs to be given a specific element against every type of access generated.
Element : element interface or an abstract class that defines the method (Accept) to accept a visitor, its meaning refers to each element must be accessible to visitors.
ConcreteElementA, ConcreteElementB : concrete elements class that provides specific implementation interview method, and this specific implementation, usually a way to access the elements of the class to use the visitor offer.
ObjectStructure : define which said object structure, object structure is an abstract representation that the internal management of the collection of elements, and these elements can be iterated for visitors.

3. relevant design patterns

Visitor pattern and iterator pattern

  • The same point: some data are processed in the above structure
  • Different points: visitor patterns for elements of a data structure stored in a specific process is performed, one at a vital iterator pattern to traverse the elements in the data structure.

Example 4. Mode

Background : unique visitors (the average user, the boss) to see different courses (free courses, fee-paying courses), you can see different results.

(1) Visitor Visitors interfaces

/**
 * Create by eval on 2019/2/11
 */
public interface IVisitor {
    void visit(FreeCourse freeCourse);

    void visit(CodingCourse codingCourse);
}

(2) ConcreteVisitor Visitors implementation class

/**
 * Create by eval on 2019/2/11
 */
public class Visitor implements IVisitor {
    /**
     * 访问免费课程,打印所有免费课程名称
     *
     * @param freeCourse
     */
    @Override
    public void visit(FreeCourse freeCourse) {
        System.out.println("免费课程:" + freeCourse.getName());
    }

    /**
     * 访问实战课程,打印所有实战课程名称以及价格
     *
     * @param codingCourse
     */
    @Override
    public void visit(CodingCourse codingCourse) {
        System.out.println("实战课程:" + codingCourse.getName() + " 价格:" + codingCourse.getPrice()+ "元");
    }
}

(3) Element program interfaces

/**
 * Create by eval on 2019/2/11
 */
public abstract class Course {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public abstract void accept(IVisitor visitor);
}

(4) ConcreteElement curriculum implementation class

/**
 * Create by eval on 2019/2/11
 */
public class CodingCourse extends Course {
    private int price;

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public void accept(IVisitor visitor) {
        visitor.visit(this);
    }
}
/**
 * Create by eval on 2019/2/11
 */
public class FreeCourse extends Course {
    @Override
    public void accept(IVisitor visitor) {
        visitor.visit(this);
    }
}

(5) Testing

import java.util.ArrayList;
import java.util.List;

/**
 * Create by eval on 2019/2/11
 */
public class Test {
    public static void main(String[] args) {
        List<Course> courseList = new ArrayList<>();

        FreeCourse freeCourse = new FreeCourse();
        freeCourse.setName("SpringMVC数据绑定");

        CodingCourse codingCourse = new CodingCourse();
        codingCourse.setName("Java设计模式");
        codingCourse.setPrice(299);

        courseList.add(freeCourse);
        courseList.add(codingCourse);

        for (Course course : courseList) {
            course.accept(new Visitor());
        }
    }
}

Test Results:

Test Results

The advantages and disadvantages

advantage:

  • It is easy to add new operations, an increase of a new visitor

Disadvantages:

  • Difficult to add new data structures
  • Specific elements of change is too much trouble

6. Extended -JDK1.7 source mode and frame visitors

java.nio.file.FileVisitor and implementation class SimpleFileVisitor

org.springframework.beans.factory.config.BeanDefinitionVisitor can traverse the various attributes of Bean, commissioned valueResolver to achieve

reference

https://www.cnblogs.com/chenssy/p/3339756.html

Guess you like

Origin www.cnblogs.com/gj-blog/p/10929607.html