java design patterns series of visitor patterns

Before writing this article, I also saw more than a dozen articles written by Daniel network design patterns of visitors, visitors say that this model is the most complex one, but I think with the other design patterns and not too much different, therefore their own order a bit, I believe most people can understand.

First, recognize the visitor pattern

1, the concept of

Some packaging operation applied to some data structure of each element, it can not change the premise of this data structure, a new definition of the role of these operating elements.

How to understand it? Take, for example, if our all played king of glory. Each hero has three basic skills, but players operate in different ways to achieve hero skills are not the same. For example, you can only play Han sent the head, tears play Han dream that is victorious.

In this example, the three basic skills of Zhuang Zhou and Zhen Ji is unchanged, but the visitor pattern can achieve some different effects on these three skills. We can draw a class diagram further to understand it.

2, FIG type

I believe this model says more complex because of its class diagram it, but after we were coloring for each module will be able to find that in fact is not so troublesome. The class diagram above relate to a total of six roles:

(1) Vistor (abstract visitors): user interface object structure specific elements of a role statement for the visit. Like the different pieces of the glory of the king inside the player.

(2) ConcreteVisitor (particularly visitors): visitors are implemented for each specific operation Vistor defined. It is like one specific player.

(3) Element (abstract elements): defines an accept operation to Visitor as a parameter. It can be likened to the king inside the hero's template.

(4) ConcreteElement (particularly metals): implements the accept Element () method call Vistor access method to perform operations on an element. It can be expressed as a specific a hero, like the disillusioned and Zhen Ji.

(5) ObjectStructure (object structure): may be a combination mode, may be set; to enumerate element comprising; providing an interface that allows Vistor access its elements. Zhuang Zhou and Zhen Ji is allowed access to the outside elements.

With this in mind, here we can use the code to analyze.

Second, code implementation

The first step: define the abstract elements (the hero of the template)

public abstract class Hero {
	//英雄可以接受玩家的访问,让玩家来操作
	public abstract void accept(Player visitor);
}
复制代码

Step Two: Definitions of specific elements (specifically, a hero a)

First, we can define Zhen Ji

//甄姬
public class ZhenHero extends Hero {
	//可以接受玩家的操作
	@Override
	public void accept(Player visitor) {
		visitor.visitZhen(this);
	}
	//自身的技能
	public void operate() {
		System.out.println("甄姬放出了技能");
	}
}
复制代码

There is also a disillusioned

//庄周
public class ZhuangHero extends Hero {
	//接受外部的访问
	@Override
	public void accept(Player visitor) {
		visitor.visitZhuang(this);
	}
	//自身的技能
    public void operate() {
    	System.out.println("庄周放出了技能");
    }
}
复制代码

The third step: defining abstract visitors (players)

//玩家可以访问甄姬和庄周的技能
public interface Player {
    public abstract void visitZhuang(ZhuangHero element);
    public abstract void visitZhen(ZhenHero element);
}
复制代码

The fourth step: Define specific visitors (my players and other players)

First is my own operation

public class PlayerMe implements Player {
	@Override
	public void visitZhuang(ZhuangHero element) {
		System.out.println("玩家我访问庄周,庄周开始使出技能");
		element.operate();
	}
	@Override
	public void visitZhen(ZhenHero element) {
		System.out.println("玩家我访问甄姬,甄姬开始使出技能");
		element.operate();
	}
}
复制代码

Then the other players operating

public class PlayerOthers implements Player {
	@Override
	public void visitZhuang(ZhuangHero element) {
		System.out.println("玩家其他人访问庄周,庄周开始使出技能");
		element.operate();
	}
	@Override
	public void visitZhen(ZhenHero element) {
		System.out.println("玩家其他人访问甄姬,甄姬开始使出技能");
		element.operate();
	}
}
复制代码

Step five: the definition of object structure

public class ObjectStructure {
    //保存所有需要被访问的元素
    private List<Hero> heros = new ArrayList<Hero>();
    public void handleRequest(Player visitor) {
        //访问所有元素
        for(Hero hero : heros) {
            hero.accept(visitor);
        }
    }
    public void addHero(Hero hero) {
    	heros.add(hero);
    }
}
复制代码

Step Six: Client test

public class Client {
	public static void main(String[] args) {
		ObjectStructure os = new ObjectStructure();
		Hero zhuang = new ZhuangHero();
		Hero zhen = new ZhenHero();

		os.addHero(zhuang);
		os.addHero(zhen);

		Player visitorMe = new PlayerMe();
		Player visitorOthers = new PlayerOthers();
		// 让访问者访问对象结构中的元素
		os.handleRequest(visitorMe);
		os.handleRequest(visitorOthers);
	}
}
复制代码

The last step we will be able to see the result

This is the Visitor pattern.

Third, the visitor pattern analysis

Condition is that the visitor using the object model structure is generally not changed, but the operation is not the lack of the same situation. For example, a complex set of objects, XML document parsing, compiler design, etc. will often use this mode.

Advantage is that the problem solved by the visitor pattern, i.e. usage scenarios drawback is that without the use of appropriate changes in the object structure.

Guess you like

Origin juejin.im/post/5d6389c3f265da03b46bfae7