1 介紹
小白:大哥,大學的報名費是不是很貴哇。
Acey:那得看學校和個人了,一般好些的學校學校就相對來說就能稍微少一些。主要還是看個人咯。
小白:哦?個人?
Acey:對呀,如果你有獎學金或者是助學金,甚至是貧困證明,你的學費都是可以減免的呢,說到這呀我又想到了一個模式,叫做職責鏈模式。
職責鏈模式:職責鏈模式是行為模式的一種,該模式構造了一系列分別擔當不同職責的類的對象來完成同一個任務,這些不同的對象像鎖鏈一樣緊密相連。
小白:額,我聽著咋感覺跟鎖鏈一樣呢,一環接著一環,環環相扣。??
Acey:可以這么理解喲,確實是一環接一環,只是每一個環都可以決定它的下一環是誰,當然每一個環也都有它獨有的職責。就拿報名費來說吧。去報名的時候,收費人員會查看你是否有獎學金,然后在查看助學金,最后看貧困證明,這樣一個鏈下來就是你該付的最終學費了。
小白:soga,好刺激呀??。那我得好好學習,爭取獎金全拿了。
Acey:呃呃,那你好好加油了。,,???,,,下面來實現這個功能吧。
2 實現
首先,先來看下職責鏈模式的類圖
其中
- Handler:責任類的抽象父類
- CncreteHandler:具體的責任類(獎學金、助學金...)
- Successor:Handler中的方法,用來設置當前環的下一環或獲取下一環
實現
第一步:創建處理類的抽象父類
Handler.class
public abstract class Handler {
//下一環對象(使用protected,讓子類可以訪問)
protected Handler handler = null;
//設置當前環的下一環
public void setSuccessor(Handler handler){
this.handler = handler;
}
//獲取當前環的下一環
public Handler getSuccessor() {
return handler;
}
//獲取當前環的學費
public abstract Integer getTuition(Student student, Integer tuition);
}
第二步:創建學生類,存儲學生獎金信息
Student.class
public class Student {
private String name;
private Integer Scholarship;//獎學金
private Integer grant;//助學金
private boolean isPoor;//是否貧困
//填充學生信息
public Student(String name, Integer scholarship, Integer grant,
boolean isPoor) {
super();
this.name = name;
Scholarship = scholarship;
this.grant = grant;
this.isPoor = isPoor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getScholarship() {
return Scholarship;
}
public void setScholarship(Integer scholarship) {
Scholarship = scholarship;
}
public Integer getGrant() {
return grant;
}
public void setGrant(Integer grant) {
this.grant = grant;
}
public boolean isPoor() {
return isPoor;
}
public void setPoor(boolean isPoor) {
this.isPoor = isPoor;
}
}
第三步:創建具體的責任鏈
Scholarship.class / Grant.class / Poor.class
//獎學金處理
public class Scholarship extends Handler{
@Override
public Integer getTuition(Student student, Integer tuition) {
//如果有獎學金學費減免
if(student.getScholarship() > 0){
tuition -= student.getScholarship();
System.out.println("獎學金減免后學費:"+tuition);
//如果有下一環就傳入下一環
if(this.getSuccessor() != null){
return this.getSuccessor().getTuition(student, tuition);
}
}
return tuition;
}
}
//助學金處理
public class Grant extends Handler{
public Integer getTuition(Student student, Integer tuition) {
//如果有助學金學費減免
if(student.getGrant() > 0){
tuition -= student.getGrant();
System.out.println("助學金減免后學費:"+tuition);
//如果有下一環就傳入下一環
if(this.getSuccessor() != null){
return this.getSuccessor().getTuition(student, tuition);
}
}
return tuition;
}
}
//貧困處理
public class Poor extends Handler{
public Integer getTuition(Student student, Integer tuition) {
//如果貧困,減免1000
if(student.isPoor()){
tuition -= 1000;
System.out.println("貧困減免后學費:"+tuition);
//如果有下一環就傳入下一環
if(this.getSuccessor() != null){
return this.getSuccessor().getTuition(student, tuition);
}
}
return tuition;
}
}
第四步:測試
MainClass.class
public class MainClass {
public static void main(String[] args) {
//先組裝責任鏈
Scholarship scholarship = new Scholarship();
Grant grant = new Grant();
Poor poor = new Poor();
scholarship.setSuccessor(grant);
grant.setSuccessor(poor);
//學生信息
Student xiaobai = new Student("小白", 2000, 1500, true);
Student zifan = new Student("張子凡", 1000, 500, false);
//開始減免學費
System.out.println(xiaobai.getName()+":"+scholarship.getTuition(
xiaobai, 8000));
System.out.println("-------------------------------------");
System.out.println(zifan.getName()+":"+scholarship.getTuition(zifan, 8000));
}
}
我們會發現,職責鏈模式的靈活性非常的好,每個責任類只需要處理自己該處理的任務,處理完成后就直接交給下一環,而且還可以根據需求自己設置當前環的下一環。就好比交學費的時候,每一位責任人員只對一種獎金處理,這樣就不會導致因報名學生過多導致堵塞現象,只需要每一位學生把所有的環鏈跑完就可以知道自己最終需要交的學費了。
喜歡的話戳一下喜歡唄。
有什么建議的話希望大家能在下方回復??
上一篇:《中介者模式 - 聽說你還是單身dog》
下一篇:《迭代模式 - 報告老師,我想逃課》