Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.



Source code

/**
 * Handles request it is responsible to.
 */
public class ConcreteHandler extends Handler {
        /**
  * Handle request or delegate it.
  */
        public void handleRequest() {
                boolean canProcessThisRequest = false;                if (canProcessThisRequest) {
                        // handle request if possible
                } else {
                        // pass it to the next in chain
                        super.handleRequest();                }
        }

}

/**
 * Defines interface for request handling
 * @role __Handler
 */
public class Handler {
        private Handler successor;        /** Default request handling */
        public void handleRequest() {
                if (successor != null) {
                        successor.handleRequest();                }
        }

        public Handler getSuccessor() {
                return this.successor;        }

        public void setSuccessor(Handler successor) {
                this.successor = successor;        }

}




comments powered by Disqus

You might like also

Template method
Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses refine certain steps of an algorithm without changing the algorithm's structure.Source Code/** * This class defines abstract primitive operations that concrete * subclasses define to implement steps of an algorithm. Implements a template * method defining the skeleton of an algorithm. * * @role __TemplateContext */ public abstract class Context } /** * Implements the primitive operations to carry out subclass-specific steps …
5137 Days ago
State
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.Source Code/** * Defines an interface for encapsulating the behavior associated with a * particular state of the Context. * * @role __State */ public interface State /** * Defines an interface of interest to clients. Maintains an instance of a * ConcreteState subclass that defines the current state. */ public class Context public void someOperation() } /** * Implements a …
5137 Days ago
Memento
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.Source code/** * Creates a memento containing a snapshot of its current internal state. * Uses the memento to restore its internal state. * @role __Originator */ public class Originator public void setMemento(Memento memento) } } /** * Represents narrow interface of the memento visible to Caretaker * @role __Memento */ public interface Memento /** * Stores internal state of …
5144 Days ago
Mediator
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.Source code/** * Each colleague knows its Mediator object. Communicates with its mediator * whenever it would have otherwise communicated with another colleague. * * @role __Colleague */ public abstract class Colleague /** @return mediator this colleague knows about */ public Mediator getMediator() } /** Concrete Colleague */ public …
5144 Days ago
Interpreter
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.Source code/** * Declares an abstract Interpret operation that is common to all nodes in the * abstract syntax tree. * * @role __InterpreterExpression */ public abstract class AbstractExpression /** * Builds an abstract syntax tree representing a particular sentence in the * language that the grammar defines. Invokes the Interpret operation. */ public class Client public void …
5144 Days ago
Observer
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Source code/** * Knows its observers. Any number of Observer objects * may observe a subject. Provides an interface for attaching * and detaching Observer objects. * @role __Subject */ public class Subject public void detach(Observer observer) } protected void notifyObservers() } } /** * Defines an updating interface for objects that * should be notified of changes in …
5144 Days ago
Command
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Builds an undo/redo manager and a set of command classes.Source Code/** * Declares an interface for executing an operation * @role __Command */ public interface Command /** * Defines a binding betweeen a Receiver object and an action. * Implements Command by invoking the corresponding operation(s) on Receiver */ public class CommandA implements Command /** @see patterns.gof.command.CommandexecuteCommand()*/ public …
5144 Days ago
Facade
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.Source Code/** * Knows which subsystem classes are responsible for a request. * Delegates client requests to appropiate susystem objects. * @role __Facade */ public class Facade } /** * Implements subsystem functionality. Handles work assigned by the Facade object. * Has no knowledge of the facade. * @role __System */ public class SubsystemA } /** * …
5144 Days ago