设计模式--访问者模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiao__jia__jia/article/details/86826371

                   设计模式--访问者模式

雇员管理系统遇到的问题

雇员管理系统遇到的问题:

需要添加一些新的操作功能

思考如何设计



Employee 

public class Employee {

	private String name;
	private float income;
	private int vacationDays;
	private int degree;

	public Employee(String name, float income, int vacationDays, int degree) {
		this.name = name;
		this.income = income;
		this.vacationDays = vacationDays;
		this.degree = degree;
	}

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

	public String getName() {
		return name;
	}

	public void setIncome(float income) {
		this.income = income;
	}

	public float getIncome() {
		return income;
	}

	public void setVacationDays(int vacationDays) {
		this.vacationDays = vacationDays;
	}

	public int getVacationDays() {
		return vacationDays;
	}

	public void setDegree(int degree) {
		this.degree = degree;
	}

	public int getDegree() {
		return degree;
	}

}

Employees 

import java.util.HashMap;

public class Employees {
	private HashMap<String, Employee> employees;

	public Employees() {
		employees = new HashMap<String, Employee>();
	}

	public void Attach(Employee employee) {
		employees.put(employee.getName(), employee);
	}

	public void Detach(Employee employee) {
		employees.remove(employee);
	}

	public Employee getEmployee(String name) {
		return employees.get(name);
	}

	public void getCompensation() {
		for (Employee employee : employees.values()) {

			System.out
					.println(employee.getName()
							+ "'s Compensation is "
							+ (employee.getDegree()
									* employee.getVacationDays() * 100));

		}

	}
}

MainTest 

import java.util.Random;

public class MainTest {
	public static void main(String[] args) {
		Employees mEmployees = new Employees();

		mEmployees.Attach(new Employee("Tom", 4500, 8, 1));
		mEmployees.Attach(new Employee("Jerry", 6500, 10, 2));
		mEmployees.Attach(new Employee("Jack", 9600, 12, 3));
		mEmployees.getCompensation();
	}

}


 

访问者模式原理



 

访问者模式:对于一组对象,在不改变数据结构的前提下,增加作用于这些结构元素新的功能。

适用于数据结构相对稳定,它把数据结构和作用于其上的操作解耦,使得操作集合可以相对自由地演化。
 


访问者模式优缺点

优点:

  符合单一职责原则

  扩展性良好

  有益于系统的管理和维护

缺点:

  增加新的元素类变得很困难

  破坏封装性
访问者模式示例代码讲解

Element 

public abstract class Element {
	abstract public void Accept(Visitor visitor);
}


Employee 

public class Employee extends Element {

	private String name;
	private float income;
	private int vacationDays;
	private int degree;

	public Employee(String name, float income, int vacationDays, int degree) {
		this.name = name;
		this.income = income;
		this.vacationDays = vacationDays;
		this.degree = degree;
	}

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

	public String getName() {
		return name;
	}

	public void setIncome(float income) {
		this.income = income;
	}

	public float getIncome() {
		return income;
	}

	public void setVacationDays(int vacationDays) {
		this.vacationDays = vacationDays;
	}

	public int getVacationDays() {
		return vacationDays;
	}

	public void setDegree(int degree) {
		this.degree = degree;
	}

	public int getDegree() {
		return degree;
	}

	@Override
	public void Accept(Visitor visitor) {
		// TODO Auto-generated method stub
		visitor.Visit(this);
	}

}


Visitor 

public interface Visitor {
	abstract public void Visit( Element element );
	
}

CompensationVisitor 

public class CompensationVisitor implements Visitor {

	@Override
	public void Visit(Element element) {
		// TODO Auto-generated method stub
		Employee employee = ((Employee) element);

		System.out.println(employee.getName() + "'s Compensation is "
				+ (employee.getDegree() * employee.getVacationDays() * 10));
	}

}

Employees 

import java.util.ArrayList;
import java.util.HashMap;

public class Employees {
	private HashMap<String, Employee> employees;

	public Employees() {
		employees = new HashMap();
	}

	public void Attach(Employee employee) {
		employees.put(employee.getName(), employee);
	}

	public void Detach(Employee employee) {
		employees.remove(employee);
	}

	public Employee getEmployee(String name) {
		return employees.get(name);
	}

	public void Accept(Visitor visitor) {
		for (Employee e : employees.values()) {
			e.Accept(visitor);
		}
	}
}

MainTest 

import java.util.Random;

public class MainTest {
	public static void main(String[] args) {
		Employees mEmployees = new Employees();

		mEmployees.Attach(new Employee("Tom", 4500, 8, 1));
		mEmployees.Attach(new Employee("Jerry", 6500, 10, 2));
		mEmployees.Attach(new Employee("Jack", 9600, 12, 3));

		mEmployees.Accept(new CompensationVisitor());

	}

}


 

访问者模式关键点

访问者模式原理

访问者模式:对于一组对象,在不改变数据结构的前提下,增加作用于这些结构元素新的功能。

注意事项:

  系统有比较稳定的数据结构

  与迭代器的关系
 

访问者模式适用场合

适用场合:

  如果一个系统有比较稳定的数据结构,又有经常变化的功能需求,那么访问者模式就是比较合适的

猜你喜欢

转载自blog.csdn.net/xiao__jia__jia/article/details/86826371