职责链模式-B

职责链模式实践


背景:

某公司内部规定,经理负责请假事务,员工最多可以请假3天;总监负责加薪事务,员工加薪不超过1000;现在某员工需要请假、加薪,通过职责链实现。

UML

      

代码

Request

package weic.om;

public class Request
{
	//申请类别--请假-加薪
	private String Type;
	//申请内容
	private String content;
	//申请数量
	private int numbers;

	public String getType()
	{
		return Type;
	}

	public void setType(String type)
	{
		Type = type;
	}

	public String getContent()
	{
		return content;
	}

	public void setContent(String content)
	{
		this.content = content;
	}

	public int getNumbers()
	{
		return numbers;
	}

	public void setNumbers(int numbers)
	{
		this.numbers = numbers;
	}
	
}

Manager

package weic.om;

public class Manager
{
	//管理者名字
	protected String name;
	public Manager(String name)
	{
		this.name = name;
	}
	
	//设置上级---链
	protected Manager Superior;
	public void setSuperior(Manager Superior)
	{
		this.Superior = Superior;
	}
	
	//申请请求处理
	public  void  RequestApplications(Request request)
	{
		
	}
	
}

Commonmanager

package weic.om;

//经理
public class Commonmanager extends Manager
{
	
	public Commonmanager(String name)
	{
		super(name);
	}

	//申请请求处理
	//经理处理---请假 数目<2
	public  void  RequestApplications(Request request)
	{
		if(request.getType().equals("请假") && request.getNumbers()<=3)
		{
			System.out.println(this.name+": "+request.getContent()+request.getNumbers()+"天"+" 被批准");
		}
		else if(request.getType().equals("请假") && request.getNumbers()>3)
		{
			System.out.println(this.name+": "+request.getContent()+request.getNumbers()+"天"+" 不批准");
		}
		else if (Superior != null) 
		{
			Superior.RequestApplications(request);
		}
	}
	
}


Majordomo

package weic.om;

//总监
public class Majordomo extends Manager 
{
	
	public Majordomo(String name)
	{
		super(name);
	}

	//申请请求处理
	//总监处理---请假 数目>5 + 加薪<1000  
	public  void  RequestApplications(Request request)
	{
		if(request.getType().equals("加薪") &&  request.getNumbers()<1000 )
		{
			System.out.println(this.name+": "+request.getContent()+" 涨薪"+request.getNumbers()+" 被批准");
		}
		else if(request.getType().equals("加薪"))
		{
			System.out.println(this.name+": "+request.getContent()+" 涨薪"+request.getNumbers()+" 以后再说");
		}
		else 
		{
			System.out.println(this.name+": "+request.getContent()+" 数目"+request.getNumbers()+" 这事我做不了主");
		}
	}
	
}
Client

package weic.om;

public class Client
{
	public static void main(String[] args)
	{
		
		//级别从低到高  经理->总监
		Manager jinli = new Commonmanager("金立");
		Manager zongjian = new Majordomo("宗间");
		
		jinli.setSuperior(zongjian);//设置链
		
		
		Request request1 = new Request();
		request1.setType("请假");
		request1.setContent("张三请假");
		request1.setNumbers(1);
		jinli.RequestApplications(request1);
		
		Request request2 = new Request();
		request2.setType("请假");
		request2.setContent("李四请假");
		request2.setNumbers(3);
		jinli.RequestApplications(request2);
		
		Request request3 = new Request();
		request3.setType("加薪");
		request3.setContent("王五想加薪");
		request3.setNumbers(3000);
		jinli.RequestApplications(request3);
		
		Request request4 = new Request();
		request4.setType("晋升");
		request4.setContent("sun想晋升");
		request4.setNumbers(1);
		jinli.RequestApplications(request4);
		
	}

}

结果:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值