Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chain of Responsibility Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.

Similar presentations


Presentation on theme: "Chain of Responsibility Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al."— Presentation transcript:

1 Chain of Responsibility Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.

2 Problem Calling a method tightly couples the sending and receiving objects –Requires the caller to know which object should perform the operation This is usually not a problem, but sometimes … –You need an operation performed –There are multiple objects that can perform it –The object that should perform the operation changes over time and depends on the context, and the caller doesn’t know which object to call You need a way to call a method without specifically saying which object should perform the operation

3 Solution Decouple sender and receiver by giving multiple objects a chance to handle a request When the sender needs to perform an operation, the request is passed along a chain of objects until one of them handles it The result is passed back along the chain to the sender The sender doesn't even know which object processed the request Any object that wants a chance to handle requests is added to the chain

4 Solution

5 Known Uses: UI Event Handling User interfaces are implemented as composites (i.e., trees) of "widgets"

6 Known Uses: UI Event Handling Events are handled bottom-up The chain of responsibility includes the leaf-level widget where the event begins and its ancestors Context-sensitive help can be handled the same way –ProcessHelp() instead of ProcessEvent()

7 Known Uses: Network Device Classification

8 When a device is discovered on a network, how do we figure out what kind of device it is (Windows, Linux, Mac, Printer, Switch, Router, etc.)? There are many heuristic algorithms one might use to “classify” a device –Query the device for information about itself using various network protocols (e.g., SNMP, WMI, …) –Determine vendor based on the device’s MAC address (e.g., Apple owns certain MAC address ranges) –Try to SSH into the device and figure out what it is –Etc. For any given device, some of these heuristic algorithms will work, and some won’t.

9 Known Uses: Network Device Classification Each heuristic algorithm is implemented by a different class called a “classifier” All of the classifier objects are linked in a “chain of responsibility” To classify a device, pass the device down the chain of classifiers until one of them figures out what the device is

10 public class PrinterClassifier : Classifier { public PrinterClassifier(Classifier successor) : base(successor) { return; } public DeviceType ClassifyDevice(Device device) { if (device exhibits printer characteristics) return DeviceType_Printer; else if (successor != null) return successor.ClassifyDevice(device); else return DeviceType_Unknown; } public abstract class Classifier { private Classifier successor; protected Classifier(Classifier successor) { this.successor = successor; } public Classifier Successor { get { return successor; } } public abstract DeviceType ClassifyDevice(Device device); }


Download ppt "Chain of Responsibility Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al."

Similar presentations


Ads by Google