Download presentation
Presentation is loading. Please wait.
1
Leftover Patterns Chain of Responsibility
2
Synopsis The Chain of Responsibility pattern allows am object to send a command without knowing what objects will receive it It accomplishes that by passing the command to a chain of objects that is typically part of a larger structure Each object in the chain may handle the command, pass the command on to the next object in the chain, or do both
3
Writing software to monitor a security system
Context Writing software to monitor a security system The security system consist of sensing devices(motion detectors, smoke detectors, etc) A computer to which they transmit status information, maintain a display showing current status information, and transmit alarms in the event of an emergency
4
One of the goals for the monitoring
Context One of the goals for the monitoring highly scalable small retail store, office building, warehouse, multibuilding complex etc. To ensure scalability The Objects responsible for individual sensors should not assume anything about their environment except that they are at the bottom level of a hierarchical organization
5
Figure 8.1 Physical security object organization.
Force Figure 8.1 Physical security object organization.
6
The sending object does not care which object handles the command
Forces An object to be able to send a command to another object without specifying the receiver. The sending object does not care which object handles the command More than one object may be able to receive and handle a command
7
Solution FIGURE 8.2 Chain of Responsibility pattern.
8
CommandSender Solution
Instances of a CommandSender class send commands to the first object in a chain of objects that may handle the command. It sends a command by calling the first CommandHandler object’s postCommand method
9
CommandHandler Solution
The CommandHandler class is the superclass of all of the objects in the chain of objects that may handle a command. It defines two methods : 1. Each subclass of CommanHandler overrides the abstract handlerCommand method to handle whatever commands instances of that class will handle. 2. Subclasses of CommandHandler do not usually override the postCommand method The postCommand method calls the handlerCommand method
10
ConcreteCommandHandler1, ConcreteCommandHandler2
Solution ConcreteCommandHandler1, ConcreteCommandHandler2 Instances of classes in this role are objects in a chain of objects that can handle commands
11
Consequences The Chain of Responsibility pattern reduces coupling between the object that sends a command and the object that handles the command. The sender of a command does not need to know what object will actually handle the command. The Chain of Responsibility pattern also allows greater flexibility in deciding how to handle commands. The Chain of Responsibility pattern does not guarantee that every command will be handled.
12
There are two basic ways to do it
Implementation the objects that constitute a chain of responsibility are part of a larger structure the chain of responsibility is formed throusome links of that larger structure A decision to make, whenever implementing the Chain of Responsibility pattern, is how you will pass commands to and through the chain of objects. There are two basic ways to do it One way is to encapsulateeach kind of command in a single object that can be passed to a single postcommand method The other way is to have as many different types of postcommand and handleCommand methods as there are different types of information associated with commands
13
Java API Usage The first version of Java, version 1.0, used the Chain of Responsibility pattern to handle user-interface events. Some platforms generate many events that most GUls do not handle or have any interest in. One such event is MOUSE_MOVE The Chain of Responsibility pattern assumes that all the objects that can handle a command are instances of a common superclass or implement a common interface
14
Code Example FIGURE 8.3 Physical security classes Sensor SecurityZone
notify(measurement:int) notify(measurement:int, source:Sensor) handleNotification(measurement:int, source:Sensor):boolean fireAlarm(zone:SecurityZone) ….. TemperatureSensor MotionSensor …… Building Warehouse handleNotification(measurement:int, source:Sensor):boolean handleNotification(measurement:int, source:Sensor):boolean Floor Area handleNotification(measurement:int, source:Sensor):boolean handleNotification(measurement:int, source:Sensor):boolean Freezer Room handleNotification(measurement:int, source:Sensor):boolean handleNotification(measurement:int, source:Sensor):boolean FIGURE 8.3 Physical security classes
15
Code Example - class TemperatureSensor
class TemperatureSensor extends Sensor { private SecurityZone zone; void notify(int measurement) { zone.notify(measurement, this); } // notify(int) } // class TemperatureSensor
16
Code Example - class SecurityZone
abstract class SecurityZone { private SecurityZone parent; SecurityZone getParent() { return parent; } // getParent() void notify(int measurement, Sensor sensor) { if (!handleNotification(measurement, sensor) && parent != null) { parent.notify(measurement, sensor); } // if } // notify(int, Sensor) abstract boolean handleNotification(int measurement, Sensor sensor); void fireAlarm(SecurityZone zone) { // Turn on sprinklers //... if (parent != null) parent.fireAlarm(zone); } // fireAlarm(SecurityZone) } // class SecurityZone
17
Code Example - class Warehouse
class Warehouse extends SecurityZone { boolean handleNotification(int measurement, Sensor sensor) { //... return false; } // handleNotification(int, Sensor) void fireAlarm(SecurityZone zone) { if (zone instanceof Area) { if (getParent() != null) getParent().fireAlarm(zone); return; } // if super.fireAlarm(zone); } // fireAlarm(SecurityZone) } // class Warehouse
18
Related Pattern Composite Command Template Method
When the chain of objects used by the Chain of Responsibility pattern is part of a larger structure, that larger structure is usually built using the Composite Pattern Command The Chain of Responsibility pattern makes the particular object that executes a command indefinite. The Command Pattern makes the object that executes a command ecplict and specific Template Method When the objects that make up a chain of responsibility are part of a larger organization built using the Composite pattern, the Template Method pattern is often used to organize the behavior of individual objects
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.