【设计模式】访问者模式

利用访问者模式模拟实现拓展客户管理系统(CRM)

涉及客户:
个人客户、企业用户
涉及服务:
提出服务申请、客户价值分析、客户对产品的偏好分析

package visitor;

public abstract class Analysis {

	public abstract void ManageIndividual(Individual i);

	public abstract void ManageEnterprise(Enterprise e);
}

package visitor;

public class PreferenceAnalysis extends Analysis{

	@Override
	public void ManageIndividual(Individual i) {
		// TODO Auto-generated method stub
		System.out.println("个人客户"+i.getName()+"对产品偏好分析");
	}

	@Override
	public void ManageEnterprise(Enterprise e) {
		// TODO Auto-generated method stub
		System.out.println("企业客户"+e.getName()+"对产品偏好分析");
	}

}

package visitor;

public class ServiceApplication extends Analysis{

	@Override
	public void ManageIndividual(Individual i) {
		// TODO Auto-generated method stub
		System.out.println("个人客户"+i.getName()+"提出服务申请");
	}

	@Override
	public void ManageEnterprise(Enterprise e) {
		// TODO Auto-generated method stub
		System.out.println("企业客户"+e.getName()+"提出服务申请");
	}

}

package visitor;

public class ValueAnalysis extends Analysis{

	@Override
	public void ManageIndividual(Individual i) {
		// TODO Auto-generated method stub
		System.out.println("个人客户"+i.getName()+"客户价值分析");
	}

	@Override
	public void ManageEnterprise(Enterprise e) {
		// TODO Auto-generated method stub
		System.out.println("企业客户"+e.getName()+"客户价值分析");
	}

}

package visitor;

public abstract class Customer {
	private String name;
	
	public Customer(String name) {
		
			this.name = name;
		}
	 
	public String getName() {
		return name;
	}
	 
	public void setName(String name) {
		this.name = name;
	}
	public abstract void Operate(Analysis a);
}

package visitor;

public class Enterprise extends Customer {

	public Enterprise(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void Operate(Analysis a) {
		// TODO Auto-generated method stub
		a.ManageEnterprise(this);
	}

}

package visitor;

public class Individual extends Customer{

	public Individual(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void Operate(Analysis a) {
		// TODO Auto-generated method stub
		a.ManageIndividual(this);
	}

}

package visitor;

import java.util.ArrayList;

public class ObjectStructure {
	private ArrayList<Customer> cl = new ArrayList<Customer>();

	public void Attach(Customer c) {
		cl.add(c);

	}

	public void Detach(Customer c) {
		cl.remove(c);
	}

	public void Display(Analysis a) {
		for (Customer c : cl)
			c.Operate(a);
	}

}

package visitor;

public class Main {
	public static void main(String args[]) {
		ObjectStructure os = new ObjectStructure();
		os.Attach(new Individual("CustomerA"));
		os.Attach(new Enterprise("CompanyB"));
		Analysis a1 = new ValueAnalysis();
		os.Display(a1);
		
		Analysis a2 = new PreferenceAnalysis();
		os.Display(a2);
		
		Analysis a3 = new ServiceApplication();
		os.Display(a3);

	}

}

UML图
【设计模式】访问者模式_第1张图片

你可能感兴趣的:(设计模式)