码迷,mamicode.com
首页 > 其他好文 > 详细

大话设计模式-职责链模式

时间:2016-05-12 12:30:16      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

需求
实现一个公司里请假加薪等流程审批的程序。
实现
级别1
public class Request 
{
	private String requestType;
	private int number;
	public String getRequestType()
	{
		return requestType;
	}
	public void setRequestType(String requestType)
	{
		this.requestType=requestType;
	}
	public int getNumber() 
	{
		return number;
	}
	public void setNumber(int number)
	{
		this.number=number;
	}
}
public class Manager 
{
	protected String name;
	public Manager(String name)
	{
		this.name=name;
	}
	public void GetResult(String managerLevel,Request request)
	{
		if(managerLevel=="manager")
		{
			if(request.getRequestType()=="leave"&&request.getNumber()<=2)
			{
				System.out.println("approval!");
			}
			else 
			{
				System.out.println("permission denied!");
			}
		}
		else if(managerLevel=="director")
		{
			if(request.getRequestType()=="leave"&&request.getNumber()<=5)
			{
				System.out.println("approval!");
			}
			else 
			{
				System.out.println("permission denied!");
			}
		}
		else if(managerLevel=="generalmanager")
		{
			if(request.getRequestType()=="leave")
			{
				System.out.println("approval!");
			}
			if(request.getRequestType()=="salary increase"&&request.getNumber()<=500)
			{
				System.out.println("approval!");
			}
			else if(request.getRequestType()=="salary increase"&&request.getNumber()>500)
			{
				System.out.println("refuse!");
			}
		}
	}
}
public class Main
{  	  
    public static void main(String[] args)
    {  
       Manager manager1=new Manager("Bob");
       Manager director1=new Manager("Alice");
       Manager generalmanager1=new Manager("Jennifer");
       Request request=new Request();
       request.setRequestType("salary increase");
       request.setNumber(200);
       manager1.GetResult("manager",request);
       director1.GetResult("director",request);
       generalmanager1.GetResult("generalmanager",request);
    }  
}
方法很长并且有很多的分支判断,是非常不好的设计。
级别2
public class Request 
{
	private String requestType;
	private int number;
	public String getRequestType()
	{
		return requestType;
	}
	public void setRequestType(String requestType)
	{
		this.requestType=requestType;
	}
	public int getNumber() 
	{
		return number;
	}
	public void setNumber(int number)
	{
		this.number=number;
	}
}
public abstract class Manager 
{
	protected String name;
	protected Manager superior;
	public Manager(String name)
	{
		this.name=name;
	}
	public void setSuperior(Manager superior)
	{
		this.superior=superior;
	}
	abstract public void RequestApplications(Request request);
}
public class CommonManager extends Manager
{
	public CommonManager(String name)
	{
		super(name);
	}
	public void RequestApplications(Request request)
	{
		if(request.getRequestType()=="leave"&&request.getNumber()<=2)
		{
			System.out.println("approval!");
		}
		else 
		{
			if(superior!=null)
			{
				superior.RequestApplications(request);
			}
		}
	}
}
public class Director extends Manager
{
	public Director(String name)
	{
		super(name);
	}
	public void RequestApplications(Request request)
	{
		if(request.getRequestType()=="leave"&&request.getNumber()<=5)
		{
			System.out.println("approval!");
		}
		else 
		{
			if(superior!=null)
			{
				superior.RequestApplications(request);
			}
		}
	}
}
public class GeneralManager extends Manager
{
	public GeneralManager(String name)
	{
		super(name);
	}
	public void RequestApplications(Request request)
	{
		if(request.getRequestType()=="leave")
		{
			System.out.println("approval!");
		}
		if(request.getRequestType()=="salary increase"&&request.getNumber()<=500)
		{
			System.out.println("approval!");
		}
		else if(request.getRequestType()=="salary increase"&&request.getNumber()>500)
		{
			System.out.println("refuse!");
		}
	}
}
public class Main
{  	  
    public static void main(String[] args)
    {  
       CommonManager commonmanager1=new CommonManager("Bob");
       Director director1=new Director("Alice");
       GeneralManager generalmanager1=new GeneralManager("Jennifer");
       commonmanager1.setSuperior(director1);
       director1.setSuperior(generalmanager1);
       Request request=new Request();
       request.setRequestType("salary increase");
       request.setNumber(200);
       commonmanager1.RequestApplications(request);
    }  
}  
职责链模式,使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将这个对象连成一条链,并沿着这条链传递该请求,知道有一个对象处理它为止。职责链模式能够降低耦合度,简化对象的相互连接,增强给对象指派职责的灵活性还能方便增加新的类。但是有些时候请求到达末端也可能得不到处理,并且代码调试时不太方便,可能会造成循环调用。如果有多个对象可以处理同一个请求,具体哪个对象处理该请求由运行时刻自动确定;在不明确指定接受者的情况下,向多个对象中的一个提交一个请求;可动态指定一组对象处理请求等几种情况时我们就可以使用职责链模式。JAVA中异常处理机制和早期的AWT事件模型都采用了职责链模式,熟悉这两部分的话应该很快能想到为什么要应用它。
技术分享
技术分享



大话设计模式-职责链模式

标签:

原文地址:http://blog.csdn.net/qq_32400847/article/details/51366548

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!