A brief introduction to the visitor model

1. What is the Visitor pattern

Visitor Pattern, one of behavioral design patterns, is a relatively simple design Pattern. Its definition is as follows: Represent an operation to be performed on the elements of an object structure .Visitor lets you define a new operation Without changing the classes of the elements on which it operates. It can define new operations on these elements without changing the data structure. Simply put, the visitor pattern changes the execution algorithm of the element class by providing an access class, in such a way that the execution algorithm of the element can change as the visitor changes.

2. Application scenarios

As a helpless pain of the workers, every day to sit in the morning no. 2 bus to work. Many of you may have noticed, like the blogger, that there are different fees for different types of cards when you punch in on the bus. For example, ordinary people may charge 2 yuan for punching in, students 1 yuan and senior citizens free. So today we will simulate a simple bus card system to describe the visitor model, please fasten your seat belt, ready to leave ~

Second, the implementation of the visitor pattern

1. Design idea

First of all, we need to analyze how many roles are involved in the bus punch card business:

  1. Abstract class of bus card (Element) : First of all, whether student card or senior card, its essence belongs to one kind of public card, so we can define their common behavior by pulling out an abstract class of bus card.
  2. Different bus card implementation classes (ConcreteElement) : Define different attributes for different card types.
  3. Card Punker Algorithm Abstract interface (Visitor) : Because there are many kinds of buses, the rules of punching of each bus may be different, so the common behavior needs to be removed.
  4. Card Puncher Implementation class (ConcreteVisitor) : Concrete puncher algorithm implementation.

So let’s draw a simple class diagram based on our analysis:Ok, now we just need to look at the diagram and write the code.

2. Code implementation

Step 1: Create the Visitor interface and implementation class

public interface Visitor {

	void visit(SimpleCard simpleCard);

	void visit(StudentCard studentCard);

	void visit(OldCard oldCard);
}
Copy the code
public class BusVisitor implements Visitor{

	@Override
	public void visit(SimpleCard simpleCard) {
		System.out.println("Didi ~ ordinary card, deduction fee 2 yuan");
	}

	@Override
	public void visit(StudentCard studentCard) {
		System.out.println("Didi ~ student card, deduction fee 1 yuan");
	}

	@Override
	public void visit(OldCard oldCard) {
		System.out.println("Didi ~ Senior citizen card, free"); }}Copy the code

Step 2: Create card abstract class and implementation class

public abstract class Card {

	protected Long amt;

	abstract void accept(Visitor visitor);

	public Card(Long amt) {
		super(a);this.amt = amt; }}Copy the code
public class SimpleCard extends Card{
	
	@Override
	void accept(Visitor visitor) {
		visitor.visit(this);		
	}

	public SimpleCard(Long amt) {
		super(amt); }}Copy the code
public class StudentCard extends Card{
	
	@Override
	void accept(Visitor visitor) {
		visitor.visit(this);		
	}

	public StudentCard(Long amt) {
		super(amt); }}Copy the code
public class OldCard extends Card{

	@Override
	void accept(Visitor visitor) {
		visitor.visit(this);
	}

	public OldCard(Long amt) {
		super(amt); }}Copy the code

Step 3: Create the client call

public class Client {

	public static void main(String[] args) {
		Visitor busVisitor = new BusVisitor();
		OldCard oldCard = new OldCard(100l);
		StudentCard studentCard = new StudentCard(200l);
		SimpleCard simpleCard = new SimpleCard(500l); oldCard.accept(busVisitor); studentCard.accept(busVisitor); simpleCard.accept(busVisitor); }}Copy the code

Test results:Here is a simple bus system to complete, have questions or improvement of the students welcome to comment ~

Summary of visitor model

1. Characteristics of the Visitor pattern

Advantages:

  1. Consistent with the principle of single responsibility
  2. It has excellent extensibility
  3. Very flexible

Disadvantages:

  1. Concrete elements publish details to visitors
  2. Specific elements have poor extensibility
  3. It violates the dependency inversion principle

2. Usage scenarios of the Visitor pattern

  1. The corresponding class of an object in an object structure rarely changes, but it is often necessary to define new operations on this object structure.
  2. There are many and unrelated operations to be performed on objects in an object structure, and the developer wants to avoid these operations contaminating the object class.

Four, conclusion

Today’s proxy mode explanation is all over, the related code of the design mode has been included in Gitee, you can take it directly if you need it:

Gitee.com/xiaolong-ob…

If you have any questions, you are welcome to leave a message in the comment section or send a private message to the blogger, who will answer them for you in the first time. The code word is not easy, feel the harvest of friends remember to pay attention to the blogger, do not be white whoring monster oh ~ blogger here wish you can be promoted in the New Year, to the peak of life!