Learn the Chain of Responsibility Design Pattern
-
Upload
prodigyview -
Category
Technology
-
view
3.528 -
download
1
description
Transcript of Learn the Chain of Responsibility Design Pattern
![Page 1: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/1.jpg)
Chain-Of-Responsibility Design Pattern
![Page 2: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/2.jpg)
OverviewObjective
Learn how to use the Chain Of Responsibility design pattern.
Requirements
Basics of Protected Methods
Basics of Abstract Classes
Estimated Time
6 minutes
www.prodigyview.com
![Page 3: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/3.jpg)
Follow Along With Code Example
1. Download a copy of the example code at www.prodigyview.com/source.
2.Install the system in an environment you feel comfortable testing in.
3.Proceed to examples/data/Command.php
![Page 4: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/4.jpg)
What is the Chain –Of-Responsibility?
The Chain-of-responsibility is a design pattern that has command objects and processing objects. The command objects iterates through the processing objects and executes on the processing objects that can handle the request.
Translated into English, a method in on object is called and it searches for other objects that can handle the methods request.
In ProdigyView,, the chain-of-responsibility is a little different. An object methods are ran through until the correct method is found and then that method is executed.
![Page 5: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/5.jpg)
Chain-of-Responsibility Visual
Method turnLeft();
Method turnRight();
Method lookUp();
Method lookDown();
Method turnAround()
Call Method lookUp()
Output
![Page 6: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/6.jpg)
Command in ProdigyView
The Chain-of-responsibility is implemented in ProdigyView by extending the PVApplication or PVStaticApplication.
Extend PVApplication
Required abstract function
![Page 7: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/7.jpg)
Command InterpreterSo genius, you have a class that is full of protected methods and no way to access them, how are we going to call them?
Well notice that our class extends PVApplication. PVApplication contains a public method called the commandInterpreter. The first argument will be the name of the method to execute. Any other arguments after that will be passed to the method.
PVApplication is an abstract class and the only method that is required for you to implement is the defaultFunction(). This will execute if no command is found. Let’s run some commands.
![Page 8: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/8.jpg)
Command Interpreter Example
The method that will be called
The arguements passed to the method
![Page 9: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/9.jpg)
Command Interpreter Results
The results from passing those commands should look like this.
![Page 10: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/10.jpg)
The Not So Obvious1. PVApplication extends PVObject which extends
PVPatterns. This means any object that extends PVApplication with has use of all the other design patterns as well the PVObject’s collections.
2. An object that extends PVApplication can be used as a stand alone object or be integrated in ProdigyView’s application system.
![Page 11: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/11.jpg)
Challenge!
This is an optional challenge designed to help you better understand the design pattern.
Create two classes that both have only protected methods and extends PVApplication or PVStaticApplication. Create at least 3 methods in each class.
Start the chain by calling one of the object’s command interpreter, and have each method call the other objects command interpreter until the default function is reached.
![Page 12: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/12.jpg)
Summary1. Have an object extend PVApplication
2. Create methods inside that object. They can be protected or public if they are going to be called by the commandInterpreter().
3. Execute a method inside that object by using the commandInterpeter and having the first argument passed be the name of the method and any other variables behind it will be passed to the called method as arguments.
![Page 13: Learn the Chain of Responsibility Design Pattern](https://reader034.fdocuments.net/reader034/viewer/2022042601/5481d35eb07959490c8b4627/html5/thumbnails/13.jpg)
API ReferenceFor a better understanding of the Collections and the Iterator, check out the api at the two links below.
PVApplication
www.prodigyview.com
More Tutorials
For more tutorials, please visit:
http://www.prodigyview.com/tutorials