The Chain of Responsibility
Transcript of The Chain of Responsibility
The Chain of Responsibility Pattern
• Some common applications– This pattern often appears with the composite pattern; the composite pattern forms a tree of object instances
– When a request is made at some object instance in the tree, if that instance cannot satisfy the request, it is passed to the parent
– At some point going up the tree, possibly at the root, the request will be handled
– Another common structure is a linear sequence of filters; for example, emails may be passed through filters for proper handling
• In the GoF classification this is a behavioral design pattern
Acknowledgements• Materials were borrowed from
– Design Patterns in Java by Steven Metsker and William Wake (textbook for course)
– Head First Design Patterns by Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra
Engineers at the Oozinoz Factory• There is a variety of equipment at the factory: machines, lines, bays, and the factory itself are machine components
• Every machine has a responsible engineer– A complex piece of equipment may have an engineer assigned directly to it
– For a simpler machine, there may be an engineer assigned for the entire production line
– When requested to find the responsible engineer, if the machine component does not know, it passes the request up to its parent; eventually a responsible engineer will be found
May or may not be assigned a value
Solution 12.1
Tools and Tool Carts• Tools are associated with a tool cart and the tool cart also has a responsible engineer
• Suppose a GUI allows the client to view machines, tools, and tool carts
• To find the responsible engineer for a tool, the system must find what tool cart it is on and find the responsible engineer for that tool cart
A Code Snippet• The code to handle these situations can become complex with a lot of it statements to check each possible condition
• Using a chain of responsibility can simplify things
• If we add the getResponsible method to all items then the calling method does not need to know which particular item it is dealing with
Solution 12.2
Solution 12.3
Anchoring a Chain• When climbing a chain of responsibility you must eventually reach the top of the chain– We can specify that each MachineComponent when constructed will specify its parent; only the top of the chain will have a null parent
– It is often convenient to establish a root class in the chain as a subclass of MachineComposite; it is the only MachineComponent allowed to have a null parent
Solution 12.4
The getResponsible Method
• Notice how simple the code has become
Solution 12.5
Prime Numbers – An Application• The fundamental theorem of arithmetic says that every positive integer can be factored into a unique product of prime numbers, such as1386 = 2 * 3 * 3 * 7 * 11
• This means we can tell a number is prime if it is not divisible by any prime number up to and including the square root of the number
• For example, to test if 719 is prime, we would try divisors 2, 3, 5, 7, 11, 13, 17, 19, 23; none of these divide into 719 without a remainder so we conclude 719 is prime
Using Filters• To generate primes, we need to filter out any number that can be divided by a smaller prime number up to the square root of the number
• Two is the only even prime, so we can restrict our attention to odd numbers
• We have a generator for all odd integers >= 3 and a collector for prime numbers
• Whenever a new prime number is detected we introduce a filter that removes all multiples of that new prime number
• Any number making it through all of the previous filters is a new prime number
Dynamic Introduction of Filters
Generator3, 5, 7, …
Collector3, 5, 7, 11, 13
Filter3
Filter5
Filter7
Filter11
Generator3, 5, 7, …
Collector3
Generator3, 5, 7, …
Collector3, 5
Filter3
Generator3, 5, 7, …
Collector3, 5, 7
Filter3
Filter5
Generator3, 5, 7, …
Collector3, 5, 7, 11
Filter3
Filter5
Filter7
3 is detected as primeA filter for multiples of 3 is introduced
5 is detected as primeA filter for 5’s is introduced
7 is detectedAdd filter for 7’s
Add filter for 11’s
Why This is a Chain of Responsibility• When a number is sent out from the generator along the chain, the generator does not know which process will handle this number
• If the number is a multiple of a smaller prime number, then it will be detected by one of the filters and not propagated further down the chain
• If the number makes it through all the filters, then it is received by the collector as a prime number; the collector is the anchor for the chain
• The collector adds in a new filter to remove multiples of this newly discovered prime