Saturday, February 4, 2023
HomeJavaChain of Duty Sample in Java? Instance Tutorial

Chain of Duty Sample in Java? Instance Tutorial


Hi there guys, when you’ve got been doing Java improvement then you will need to have come throughout design sample, tried and examined resolution of widespread software program drawback. Up to now, now we have checked out a number of widespread Object Oriented Design Patterns in Java like Adapter, Decorator, State, Technique, Template, Composite, Command, Observer, Manufacturing facility, Builder, Customer and so on, and right now I’m going to speak about Chain of Duty design sample in Java. The Chained or Chain of Duty Sample is a design sample that enables a request to be handed alongside a sequence of objects till it’s dealt with by one of many objects within the chain. This sample is useful for conditions the place it’s not recognized prematurely which object within the chain ought to deal with the request, or the place the request must be dealt with by a number of objects within the chain.

Chained Or Chain Of Duty Sample with Examples

In
the Chained or Chain of Duty Sample, every object within the
chain has the chance to deal with the request, and whether it is unable to
accomplish that, it passes the request alongside to the subsequent object within the chain. This
permits for a versatile and dynamic method to request dealing with, because the
chain could be modified at runtime so as to add or take away objects as wanted.

In Java, the Chained or Chain of Duty Sample could be applied utilizing inheritance or composition.

1. Chain of Duty Sample utilizing Inheritance in Java

One
strategy to implement the Chained or Chain of Duty Sample in Java
is through the use of inheritance. On this method, a base class or interface
defines the strategies for dealing with a request and for setting the subsequent
object within the chain. Concrete subclasses or implementations of the bottom
class or interface then implement the request dealing with logic and might
override the tactic for setting the subsequent object within the chain to specify
the subsequent object within the chain for his or her specific case.

Right here is an instance of the Chained or Chain of Duty Sample utilizing inheritance in Java:

public interface RequestHandler {
    void handleRequest(Request request);
    void setNext(RequestHandler subsequent);
}

public class ConcreteRequestHandler1 implements RequestHandler {
    personal RequestHandler subsequent;

    @Override
    public void handleRequest(Request request) {
        if (request.getType() == RequestType.TYPE1) {
            
        } else {
            subsequent.handleRequest(request);
        }
    }

    @Override
    public void setNext(RequestHandler subsequent) {
        this.subsequent = subsequent;
    }
}

public class ConcreteRequestHandler2 implements RequestHandler {
    personal RequestHandler subsequent;

    @Override
    public void handleRequest(Request request) {
        if (request.getType() == RequestType.TYPE2) {
            
        } else {
            subsequent.handleRequest(request);
        }
    }

    @Override
    public void setNext(RequestHandler subsequent) {
        this.subsequent = subsequent;
    }
}



RequestHandler handler1 = new ConcreteRequestHandler1();
RequestHandler handler2 = new ConcreteRequestHandler2();
handler1.setNext(handler2);
handler1.handleRequest(new Request(RequestType.TYPE1));
handler1.handleRequest(new Request(RequestType.TYPE2));

In
this instance, the RequestHandler interface defines the strategies for
dealing with a request and setting the subsequent object within the chain. The
ConcreteRequestHandler1 and ConcreteRequestHandler2 lessons implement
the RequestHandler interface and supply the request dealing with logic for
their respective request sorts. 

The objects within the chain are created and
linked collectively utilizing the setNext() methodology, and the request is handed
alongside the chain utilizing the handleRequest() methodology.

2. Chain of Duty Sample utilizing Composition in Java

One other
strategy to implement the Chained or Chain of Duty Sample in Java
is through the use of composition. On this method, every object within the chain has
a reference to the subsequent object within the chain and implements the request
dealing with logic immediately. This method permits for extra flexibility and
reuse, because the objects within the chain could be composed at runtime and could be
shared amongst a number of chains.

Right here is an instance of the Chained or Chain of Duty Sample utilizing composition in Java:

ublic class RequestHandler {
    personal RequestHandler subsequent;

    public void handleRequest(Request request) {
        if (canHandleRequest(request)) {
            
        } else if (subsequent != null) {
            subsequent.handleRequest(request);
        }
    }

    public void setNext(RequestHandler subsequent) {
        this.subsequent = subsequent;
    }

    protected boolean canHandleRequest(Request request) {
        
        return false;
    }
}

public class ConcreteRequestHandler1 extends RequestHandler {
    @Override
    protected boolean canHandleRequest(Request request) {
        return request.getType() == RequestType.TYPE1;
    }
}

public class ConcreteRequestHandler2 extends RequestHandler {
    @Override
    protected boolean canHandleRequest(Request request) {
        return request.getType() == RequestType.TYPE2;
    }
}



RequestHandler handler1 = new ConcreteRequestHandler1();
RequestHandler handler2 = new ConcreteRequestHandler2();
handler1.setNext(handler2);
handler1.handleRequest(new Request(RequestType.TYPE1));
handler1.handleRequest(new Request(RequestType.TYPE2));

In
this instance, the RequestHandler class defines the essential construction of
an object within the chain and supplies the logic for passing the request
alongside the chain. The ConcreteRequestHandler1 and ConcreteRequestHandler2
lessons prolong the RequestHandler class and override the
canHandleRequest() methodology to offer the request dealing with logic for
their respective request sorts.

The objects within the chain are created and
linked collectively utilizing the setNext() methodology, and the request is handed
alongside the chain utilizing the handleRequest() methodology.

3. Benefits and Disadvantages

The
Chained or Chain of Duty Sample has a number of benefits and
disadvantages that needs to be thought-about when deciding whether or not to make use of it
in a venture.

Benefits:

  • Decoupling:
    The Chained or Chain of Duty Sample decouples the sender of a
    request from its receiver, permitting the objects within the chain to be
    modified or modified independently.
  • Flexibility: The Chained or
    Chain of Duty Sample permits for a versatile and dynamic
    method to request dealing with, because the chain could be modified at runtime to
    add or take away objects as wanted.
  • Reuse: The Chained or Chain of
    Duty Sample permits for the reuse of objects within the chain, as
    they are often shared amongst a number of chains.

Disadvantages:

  • Complexity:
    The Chained or Chain of Duty Sample can introduce further
    complexity to the system, because it requires the creation and administration of
    a number of objects within the chain.
  • Efficiency: The Chained or
    Chain of Duty Sample can have a unfavourable affect on
    efficiency, as every object within the chain has to course of the request,
    even when it’s not the ultimate handler.
  • Debugging: The Chained or
    Chain of Duty Sample could make it harder to debug the
    system, as it’s not at all times clear which object within the chain is
    answerable for dealing with a specific request.

Conclusion

The
Chained or Chain of Duty Sample is a design sample that
permits a request to be handed alongside a sequence of objects till it’s
dealt with by one of many objects within the chain. This sample is helpful for
conditions the place it’s not recognized prematurely which object within the chain
ought to deal with the request, or the place the request must be dealt with by
a number of objects within the chain. 

In Java, the Chained or Chain of
Duty Sample could be applied utilizing inheritance or
composition. Inheritance permits for a easy and easy
implementation, however it has restricted flexibility and reuse. Composition
permits for extra flexibility and reuse, however it requires extra complicated code
to handle the objects within the chain.

Different Java Design Patterns tutorials you could like

  • How you can design a Merchandising Machine in Java? (questions)
  • How you can implement a Decorator design sample in Java? (tutorial)
  • 5 Free Programs to be taught Object Oriented Programming (programs)
  • 7 Finest Programs to be taught Design Sample in Java (programs)
  • How you can implement the Technique Design Sample in Java? (instance)
  • 10 OOP Design Precept Each Java developer ought to be taught (strong precept)
  • 30 System Design Issues for interviews (System design issues)
  • How you can create thread-safe Singleton in Java (instance)
  • 7 Finest Books to be taught the Design Sample in Java? (books)
  • Distinction between Manufacturing facility and Dependency Injection Sample? (reply)
  • 20 Software program Design and Design Sample Interview questions (design questions)
  • Distinction between State and Technique Design Sample in Java? (reply)
  • 18 Java Design Sample Interview Questions with Solutions (listing)
  • 20 System Design Interview Questions (listing)
  • 5 Free Programs to be taught Knowledge Construction and Algorithms (programs)
  • 7 Books to be taught System Design for Newbies (System design books)
  • Distinction between Manufacturing facility and Summary Manufacturing facility Sample? (instance)

Thanks loads for studying this text to date. In the event you like this clarification and implementation of Chain of Duty Sample in Java  then please share it with your folks and colleagues. You probably have any
questions or suggestions then please drop a observe.

P. S. – If you’re an skilled Java developer and need to
be taught
Design patterns and on the lookout for a greatest design sample sources like
books and on-line programs then it’s also possible to checkout this listing of greatest design sample programs for expertise builders to start out with. It incorporates on-line programs to be taught design sample and how one can use them in actual world coding.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments