一、以加薪**為例
//加薪**初步
class
request
public
void
setnumber
(int number)
public string getrequesttype()
public
void
setrequesttype
(string requesttype)
public string getrequestcontent()
public
void
setrequestcontent
(string requestcontent)
}class
manager
public
void
getresult
(string managerlevel, request request)
else
}else
if(managerlevel ==
"總監"
)else
}else
if(managerlevel ==
"總經理"
)else
if(request.
getrequesttype()
=="加薪"
&& request.
getnumber()
<=
500)
else
if(request.
getrequesttype()
=="加薪"
&& request.
getnumber()
>
500)}}
}public
class
chains
}
二、職責鏈模式
使多個物件都有機會處理請求,從而避免請求的傳送者和接收者之間的耦合關係。將這個物件連成一條鏈,並沿著這條鏈傳遞該請求,直到有乙個物件處理它為止。
abstract
class
handler
public
abstract
void
handlerequest
(int request);}
class
concretehandler1
extends
handler
else
if(successor != null)}}
class
concretehandler2
extends
handler
else
if(successor != null)}}
class
concretehandler3
extends
handler
else
if(successor != null)}}
public
class
chains
;for
(int request : requests)
}}
三、加薪**重構
class
request
public
void
setnumber
(int number)
public string getrequesttype()
public
void
setrequesttype
(string requesttype)
public string getrequestcontent()
public
void
setrequestcontent
(string requestcontent)
}abstract
class
manager
// 設定管理者的上級
public
void
setsuperior
(manager superior)
// 申請請求
abstract
public
void
(request request);}
class
commonmanager
extends
manager
@override
public
void
(request request)
else}}
}class
majordomo
extends
manager
@override
public
void
(request request)
else}}
}class
generalmanager
extends
manager
@override
public
void
(request request)
else
if(request.
getrequesttype()
=="加薪"
&& request.
getnumber()
<=
500)
else
if(request.
getrequesttype()
=="加薪"
&& request.
getnumber()
>
500)}}
public
class
chains
}
二十三種設計模式之責任鏈模式
責任鏈模式 如果有多個物件都機會處理請求,責任鏈可使請求的傳送者和接受者解耦,請求沿著責任鏈解耦,請求沿著責任鏈傳遞,直到有乙個物件處理他位置.public class purchaserequest public int gettype public float getsum public int...
設計模式(二十) 職責鏈模式
職責鏈模式,使多個物件都有機會處理請求,從而避免請求的傳送者和接收者之間的耦合關係。將這個物件連成一條鏈,並沿著這條鏈傳遞該請求,直到有乙個物件處理它為止。1.如下 using system namespace chainofresponsibility using system namespace...
設計模式(二十)職責鏈模式
職責鏈模式 chain of responsbility 使多個物件都有機會處理請求,從而避免請求的傳送者和接受者之間的耦合關係。將這個物件啊連城一條鏈,並沿著這條鏈傳遞改請求,直到有乙個物件處理他為止 public abstract class handler 處理請求的抽象方法 public a...