Skip to content

Instantly share code, notes, and snippets.

@juneym
Created July 15, 2011 01:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juneym/1083848 to your computer and use it in GitHub Desktop.
Save juneym/1083848 to your computer and use it in GitHub Desktop.
Task Object, Coordination & Chain of Responsibility Design pattern
Overview:
This was an example I have given to my co-developer who was doing some Task/Coodinator type of code. Hope this is helpful to many other developers out there.
Note that the example only supports one-to-one dependency between TaskObject instance. The TaskAbstract class can be easily modified to support 1-to-many dependency.
For simpicity, I have combined all three classes in this single gist:
1. TaskAbstract
2. TaskObject (extends TaskAbstract)
3. TaskCoordinator (extends TaskAbstract)
The TaskCoordinator class shows an example that resembles the following dependency:
1. A $queue contains multiple TaskObject instances
2. TaskObject."Raul" is dependent on TaskObject."Mark"
3. TaskObject."Mark" is dependent on TaskObject."Tim"
4. TaskObject."Faustino" is independent.
```php
$queue[] = new TaskObject("Raul");
$queue[] = new TaskObject("Mark");
$queue[] = new TaskObject("Tim");
$queue[] = new TaskObject("Faustino");
$queue[0]->setDependency($queue[1]);
$queue[1]->setDependency($queue[2]);
```
[Raul] -> [Mark] -> [Tim] [Faustino]
^ ^
| |
| |
[Coordinator] |
............. [Coordinator]
The output is:
Hello! I'm task: Tim
Hello! I'm task: Mark
Hello! I'm task: Raul
Hello! I'm task: Faustino
<?php
abstract class TaskAbstract {
const STATUS_READY = 1;
const STATUS_DONE = 2;
protected $dependency;
protected $status;
abstract public function run($caller);
public function getStatus() {
return $this->status;
}
public function setDependency(TaskAbstract $task) {
$this->dependency = $task;
}
}
class TaskObject extends TaskAbstract {
private $taskName = "";
public function __construct($taskName) {
$this->taskName = $taskName;
$this->status = self::STATUS_READY;
}
public function run($caller) {
if ($this->status == self::STATUS_DONE) {
return true;
}
if ($this->status != self::STATUS_READY) {
return false;
}
//run dependency if available
if (!empty($this->dependency)) {
$this->dependency->run($this);
}
echo "Hello! I'm task: {$this->taskName} \n";
$this->status = self::STATUS_DONE;
}
}
class TaskCoordinator extends TaskAbstract {
public function run( $caller) {
$queue = array();
$queue[] = new TaskObject("Raul");
$queue[] = new TaskObject("Mark");
$queue[] = new TaskObject("Tim");
$queue[] = new TaskObject("Faustino");
$queue[0]->setDependency($queue[1]);
$queue[1]->setDependency($queue[2]);
/*
$queue[0]->run($this);
$queue[3]->run($this);
$queue[3]->run($this);
*/
foreach ($queue as $q) {
$q->run($this);
}
}
}
$coordinator = new TaskCoordinator();
$coordinator->run(null);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment