Chain of Responsibility Design Pattern

Chain of Responsibility Design Pattern

Design Pattern:

A design pattern is a description or template that can be used in many different situations to solve a commonly occurring problem in software design.

Chain-of-responsibility Pattern:

The chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects.Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain.

Example:

Below is the example of this pattern using java. In this example we have different payment types, each having its own debit functionality. Every time a Payment utility receives a payment request, the request is passed to his successor, if it is of different payment type.

The Payment Abstract class with abstract method debitAmount.


/**
*
*/
package com.abhi.corpattern.chainservice;

/**
* @author Abhinav
*
*/
public abstract class Payment {

protected int paymentType;

protected Payment payment;

/**
* @param nextPayment
*/
public void setPaymentType(Payment nextPayment) {
this.payment = nextPayment;
}

/**
* @param paymentType
*/
public void executePayment(int paymentType) {

if (this.paymentType == paymentType) {
debitAmount();
}

if (payment != null) {
payment.executePayment(paymentType);
}
}

/**
*
*/
abstract protected void debitAmount();

}

Three implementation of the above mentioned abstract class:

1. CreditCardPayment


/**
*
*/
package com.abhi.corpattern.chainservice.impl;

import com.abhi.corpattern.chainservice.Payment;

/**
* @author Abhinav
*
*/
public class CreditCardPayment extends Payment {

/**
* @param type
*/
public CreditCardPayment(int type) {
this.paymentType = type;
}

/*
* (non-Javadoc)
*
* @see com.abhi.corpattern.chainservice.Payment#executePayment()
*/
@Override
protected void debitAmount() {
System.out.println("Payment has been done using Credit Card");
}

}

2. DebitCardPayment


/**
*
*/
package com.abhi.corpattern.chainservice.impl;

import com.abhi.corpattern.chainservice.Payment;

/**
* @author Abhinav
*
*/
public class DebitCardPayment extends Payment {

/**
* @param type
*/
public DebitCardPayment(int type) {
this.paymentType = type;
}

/*
* (non-Javadoc)
*
* @see com.abhi.corpattern.chainservice.Payment#executePayment()
*/
@Override
protected void debitAmount() {
System.out.println("Payment has been done using Debit Card");
}

}

3. NetBankingPayment


/**
*
*/
package com.abhi.corpattern.chainservice.impl;

import com.abhi.corpattern.chainservice.Payment;

/**
* @author Abhinav
*
*/
public class NetBankingPayment extends Payment {

/**
* @param type
*/
public NetBankingPayment(int type) {
this.paymentType = type;
}

/*
* (non-Javadoc)
*
* @see com.abhi.corpattern.chainservice.Payment#executePayment()
*/
@Override
protected void debitAmount() {
System.out.println("Payment has been done using Net Banking");
}

}

In the PaymentMain class, the successor are set as follows:

CreditCardPayment -> DebitCardPayment -> NetBankingPayment


/**
*
*/
package com.abhi.corpattern.main;

import com.abhi.corpattern.chainservice.Payment;
import com.abhi.corpattern.chainservice.impl.CreditCardPayment;
import com.abhi.corpattern.chainservice.impl.DebitCardPayment;
import com.abhi.corpattern.chainservice.impl.NetBankingPayment;
import com.abhi.corpattern.constant.PaymentConstant;

/**
* @author Abhinav
*
*/
public class PaymentMain {

/**
* @return
*/
public static Payment getChainOfPaymentType() {
Payment creditcardpayment = new CreditCardPayment(
PaymentConstant.CREDIT_CARD);
Payment debitcardpayment = new DebitCardPayment(
PaymentConstant.DEBIT_CARD);
Payment netbankingpayment = new NetBankingPayment(
PaymentConstant.NET_BANKING);

creditcardpayment.setPaymentType(debitcardpayment);
debitcardpayment.setPaymentType(netbankingpayment);

return creditcardpayment;
}

/**
* @param args
*/
public static void main(String[] args) {

Payment payment = getChainOfPaymentType();

payment.executePayment(PaymentConstant.DEBIT_CARD);
payment.executePayment(PaymentConstant.NET_BANKING);

}

}

In the PaymentConstant, three payment type constants defined


/**
*
*/
package com.abhi.corpattern.constant;

/**
* @author Abhinav
*
*/
public class PaymentConstant {

public static final int CREDIT_CARD = 1;
public static final int DEBIT_CARD = 2;
public static final int NET_BANKING = 3;

}

You can checkout/clone the chain of responsibility example repository from the mentioned path – https://github.com/erabhinavrana/my-chaindesign-pattern

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s