Design patterns learning summary (xxii) - Visitor Pattern

definition

Visitor pattern is a representation of the operation of each element of an object structure effect, it allows us to define the role does not change the premise of the class of each element in the new operation of these elements. Visitor pattern applied to the data structure of the system is relatively stable.

Character

  • Vistor: abstract visitors. An operating structure for the object in each of the class declaration ConcreteElement.
  • ConcreteVisitor: specific visitors. Each operation to achieve Visitor stated, each operation implemented as part of the algorithm.
  • Element: abstract elements. Accept a defined operation, it is to a visitor as an argument.
  • ConcreteElement: specific elements. Accept achieve operation.
  • ObjectStructure: object structure. Able to enumerate its elements, we can provide a high-level interface to allow visitors to access its elements.

Advantages and disadvantages

advantage

  • Add a new access makes the operation easier.

  • Enables users without modifying the existing class hierarchy, class hierarchy define operations.

  • It will focus on the access behavior of elements related to object to a visitor object, instead of scattered elements engage in one class.

Shortcoming

  • Add new elements class is difficult. In the visitor pattern, each adding a new element classes are meant to add a new abstract operation in the role of visitors in the abstract, and a corresponding increase in the specific operation of each visitor to a specific class, contrary to the "opening and closing requirements principle ".

  • Violate encapsulation. When using the visitor pattern, the package will break class combination.

  • More difficult to understand. It looks like the most difficult design patterns.

Examples

For example medication to pharmacies

Abstract medicine:

/**
 * 抽象药
 */
public abstract class Medicine {
    protected String name;
    protected double price;

    public Medicine (String name,double price){
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

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

    public abstract void accept(Visitor visitor);
}

Specific drugs:

public class MedicineA extends Medicine{

    public MedicineA(String name, double price) {
        super(name, price);
    }

    public void accept(Visitor visitor) {
        visitor.visitor(this);
    }
}

public class MedicineB extends Medicine{

    public MedicineB(String name, double price) {
        super(name, price);
    }

    public void accept(Visitor visitor) {
        visitor.visitor(this);
    }
}

Visitors abstract:

/**
 *  抽象访问者
 */
public abstract class Visitor {
    protected String name;

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


    public abstract void visitor(MedicineA a);
    public abstract void visitor(MedicineB a);

}

Specific Visitors:

/**
 * 药房工作者
 */
public class WorkerOfPharmacy extends Visitor{

    public void visitor(MedicineA a) {
        System.out.println("药房工作者:" + name + "拿药 :" + a.getName());
    }

    public void visitor(MedicineB b) {
        System.out.println("药房工作者:" + name + "拿药 :" + b.getName());
    }

}

/**
 * 划价员
 */
public class Charger  extends Visitor{

    @Override
    public void visitor(MedicineA a) {
        System.out.println("划价员:" + name +"给药" + a.getName() +"划价:" + a.getPrice());
    }
    @Override
    public void visitor(MedicineB b) {
        System.out.println("划价员:" + name +"给药" + b.getName() +"划价:" + b.getPrice());
    }
}

prescription:

public class Presciption {
    List<Medicine> list = new ArrayList<Medicine>();

    public void accept(Visitor visitor){
        Iterator<Medicine> iterator = list.iterator();

        while (iterator.hasNext()) {
            iterator.next().accept(visitor);
        }
    }

    public void addMedicine(Medicine medicine){
        list.add(medicine);
    }

    public void removeMedicien(Medicine medicine){
        list.remove(medicine);
    }
}

test:

public static void main(String[] args) {
    Medicine a = new MedicineA("板蓝根", 11.0);
    Medicine b = new MedicineB("感康", 14.3);

    Presciption presciption = new Presciption();
    presciption.addMedicine(a);
    presciption.addMedicine(b);

    Visitor charger = new Charger();
    charger.setName("张三");

    Visitor workerOfPharmacy = new WorkerOfPharmacy();
    workerOfPharmacy.setName("李四");

    presciption.accept(charger);
    System.out.println("-------------------------------------");
    presciption.accept(workerOfPharmacy);
}

Console output:

划价员:张三给药板蓝根划价:11.0
划价员:张三给药感康划价:14.3
-------------------------------------
药房工作者:李四拿药 :板蓝根
药房工作者:李四拿药 :感康

Guess you like

Origin www.cnblogs.com/markLogZhu/p/11582731.html