文章目录
1. 需求
用代码实现男人和女人的区别
2. 代码版本1.0
2.1 人抽象类
public abstract class Person {
protected String action;
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
//得到结论或反应
public abstract void getConclusion();
}
2.2 男人和女人类
public class Man extends Person{
@Override
public void getConclusion() {
if (action=="成功"){
System.out.println(this.getClass().getSimpleName()+this.action+"时,背后多半有一个伟大的女人");
}else if(action=="失败"){
System.out.println(this.getClass().getSimpleName()+this.action+"时,闷头喝酒,谁也不用劝");
}else if(action=="恋爱"){
System.out.println(this.getClass().getSimpleName()+this.action+"时,凡是不懂也要装懂");
}
}
}
public class Woman extends Person{
@Override
public void getConclusion() {
if (action=="成功"){
System.out.println(this.getClass().getSimpleName()+this.action+"时,背后多半有一个不成功的男人");
}else if(action=="失败"){
System.out.println(this.getClass().getSimpleName()+this.action+"时,眼泪汪汪,谁也劝不了");
}else if(action=="恋爱"){
System.out.println(this.getClass().getSimpleName()+this.action+"时,遇事懂也装作不懂");
}
}
}
2.3 客户端代码
ArrayList<Person> persons = new ArrayList<>();
Person man1 = new Man();
man1.setAction("成功");
persons.add(man1);
Person woman1 = new Woman();
woman1.setAction("成功");
persons.add(woman1);
Person man2 = new Man();
man2.setAction("失败");
persons.add(man2);
Person woman2 = new Woman();
woman2.setAction("失败");
persons.add(woman2);
Person man3 = new Man();
man3.setAction("恋爱");
persons.add(man3);
Person woman3 = new Woman();
woman3.setAction("恋爱");
persons.add(woman3);
for (Person person : persons) {
person.getConclusion();;
}
2.4 结果
Man成功时,背后多半有一个伟大的女人
Woman成功时,背后多半有一个不成功的男人
Man失败时,闷头喝酒,谁也不用劝
Woman失败时,眼泪汪汪,谁也劝不了
Man恋爱时,凡是不懂也要装懂
Woman恋爱时,遇事懂也装作不懂
2.5 不足
代码中的if…else过多
3. 代码版本2.0
3.1 UML类图
3.2 "状态"的抽象类和"人"的抽象类
public abstract class Action {
//得到男人结论或反应
public abstract void getManConclusion(Man concreteElementA);
//得到女人结论或反应
public abstract void getWomanConclusion(Woman concreteElementA);
}
public abstract class Person {
//接受
public abstract void accept(Action visitor);
}
3.3 男人和女人类
这里需要提一下当中用到一种双分派的技术,首先在客户程序中将具体状态作为参数传递给’男人’类完成了一次分派,然后’男人’类调用作为参数的’具体状态’中的方法’男人反应’,同时将自己(this)作为参数传递进去。这便完成了第二次分派。双分派意味着得到执行的操作决定于请求的种类和两个接收者的类型。'接受’方法就是一个双分派的操作,它得到执行的操作不仅决定于’状态’类的具体状态,还决定于它访问的’人’的类别。
public class Man extends Person{
@Override
public void accept(Action visitor) {
visitor.getManConclusion(this);
}
}
public class Woman extends Person{
@Override
public void accept(Action visitor) {
visitor.getWomanConclusion(this);
}
}
3.4 对象结构类
由于总是需要男人与女人在不同状态的对比,所以我们需要一个对象结构类来针对不同的’状态’遍历男人与女人,得到不同的反应。
public class ObjectStructure {
private ArrayList<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);
}
}
}
3.5 客户端代码
ObjectStructure o = new ObjectStructure();
o.attach(new Man());
o.attach(new Woman());
//成功
Success v1 = new Success();
o.display(v1);
//失败
Failing v2 = new Failing();
o.display(v2);
//恋爱
Amativeness v3 = new Amativeness();
o.display(v3);
4. 需求增加
现在要增加”结婚“的状态来考查男人和女人的反应
4.1 思路
由于用了双分派,使得我只需要增加一个状态子类,就可以在客户端调用来查看,不需要改动其他任何类的代码
4.2 结婚状态类
public class Marriage extends Action{
@Override
public void getManConclusion(Man concreteElementA) {
System.out.println(concreteElementA.getClass().getSimpleName()+" "+"时,感慨道:恋爱游戏终结时,有妻徒刑遥遥无期");
}
@Override
public void getWomanConclusion(Woman concreteElementB) {
System.out.println(concreteElementB.getClass().getSimpleName()+" "+"时,欣慰曰:爱情长跑路漫漫,婚姻保险保平安");
}
}
4.3 客户端代码
ObjectStructure o = new ObjectStructure();
o.attach(new Man());
o.attach(new Woman());
//成功
Success v1 = new Success();
o.display(v1);
//失败
Failing v2 = new Failing();
o.display(v2);
//恋爱
Amativeness v3 = new Amativeness();
o.display(v3);
//婚姻
Marriage v4 = new Marriage();
o.display(v4);
5. 访问者模式
5.1 概念
访问者模式(Visitor),表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
5.2 应用场景
访问者模式的目的是要把处理从数据结构分离出来。很多系统可以按照算法和数据结构分开,如果这样的系统有比较稳定的数据结构,又有易于变化的算法的话,使用访问者模式就是比较合适的,因为访问者模式使得算法操作的增加变得容易。
反之,如果这样的系统的数据结构对象易于变化,经常要有新的数据对象增加进来,就不适合使用访问者模式。
5.3 优缺点
优点:
- 增加新的操作很容易,因为增加新的操作就意味着增加一个新的访问者。访问者模式将有关的行为集中到一个访问者对象中。
缺点: - 增加新的数据结构变得困难了
5.4 案例代码
5.4.1 UML类图
5.4.2 Visitor类
Visitor类,为该对象结构中ConcreteElement的每一个类声明一个Visit操作。
public abstract class Visitor {
public abstract void visitConcreteElementA(ConcreteElementA concreteElementA);
public abstract void visitConcreteElementB(ConcreteElementB concreteElementB);
}
5.4.3 具体访问者类
具体访问者,实现每个由Visitor声明的操作。每个操作实现算法的一部分,而该算法片断乃是对应于结构中对象的类。
public class ConcreteVisitor1 extends Visitor{
@Override
public void visitConcreteElementA(ConcreteElementA concreteElementA) {
System.out.println(concreteElementA.getClass().getSimpleName()+"被"+this.getClass().getSimpleName()+"访问");
}
@Override
public void visitConcreteElementB(ConcreteElementB concreteElementB) {
System.out.println(concreteElementB.getClass().getSimpleName()+"被"+this.getClass().getSimpleName()+"访问");
}
}
public class ConcreteVisitor2 extends Visitor{
@Override
public void visitConcreteElementA(ConcreteElementA concreteElementA) {
System.out.println(concreteElementA.getClass().getSimpleName()+"被"+this.getClass().getSimpleName()+"访问");
}
@Override
public void visitConcreteElementB(ConcreteElementB concreteElementB) {
System.out.println(concreteElementB.getClass().getSimpleName()+"被"+this.getClass().getSimpleName()+"访问");
}
}
5.4.4 Element类
Element类:定义一个Accept操作,它以一个访问者为参数
public abstract class Element {
public abstract void accept(Visitor visitor);
}
5.4.3 具体访问者类
ConcreteElementA和ConcreteElementB类:具体元素,实现Accept操作。
public class ConcreteElementA extends Element{
@Override
public void accept(Visitor visitor) {
visitor.visitConcreteElementA(this);
}
//其他相关操作
public void operationA(){
}
}
public class ConcreteElementB extends Element{
@Override
public void accept(Visitor visitor) {
visitor.visitConcreteElementB(this);
}
public void operationB(){}
}
5.4.6 ObjectStructure类
ObjectStructure类:能枚举它的元素,可以提供一个高层的接口以允许访问者访问它的元素。
public class ObjectStructure {
private ArrayList<Element> elements = new ArrayList<>();
//增加
public void attach(Element element){
elements.add(element);
}
//移除
public void detach(Element element){
elements.remove(element);
}
//查看显示
public void accept(Visitor visitor){
for (Element element : elements) {
element.accept(visitor);
}
}
}
客户端代码
ObjectStructure o = new ObjectStructure();
o.attach(new ConcreteElementA());
o.attach(new ConcreteElementB());
ConcreteVisitor1 v1 = new ConcreteVisitor1();
ConcreteVisitor2 v2 = new ConcreteVisitor2();
o.accept(v1);
o.accept(v2);