4、實例:
請求類
package com.zcr.chain;
//申請
public class Request
{
//申請類別
private String requestType;
//申請內容
private String requestContent;
//數量
private int number;
public String getRequestType()
{
return requestType;
}
public void setRequestType(String requestType)
{
this.requestType = requestType;
}
public String getRequestContent()
{
return requestContent;
}
public void setRequestContent(String requestContent)
{
this.requestContent = requestContent;
}
public int getNumber()
{
return number;
}
public void setNumber(int number)
{
this.number = number;
}
}
package com.zcr.chain;
//管理者
public abstract class Manager
{
protected String name;
public Manager()
{
}
//管理者的上級
protected Manager superior;
public Manager(String name)
{
this.name = name;
}
//設置管理者的上級
public void SetSuperior(Manager superior)
{
this.superior = superior;
}
//申請請求
abstract public void RequestApplications(Request request);
}
package com.zcr.chain;
//經理
public class Majordomo extends Manager
{
public Majordomo(String name)
{
this.name = name;
}
@Override
public void RequestApplications(Request request)
{
//經理所能有的權限就是可准許下屬5天內的假期
if(request.getRequestType().equals("請假") && request.getNumber() <= 5)
{
System.out.println(name+":"+request.getRequestContent()+"數量"+request.getNumber()+"被批准");
}
else
{
//其余的申請都需轉到上級
if(null != superior)
{
superior.RequestApplications(request);
}
}
}
}
package com.zcr.chain;
//總經理
public class GeneralManager extends Manager
{
public GeneralManager(String name)
{
this.name = name;
}
@Override
public void RequestApplications(Request request)
{
//總經理允許任意時間假期
if(request.getRequestType().equals("請假"))
{
System.out.println(name+":"+request.getRequestContent()+"數量"+request.getNumber()+"被批准");
}
else if(request.getRequestType().equals("加薪") && request.getNumber() <=500)
{
System.out.println(name+":"+request.getRequestContent()+"數量"+request.getNumber()+"被批准");
}
else if(request.getRequestType().equals("加薪") && request.getNumber() >500)
{
System.out.println(name+":"+request.getRequestContent()+"數量"+request.getNumber()+"再說把");
}
}
}
package com.zcr.chain;
//經理
public class CommonManager extends Manager
{
public CommonManager(String name)
{
this.name = name;
}
@Override
public void RequestApplications(Request request)
{
//經理所能有的權限就是可准許下屬兩天內的假期
if(request.getRequestType().equals("請假") && request.getNumber() <= 2)
{
System.out.println(name+":"+request.getRequestContent()+"數量"+request.getNumber()+"被批准");
}
else
{
//其余的申請都需轉到上級
if(null != superior)
{
superior.RequestApplications(request);
}
}
}
}
package com.zcr.chain;
public class ChainTest
{
public static void main(String[] args)
{
CommonManager jinli = new CommonManager("經理");
Majordomo zongjian = new Majordomo("總監");
GeneralManager zongjingli = new GeneralManager("總經理");
//設置上機,完全根據實際需求來更改設置
jinli.SetSuperior(zongjian);
zongjian.SetSuperior(zongjingli);
Request request = new Request();
request.setRequestType("請假");
request.setRequestContent("XX來請假");
request.setNumber(1);
jinli.RequestApplications(request);
//客戶端的申請都是有“經理”發起,但實際上誰來決策由具體管理類來處理,客戶端不知道
Request request2 = new Request();
request2.setRequestType("請假");
request2.setRequestContent("XX來請假");
request2.setNumber(4);
jinli.RequestApplications(request2);
Request request3 = new Request();
request3.setRequestType("加薪");
request3.setRequestContent("XX請求加薪");
request3.setNumber(500);
jinli.RequestApplications(request3);
Request request4 = new Request();
request4.setRequestType("加薪");
request4.setRequestContent("XX請求加薪");
request4.setNumber(1000);
jinli.RequestApplications(request4);
}
}
5、結果:
6、書籍推薦
《大話設計模式》。上面的例子來自該書的改變,該書通過小菜和大鳥兩人在生活中遇到的事情引出設計模式來解決問題,富有趣味性。讀起來沒那麼枯燥無味。
大話設計模式(帶目錄完整版) PDF+源代碼 http://www.linuxidc.com/Linux/2014-08/105152.htm