Command (komut) tasarım şablonu davranışsal (behavioral patterns) tasarım şablonları grubunda yer alır. Kullanım amacı kullanacağımız methodları nesne haline getirmek ve istediğimiz zaman çağırmak.
Küçük bir örnek vermek gerekirse
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Command { public function add() { $command = new Read(); $this->_commands []= $command; } public funtion run() { foreach($this->_commands as $cmd) { $cmd->execute(); } } } $command = new Command(); $command->add(); $command->add(); $command->add(); $command->add(); $command->run(); // dedigimizde tum komutları çalıştırır |
evet şimdi örneğimize geçebiliriz. örneğimizde bir sanal kitap uygulaması düşünelim. sanal kitabımızı gezen kullanıcıların kaç sayfa gezdiklerini tutacağız ve eski okudukları sayfaya dönmeleri için “undo” özelliği ve bunu geri alabilmek için “redo” özelliği sunacağız.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | interface ICommand { public function execute(); public function undo(); public function redo(); public function cevir(); } class Read implements ICommand { private $obj; public $act; public $count; public $_page; public function __construct($act, $count, $obj) { $this->obj = $obj; $this->act = $act; $this->count = $count; } public function execute() { $this->_page = $this->obj->Read($this->act, $this->count); echo $this->count.' sayfa ('.$this->act.') yönde değişti.<br>'; return true; } public function undo() { echo 'undo : '; $this->cevir(); $this->execute(); } public function redo() { echo 'redo : '; $this->cevir(); $this->execute(); } public function cevir() { $this->act = ($this->act == '+') ? '-' : '+'; } } class Book { private $_commands = array(); private $_redo = array(); private $_undo = array(); private $pager; public function __construct($obj) { $this->pager = new Pager(); } public function addCommand($act, $count) { $command = new Read($act, $count, $this->pager); $this->_commands []= $command; } public function activate() { foreach($this->_commands as $cmd) { if($cmd->execute()) { array_push($this->_undo, $cmd); } } } public function undo() { $cmd = array_pop($this->_undo); array_push($this->_redo, $cmd); $cmd->undo(); } public function redo() { $cmd = array_pop($this->_redo); array_push($this->_undo, $cmd); $cmd->redo(); } public function display() { $count = count($this->_undo); echo 'şu an '.$this->_undo[$count - 1]->_page.'. sayfadasınız'; } } |
uygulamamızı çalıştırmak için kullanacağımız kodumuz
1 2 3 4 5 6 7 8 9 10 11 12 | echo '<pre>'; $Book = new Book($page); $Book->addCommand('+', 5); $Book->addCommand('+', 1); $Book->addCommand('-', 3); $Book->addCommand('+', 4); $Book->addCommand('-', 6); $Book->activate(); $Book->undo(); $Book->undo(); $Book->redo(); $Book->display(); |
ekran çıktısı
5 sayfa (+) yönde değişti.
1 sayfa (+) yönde değişti.
3 sayfa (-) yönde değişti.
4 sayfa (+) yönde değişti.
6 sayfa (-) yönde değişti.
undo : 6 sayfa (+) yönde değişti.
undo : 4 sayfa (-) yönde değişti.
redo : 4 sayfa (+) yönde değişti.şu an 7. sayfadasınız
dosya : command design pattern
benzer konular:














design patterns