Definition and Usage Analysis of visitor mode in JS design mode, and js Design Mode

Source: Internet
Author: User

Definition and Usage Analysis of visitor mode in JS design mode, and js Design Mode

This document describes the definition and usage of the visitor mode in the JS design mode. We will share this with you for your reference. The details are as follows:

The visitor mode mainly includes the following roles:

1. Abstract Visitor: abstract class or interface that declares what elements a visitor can access. Specifically, the parameters in the visit method define which objects can be accessed in the program.
2. Visitor: implement the method declared by the abstract visitor. It affects what the Visitor should do and what to do after accessing a class.
3. abstract element class: an interface or abstract class that declares which type of visitor access is accepted. It is defined by parameters in the accept method in the program. Abstract elements generally have two types of Methods: one is its own business logic, and the other is the type of visitors allowed to access.
4. Element class: implements the accept method declared by the abstract element class, which is usuallyvisitor.visit(this)Has basically formed a pattern.
5. Structure object: a container of an element. It generally contains a container containing multiple different classes and interfaces, such as List, Set, and Map, this role is rarely abstracted in projects.

Implementation Code:

// Visitor function Visitor () {this. visit = function (concreteElement) {concreteElement. doSomething () ;}/// function ConceteElement () {this. doSomething = function () {console. log ("this is a specific element");} this. accept = function (visitor) {visitor. visit (this) ;}}// Clientvar ele = new ConceteElement (); var v = new Visitor (); ele. accept (v );

Advantages of visitor Mode

1. comply with the single principle of responsibility: in any scenario applicable to the visitor mode, the operations in the element class that need to be encapsulated in the visitor must be operations that have little to do with the element class itself and are easy to change, on the one hand, the visitor mode conforms to the single responsibility principle. On the other hand, because encapsulated operations are usually changeable, when a change occurs, the element class itself can be kept unchanged, implement expansion of the change part.

2. good scalability: the element class can expand different operations by accepting different visitors.

Applicable scenarios of visitor Mode

1. if an object has some operations unrelated to the object (or the relationship is weak), in order to avoid these operations polluting the object, the visitor mode can be used to encapsulate these operations into the visitor.

2. If there are similar operations in a group of objects, in order to avoid a large number of repeated code, you can also encapsulate these repeated operations into the visitor.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.