Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782434
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-17 10:38:57

指一个对象,要经过一系列的handler 来处理,例如java 的try catch block 里面去catch 不同的Exception. 

Usage in JDK:
java.util.logging.Logger#log()
javax.servlet.Filter #doFilter()

代码:

点击(此处)折叠或打开

  1. package chainofresponsibility;

  2. //This class is the request object
  3. class Number{
  4.     private int number;

  5.     public int getNumber(){
  6.         return number;
  7.     }

  8.     public Number(int num){
  9.         this.number = num;
  10.     }
  11. }

  12. //This is the interface that acts as a chain link
  13. interface Chain{
  14.     public void setNext(Chain nextInChain);
  15.     public void process(Number request);
  16. }

  17. class PositiveNumberProcessor implements Chain{
  18.     private Chain nextInChain;

  19.     @Override
  20.     public void setNext(Chain c){
  21.         nextInChain = c;
  22.     }

  23.     @Override
  24.     public void process(Number request){
  25.         if(request.getNumber() > 0){
  26.             System.out.println("PositiveNumberProcessor :"+request.getNumber());
  27.         }else{
  28.             nextInChain.process(request);
  29.         }
  30.     }
  31. }


  32. class NegativeNumberProcessor implements Chain{

  33.     private Chain nextInChain;

  34.     @Override
  35.     public void setNext(Chain c){
  36.         nextInChain = c;
  37.     }

  38.     @Override
  39.     public void process(Number request){
  40.         if(request.getNumber() < 0){
  41.             System.out.println("NegativeNumberProcessor :"+request.getNumber());
  42.         }else{
  43.             nextInChain.process(request);
  44.         }
  45.     }
  46. }

  47. class ZeroProcessor implements Chain{

  48.     private Chain nextInChain;

  49.     @Override
  50.     public void setNext(Chain c){
  51.         nextInChain = c;
  52.     }

  53.     @Override
  54.     public void process(Number request){
  55.         if(request.getNumber() == 0){
  56.             System.out.println("ZeroProcessor :"+request.getNumber());
  57.         }else{
  58.             nextInChain.process(request);
  59.         }
  60.     }
  61. }

  62. public class ChainOfResponsibilityDemo {
  63.     public static void main(String[] args){

  64.         //configure Chain of reponsibility
  65.         Chain c1 = new NegativeNumberProcessor();
  66.         Chain c2 = new ZeroProcessor();
  67.         Chain c3 = new PositiveNumberProcessor();
  68.         c1.setNext(c2);
  69.         c2.setNext(c3);

  70.         //Calling chain of responsibility
  71.         c1.process(new Number(99));
  72.         c1.process(new Number(-30));
  73.         c1.process(new Number(0));
  74.         c1.process(new Number(100));
  75.     }
  76. }


阅读(569) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~