Lying design pattern notes (xxv) の Visitor pattern

Visitor Pattern

definition

It represents an operation of each element of an object structure effect. It makes you can not change the definition of the class under the premise of each element acting on the operation of these new elements.

UML 图

Feature

  • Visitor pattern applied to the data structure is relatively stable system, that the coupling between the operating data and acting on the structure to be freed, so that the operation can be set relatively freely evolution
  • The aim is to Visitor pattern processing data structure separate from the
  • If there is a data structure algorithms relatively stable, and easy to change, then use the visitor pattern is more appropriate, because the visitor pattern so as to increase the operative algorithm is easy to become
  • Visitor pattern is to increase the advantage of the new operation is very easy, because it means adding new operation adds a new visitor. The visitor pattern of behavior related to the concentration of a visitor object
  • The disadvantage is the fact, visitors to add new data structure becomes difficult.

For chestnuts

People, divided into men and women, respectively, describe his / her success, failure, for different reasons at love. . .

UML 图

Talk is cheap, show me the code

(Shit less, grading up)

/**
 * 状态
 * Created by callmeDevil on 2019/12/22.
 */
public abstract class Action {
    // 得到男人结论或反应
    public abstract void getManConclusion(Man concreteElementA);
    // 得到女人结论或反应
    public abstract void getWomanConclusion(Woman concreteElementB);
}
/**
 * 成功
 * Created by callmeDevil on 2019/12/22.
 */
public class Success extends Action {

    @Override
    public void getManConclusion(Man concreteElementA) {
        System.out.println(String.format("%s %s时,背后多半有一个伟大的女人。",
                concreteElementA.getClass().getSimpleName(), this.getClass().getSimpleName()));
    }

    @Override
    public void getWomanConclusion(Woman concreteElementB) {
        System.out.println(String.format("%s %s时,背后多半有一个不成功的男人。",
                concreteElementB.getClass().getSimpleName(), this.getClass().getSimpleName()));
    }

}
/**
 * 失败
 * Created by callmeDevil on 2019/12/22.
 */
public class Failing extends Action {

    @Override
    public void getManConclusion(Man concreteElementA) {
        System.out.println(String.format("%s %s时,背后多半有一个不伟大的女人。",
                concreteElementA.getClass().getSimpleName(), this.getClass().getSimpleName()));
    }

    @Override
    public void getWomanConclusion(Woman concreteElementB) {
        System.out.println(String.format("%s %s时,背后多半有一个成功的男人。",
                concreteElementB.getClass().getSimpleName(), this.getClass().getSimpleName()));
    }

}
/**
 * 恋爱
 * Created by callmeDevil on 2019/12/22.
 */
public class Amativeness extends Action {

    @Override
    public void getManConclusion(Man concreteElementA) {
        System.out.println(String.format("%s %s时,背后多半是个高富帅。",
                concreteElementA.getClass().getSimpleName(), this.getClass().getSimpleName()));
    }

    @Override
    public void getWomanConclusion(Woman concreteElementB) {
        System.out.println(String.format("%s %s时,背后多半是个会打扮。",
                concreteElementB.getClass().getSimpleName(), this.getClass().getSimpleName()));
    }

}
/**
 * 人
 * Created by callmeDevil on 2019/12/22.
 */
public abstract class Person {
    // 接受
    public abstract void accept(Action visitor);
}
/**
 * 男人
 * Created by callmeDevil on 2019/12/22.
 */
public class Man extends Person{
    @Override
    public void accept(Action visitor) {
        // 首先在客户端程序中将具体状态作为参数传递给“男人”类完成了一次分派,然后“男人”类调用
        // 作为参数的“具体状态”中的方法“男人反应”,同时将自己(this)作为参数传递进去,这便完成
        // 了第二次分派。这种技术手段称为“双分派”。
        visitor.getManConclusion(this);
    }
}
/**
 * 女人
 * Created by callmeDevil on 2019/12/22.
 */
public class Woman extends Person{
    @Override
    public void accept(Action visitor) {
        visitor.getWomanConclusion(this);
    }
}
/**
 * 对象结构
 * Created by callmeDevil on 2019/12/22.
 */
public class ObjectStructure {

    private List<Person> elements = new ArrayList<>();

    // 增加
    public void attach(Person element) {
        elements.add(element);
    }

    // 移除
    public void detach(Person element) {
        elements.remove(element);
    }

    // 查看显示
    public void display(Action visitor) {
        for (Person element : elements) {
            element.accept(visitor);
        }
    }

}
public class Test {
    public static void main(String[] args) {
        ObjectStructure obj = new ObjectStructure();
        // 对象结构中加入要对比的男人和女人
        obj.attach(new Man());
        obj.attach(new Woman());

        // 成功的反应
        Success success = new Success();
        obj.display(success);

        // 失败的反应
        Failing failing = new Failing();
        obj.display(failing);

        // 恋爱的反应
        Amativeness amativeness = new Amativeness();
        obj.display(amativeness);
    }
}

operation result

Man Success时,背后多半有一个伟大的女人。
Woman Success时,背后多半有一个不成功的男人。
Man Failing时,背后多半有一个不伟大的女人。
Woman Failing时,背后多半有一个成功的男人。
Man Amativeness时,背后多半是个高富帅。
Woman Amativeness时,背后多半是个会打扮。

Guess you like

Origin www.cnblogs.com/call-me-devil/p/12079781.html