设计模式之访问者模式

场景:男人和女人,玩游戏时有不同的状态。

首先设计一个抽象人类,再设计一个男人和女人类继承该抽象类;这两个类当做被访问的元素;

再设计一个访问者接口,和一个赢游戏时状态和输游戏时状态实现该接口;

接着是一个管理被访问元素的类,最后是客户端主类。

 

抽象类和被访问元素类:

package com.freshbin.pattern.visitor.myexample.element;

import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;

/**
 * Person抽象类
 * 
 * @author freshbin
 * @date 2019年1月27日 下午4:00:55
 */
public abstract class Person {
	private String type;
	
	public Person(String type) {
		this.type = type;
	}
	
	public abstract void accept(StateInterface stateInterface);

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
package com.freshbin.pattern.visitor.myexample.element;

import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;

/**
 * 男人类
 * 
 * @author freshbin
 * @date 2019年1月27日 下午4:06:34
 */
public class Man extends Person {

	public Man() {
		super("男人");
	}

	@Override
	public void accept(StateInterface stateInterface) {
		stateInterface.visitPerson(this);
	}
	
}
package com.freshbin.pattern.visitor.myexample.element;

import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;

/**
 * 女人类
 * 
 * @author freshbin
 * @date 2019年1月27日 下午4:06:34
 */
public class Woman extends Person {

	public Woman() {
		super("女人");
	}

	@Override
	public void accept(StateInterface stateInterface) {
		stateInterface.visitPerson(this);
	}
	
}

访问者接口和具体类:

package com.freshbin.pattern.visitor.myexample.visitor;

import com.freshbin.pattern.visitor.myexample.element.Person;

/**
 * 状态接口
 * 
 * @author freshbin
 * @date 2019年1月27日 下午4:08:16
 */
public interface StateInterface {
	public void visitPerson(Person person);
}
package com.freshbin.pattern.visitor.myexample.visitor;

import com.freshbin.pattern.visitor.myexample.element.Person;

/**
 * WinGame
 * 
 * @author freshbin
 * @date 2019年1月27日 下午4:13:15
 */
public class WinGame implements StateInterface {

	@Override
	public void visitPerson(Person person) {
		switch (person.getType()) {
		case "男人":
			System.out.println("打游戏时赢了:" + person.getType() + "会淡定!");
			break;
			
		case "女人":
			System.out.println("打游戏时赢了:" + person.getType() + "会欣喜!");
			break;

		default:
			break;
		}
	}
}
package com.freshbin.pattern.visitor.myexample.visitor;

import com.freshbin.pattern.visitor.myexample.element.Person;

/**
 * LoseGame
 * 
 * @author freshbin
 * @date 2019年1月27日 下午4:13:15
 */
public class LoseGame implements StateInterface {

	@Override
	public void visitPerson(Person person) {
		switch (person.getType()) {
		case "男人":
			System.out.println("打游戏时输了:" + person.getType() + "会淡定!");
			break;
			
		case "女人":
			System.out.println("打游戏时输了:" + person.getType() + "会伤心!");
			break;

		default:
			break;
		}
	}
}

管理被访问元素的类:

package com.freshbin.pattern.visitor.myexample;

import java.util.LinkedList;

import com.freshbin.pattern.visitor.myexample.element.Person;
import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;

public class ObjectStructure {
	private LinkedList<Person> element = new LinkedList<>();
	
	public void addElement(Person person) {
		element.add(person);
	}
	
	public void display(StateInterface stateInterface) {
		element.parallelStream().forEach((person) -> {
			person.accept(stateInterface);
		});
	}
}

客户端主类:

package com.freshbin.pattern.visitor.myexample;

import com.freshbin.pattern.visitor.myexample.element.Man;
import com.freshbin.pattern.visitor.myexample.element.Person;
import com.freshbin.pattern.visitor.myexample.element.Woman;
import com.freshbin.pattern.visitor.myexample.visitor.LoseGame;
import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;
import com.freshbin.pattern.visitor.myexample.visitor.WinGame;

/**
 * 访问者模式
 * 
 * @author freshbin
 * @date 2019年1月27日 下午4:18:27
 */
public class VisitorPatternDemo {
	public static void main(String[] args) {
		Person man = new Man();
		Person woman = new Woman();
		
		StateInterface winGame = new WinGame();
		StateInterface loseGame = new LoseGame();
		
		ObjectStructure objectStructure = new ObjectStructure();
		objectStructure.addElement(man);
		objectStructure.addElement(woman);
		
		objectStructure.display(winGame);
		
		System.out.println("=======================================");
		
		objectStructure.display(loseGame);
	}
}

效果图:

github地址:https://github.com/freshbin/designPattern

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值