責任鏈模式

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   strong   for   

邏輯:

請假這個事情,相信每個人都不陌生。

我們公司是個相對很寬鬆的公司。

在公司裡,如果你的請假時間小於0.5天,那麼只需要向專案經理打聲招呼就OK了。

如果超過了0.5天,但是還小於2天,那麼就要去找人事部處理,當然,這就要扣工資了。

如果超過了2天,你就需要去找總經理了,工資當然也玩完了。

 

類圖:

 

 

 代碼:

Requester

package designMode.responsibility;/**  * @Package designMode.responsibility * @ClassName: Requester * @Description: TODO(這裡用一句話描述這個類的作用) * @author andy * @date 2014-8-26 下午4:35:56 */public class Requester {    private int day;    private String reason;    public int getDay() {        return day;    }    public void setDay(int day) {        this.day = day;    }    public String getReason() {        return reason;    }    public void setReason(String reason) {        this.reason = reason;    }    public Requester(int day, String reason) {        this.day = day;        this.reason = reason;    }}

Boss

package designMode.responsibility;/**  * @Package designMode.responsibility * @ClassName: Boss * @Description: TODO(這裡用一句話描述這個類的作用) * @author andy * @date 2014-8-26 下午4:38:29 */public abstract class Boss {    private String name;    private Boss successor;        public Boss(String str){        this.name = str;    }        public abstract boolean passRequest(Requester req);    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Boss getSuccessor() {        return successor;    }    public void setSuccessor(Boss successor) {        this.successor = successor;    }}

 

 

 ProjectManager

package designMode.responsibility;/**  * @Package designMode.responsibility * @ClassName: ProjectManager * @Description: TODO(這裡用一句話描述這個類的作用) * @author andy * @date 2014-8-26 下午4:43:12 */public class ProjectManager extends Boss {    public ProjectManager(String str) {        super(str);    }        public boolean passRequest(Requester req) {        if(null != req){            if(req.getDay() <=0.5){                System.out.println(this.getName() + "處理!");                return true;            }else{                this.getSuccessor().passRequest(req);            }        }        return false;    }}

HumanResource

package designMode.responsibility;/**  * @Package designMode.responsibility * @ClassName: HumanResource * @Description: TODO(這裡用一句話描述這個類的作用) * @author andy * @date 2014-8-26 下午4:46:06 */public class HumanResource extends Boss{        public HumanResource(String str) {        super(str);    }    public  boolean passRequest(Requester req){        if(null != req){            if(req.getDay()<=2){                System.out.println(this.getName() + "處理!");                return true;            }else{                this.getSuccessor().passRequest(req);            }        }        return false;    }}

Manager

package designMode.responsibility;/**  * @Package designMode.responsibility * @ClassName: Manager * @Description: TODO(這裡用一句話描述這個類的作用) * @author andy * @date 2014-8-26 下午4:50:17 */public class Manager extends Boss {    public Manager(String str) {        super(str);    }    public boolean passRequest(Requester req) {        if(null != req && "正當理由".equals(req.getReason())){            System.out.println(this.getName() + "處理!");            return true;        }        return false;    }}

MainTest

package designMode.responsibility;/**  * @Package designMode.responsibility * @ClassName: MainTest * @Description: TODO(這裡用一句話描述這個類的作用) * @author andy * @date 2014-8-26 下午5:02:52 */public class MainTest {        public static void main(String[] args) {        Requester requester = new Requester(1, "正當理由");        Boss a = new ProjectManager("專案經理");        Boss b = new HumanResource("人事部");        Boss c = new Manager("總經理");                a.setSuccessor(b);        b.setSuccessor(c);        a.passRequest(requester);    }}


控制台列印結果:人事部處理!

 

定義:待續

 

 

參考:  http://www.cnblogs.com/kym/archive/2009/04/06/1430078.html

 

責任鏈模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.