Tuesday, November 21, 2023

Chain Responsibility design pattern

 Chain Responsibility design pattern:

Chain responsibility design pattern is a behavioral design pattern . We will implement this design pattern to achieve loose coupling in software where several operation will execute one by one with same request . 

In this design pattern same request is pass through the chain until finish all the task in the chain .

Senarios: - Suppose we want to build an Notification sender application . That can notify user / developer through sms/ email etc . So we can create a class Notify user class and can implement message send logic there .  But in those logic there should have some other logic such as - loading credential information form db etc.

Another thing- in future we want to send notification to other , so respective service should execute one by one . If we don't follow a structured way and code base grows it will difficult to handle and need more refactoring .

So , we can create a chain of responsibility and execute one by one .

One service execute its work and can pass request to the next service in the chain .

Lets see the example of chain responsibility design pattern below-

1. Create an abstract class. Every service will override its execute method with same request

 AbstractService

public abstract class AbstractService {

    private AbstractService next;

    /**
     * Build the chain of objects
     */
    public static synchronized AbstractService link(AbstractService first, AbstractService... chain) {
        AbstractService head = first;
        for (AbstractService nextChain : chain) {
            head.next = nextChain;
            head = nextChain;
        }
        return first;
    }

    /**
     * Subclasses will implement this method with concrete checks.
     */
    public abstract Request execute(Request request);

    /**
     * Runs check on the next object in chain or ends traversing if we're in last in
     * the chain
     */
    protected Request executeNext(Request request) {
        if (next == null)
            return request;
        return next.executeNext(request);
    }

}

 Request:

public class Request {

    private String title;

    public void setTitile(String title) {
        this.title = title;
    }
}

AdminNotifyService:

public class AdminNotifyService extends AbstractService{

    @Override
    public Request execute(Request request) {
       
        System.out.println("AdminNotifyService.........");
       
        return executeNext(request);
    }
}

UserNotifyService:

public class UserNotifyService extends AbstractService{

    @Override
    public Request execute(Request request) {
       
        System.out.println("UserNotifyService.......");
       
        return executeNext(request);
    }
}

 MainApp:

public class MainApp {

    public static void main(String[] args) {
       
        Request request = new Request();
        request.setTitile("chain responsibility");
       
        AbstractService abstractService = AbstractService.link(new AdminNotifyService(), new UserNotifyService());
        abstractService.execute(request);
    }
}

No comments:

Post a Comment

Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...