DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Two Implementations of the Chain Of Responsibility Pattern

Two Implementations of the Chain Of Responsibility Pattern

Cagdas Basaraner user avatar by
Cagdas Basaraner
·
Mar. 11, 12 · Java Zone · Interview
Like (0)
Save
Tweet
10.59K Views

Join the DZone community and get the full member experience.

Join For Free

 The Chain of Responsibility design pattern is needed when a few processors should exist for performing an operation and a particular order should be defined for those processors. Also the changeability of the order of processors on runtime are important.


UML represantation of the pattern is as below:

Handler defines the general structure of processor objects. "HandleRequest" here is the abstract processor method. Handler also has a reference of its own type, which represents the next handler. For this a public "setNextHandler" method should be defined and exactly the handler is an abstract class. 
ConcreteHandler define different representations of processors. At last, Client is responsible with creating required handlers (processors) and define a chain order between them.

Generally two diffent implementation may exist for this pattern. Difference is related with the "location of the chain routing business logic". Chain routing business logic may be either in Handler abstract class or ConcreteHandler classes, or both of them. Sample of first two approaches will be given below:

1. "Handler" has chain routing business logic:
    public abstract class Processor {
        protected Processor next;
        protected int threshold;
        public void setNextProcessor(Processor p) {
            next = p;
        }
        public void process(String data, int value) {
            if (value <= threshold) {
                process(data);
            }
            if (next != null) {
                next.message(data, threshold);
            }
        }
        abstract protected void processData(String data);
    }
    public class ProcessorA extends Processor {
       
        public ProcessorA (int threshold) {
            this.threshold = threshold;
        }
        protected void processData(String data) {
            System.out.println("Processing with A: " + data);
        }
    }
    public class ProcessorB extends Processor {
       
        public ProcessorB (int threshold) {
            this.threshold = threshold;
        }
        protected void writeMessage(String data) {
            System.err.println("Processing with B: " + data);
        }
    }
    public class Client {
        public static void main(String[] args) {
            Processor p, p1, p2;
            p1 = p = new ProcessorA(2);
            p2 = new ProcessorB(1);
            p1.setNextProcessor(p2);
            // Handled by ProcessorA
            p.process("data1", 2);
            // Handled by ProcessorA and ProcessorB
            p.process("data2", 1);
        }
    }

 
2. "ConcreteHandler"s have chain routing business logic: 
    public abstract class Processor {
        protected Processor next;
        protected int threshold;
        public void setNextProcessor(Processor p) {
            next = p;
        }
        abstract protected void processData(String data);
    }
    public class ProcessorA extends Processor {
       
        public ProcessorA (int threshold) {
            this.threshold = threshold;
        }
        protected void processData(String data, int value) {
            System.out.println("Processing with A: " + data);
            if (value >= threshold && next != null) {
                next.processData(data, value);
            }
        }
    }
    public class ProcessorB extends Processor {
       
        public ProcessorB (int threshold) {
            this.threshold = threshold;
        }
        protected void processData(String data, int value) {
            System.out.println("Processing with B: " + data);
            if (value >= threshold && next != null) {
                next.processData(data, value);
            }
        }
    }
    public class Client {
        public static void main(String[] args) {
            Processor p, p1, p2;
            p1 = p = new ProcessorA(2);
            p2 = new ProcessorB(1);
            p1.setNextProcessor(p2);
            // Handled by ProcessorA
            p.processData("data1", 1);
            // Handled by ProcessorA and ProcessorB
            p.processData("data2", 2);
        }
    }

 
 

 

 

Implementation

Published at DZone with permission of Cagdas Basaraner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Writing Beautiful, Optimized, and Better .NET Code With NDepend Static Analysis
  • How to Make Git Forget a Tracked File Now in .gitignore
  • How to Build Security for Your SaaS User Communications
  • How Java Apps Litter Beyond the Heap

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo