码迷,mamicode.com
首页 > 其他好文 > 详细

责任链模式

时间:2014-08-26 19:32:46      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   for   

逻辑:

请假这个事情,相信每个人都不陌生。

我们公司是个相对很宽松的公司。

在公司里,如果你的请假时间小于0.5天,那么只需要向项目经理打声招呼就OK了。

如果超过了0.5天,但是还小于2天,那么就要去找人事部处理,当然,这就要扣工资了。

如果超过了2天,你就需要去找总经理了,工资当然也玩完了。

 

类图:

 

bubuko.com,布布扣

 

 代码:

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

 

责任链模式

标签:des   style   blog   http   color   os   io   strong   for   

原文地址:http://www.cnblogs.com/o-andy-o/p/3937998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!