Java graphical design pattern Visitor Pattern

Demand test system

1) divided people into men and women, for evaluation of the singer, a singer after watching the show, they get different comments about the singer (evaluation There are different types, such as success, failure, etc.)
2) conventional scheme
Here Insert Picture Description

Question the traditional way of analysis

1) If the system is relatively small, there is no problem, but when considering the system adding more and more new features, changes to the code is relatively large, in violation of the principle of class ocp, is not conducive to maintenance.
2) poor scalability, such as adding a new type of person classes, or management.

Visitor pattern basic introduction

1) mode visitor (Visitor Pattern), encapsulating some of the effects on the operation of certain elements of the data structure, it may not change the definition of the role of the premise of the new data structure in the operation of these elements.
2) the main data structures and data separation operation, and the data structure operatively coupled to solve the problem
3) basic operating principle is the visitor pattern: a plus-visited provide external interfaces which are accessed in the class
4) the visitor pattern the main scenarios: the need for a target structure of objects many different operations (these operations are not associated with each other), while avoiding the need to make these operations class "pollution" of these objects, you can use the visitor pattern resolved.
Here Insert Picture Description
Description:
. 1) is an abstract Visitor visitor, a visit object structure for operating each of the class declaration ConcreteElement.
2) ConcreteVisitor: access a specific value, each operation has achieved Visitor statement, the operation of each part is achieved.
3) ObjectStructure can enumerate its elements, can provide a high-level interface to allow visitors access to the elements.
4) Element accept defines a method for receiving a visitor object.
5) ConcreteElement concrete elements accept method implementation class.

Visitors mode application examples

Here Insert Picture Description

package com.example.demo.visitor;

import javax.activation.MailcapCommandMap;

public abstract class Action {
	
	/**
	 * 得到男性的测评
	 * @param man
	 */
	public abstract void getManResult(Man man);
	
	/**
	 * 得到女的测评
	 * @param woman
	 */
	public abstract void getWomanResult(Woman woman);

}
package com.example.demo.visitor;

public class Fail extends Action{

	@Override
	public void getManResult(Man man) {
		// TODO Auto-generated method stub
		System.out.println(" 男人给的评价该歌手很失败 ! ");
	}

	@Override
	public void getWomanResult(Woman woman) {
		// TODO Auto-generated method stub
		System.out.println(" 女人给的评价该歌手很失败 ! ");
	}

}
package com.example.demo.visitor;

public class Success extends Action{

	@Override
	public void getManResult(Man man) {
		// TODO Auto-generated method stub
		System.out.println(" 男人给的评价该歌手很成功 ! ");
	}

	@Override
	public void getWomanResult(Woman woman) {
		// TODO Auto-generated method stub
		System.out.println(" 女人给的评价该歌手很成功 ! ");
	}

}
package com.example.demo.visitor;

public abstract class Person {
	
	public abstract void accept(Action action);
}
package com.example.demo.visitor;

public class Man extends Person {

	@Override
	public void accept(Action action) {
		// TODO Auto-generated method stub
		action.getManResult(this);
	}

}
package com.example.demo.visitor;

/**
 * 说明
 * 1. 这里我们使用到了双分派,即首先在客户端程序中,将具体状态作为参数传递Woman中(第一次分派)
 * 2. 然后Woman 类调用作为参数的 “具体方法” 中方法getWomanResult,同时将自己(this)作为参数传入,完成第二次的分派
 * @author zhaozhaohai
 *
 */
public class Woman extends Person {

	@Override
	public void accept(Action action) {
		// TODO Auto-generated method stub
		action.getWomanResult(this);
	}

}
package com.example.demo.visitor;

import java.util.LinkedList;
import java.util.List;

/**
 * 数据结构,管理很多人(Man,Woman)
 * @author zhaozhaohai
 *
 */
public class ObjectStructure {

	/**
	 * 维护一个集合
	 */
	private List<Person> persons = new LinkedList<Person>();
	
	/**
	 * 增加到list
	 * @param person
	 */
	public void attach(Person person) {
		persons.add(person);
	}
	
	/**
	 * 移除
	 * @param person
	 */
	public void detach(Person person) {
		persons.remove(person);
	}
	
	/**
	 * 显示测试情况
	 * @param action
	 */
	public void display(Action action) {
		for (Person person : persons) {
			person.accept(action);
		}
	}
}
package com.example.demo.visitor;

public class Wait extends Action{

	@Override
	public void getManResult(Man man) {
		// TODO Auto-generated method stub
		System.out.println(" 男人给的评价是该歌手待定。。 ");
	}

	@Override
	public void getWomanResult(Woman woman) {
		// TODO Auto-generated method stub
		System.out.println(" 女人给的评价是该歌手待定。。 ");
	}

}
package com.example.demo.visitor;

public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 创建ObjectStructure
		ObjectStructure objectStructure = new ObjectStructure();
		objectStructure.attach(new Man());
		objectStructure.attach(new Woman());
		
		// 成功
		Success success = new Success();
		objectStructure.display(success);
		System.out.println("-------------------");
		// 失败
		Fail fail = new Fail();
		objectStructure.display(fail);
		System.out.println("----------给的是待定的测评------------");
		Wait wait = new Wait();
		objectStructure.display(wait);
	}

}

Summary - double dispatch
above-mentioned double dispatch, dispatch refers to the so-called double change no matter how kind, we can expect to find a way to run. Dual type class and assigning means to obtain two operation performed depends on the recipient's request.
Suppose you want to add a Wait state class, the class and the influence of reaction Woman Man classes, since double dispatch, simply add an Action subclass can be called to the client, no need to change any other class code.
! [Insert Picture description here] (https://img-blog.csdnimg.cn/20200328211428620.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW8xMjk5MDAyNzg4,size_16,color_FFFFFF,t_

Notes and details of the visitor pattern

Advantages:
1) a single visitor patterns in line with the principles of responsibility, so that the program has excellent scalability and flexibility is very high.
2) Visitor pattern can be unified function, you can make a report, UI, interceptors and filters for the data structure is relatively stable system.
Disadvantages:
1) the specific elements of the published details of a visitor, that visitor to focus on other types of internal details of the class, which is not recommended by the Law of Demeter, the specific elements of this type cause changes more difficult.
2) contrary to the principle cause class dependent, dependent on the specific elements of the visitor, rather than abstract elements.
3) Therefore, if a system has a relatively stable data structure, there are constantly changing functional requirements, then the visitor pattern is more appropriate.

Guess you like

Origin www.cnblogs.com/haizai/p/12593368.html