Decorator (dekoratör) tasarım şablonu yapısal (Structural Patterns) tasarım şablonları grubunda yer alır. Kullanım amacı sınıf yapısını değiştirmeden oluşturulan yeni nesneler ile yeni özelliklerin eklenmesidir. Decorator ile nesnemize yeni ek özellikler eklemiş olur. Bu tasarım şablonu yeni alt sınıf yaratmadan sınıfımıza yeni özellikler ekleyebilmemizi sağlar.
Bir örnek uygulama yaparak konuyu daha iyi kavrayalım. Bir otomobilimizin olduğunu düşünelim ve bunu modifiye yapalım. Modifiye yapacağımız kısımları ise tampon, jant ve egzos. bu kısımların hepsi birbirinden bağımsız ama hepsi otomobile ait kısımlar. fakat biz bunları otomobilden türetmeyeceğiz.

1- aksesuar sınıflarımızı oluşturalım
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 | abstract class AbstractOtomobil { protected $decorator; public function __construct( $decorator ) { $this -> decorator = $decorator; } abstract public function getAksesuar(); } class Tampon extends AbstractOtomobil { protected $aksesuar = 'mugen'; public function __construct( $decorator ) { parent::__construct( $decorator ); } public function getAksesuar() { return $this -> decorator -> getAksesuar().' + '. $this -> aksesuar;; } } class Egzos extends AbstractOtomobil { protected $aksesuar = 'remus'; public function __construct( $decorator ) { parent::__construct( $decorator ); } public function getAksesuar() { return $this -> decorator -> getAksesuar().' + '. $this -> aksesuar; } } class Jant extends AbstractOtomobil { protected $aksesuar = 'çelik'; public function __construct( $decorator ) { parent::__construct( $decorator ); } public function getAksesuar() { return $this -> decorator -> getAksesuar() .' + '. $this -> aksesuar; } } |
2- Otomobil sınıfımızı oluşturalım
1 2 3 4 5 6 7 8 9 10 11 | class Otomobil { private $aksesuar = 'Honda'; public function __construct() { } public function getAksesuar() { return $this -> aksesuar; } } |
evet şimdi bu sınıflar ile nasıl çalışacağımıza göz atalım
1 2 3 | $oto = new Tampon(new Egzos (new Jant(new Otomobil))); echo $oto->getAksesuar(); |
uygulamamızın ekran çıktısı
Honda + çelik + remus + mugen
bu konu ile ilgili anlatacaklarım bu kadar.
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 | <?php abstract class AbstractOtomobil { protected $decorator; public function __construct( $decorator ) { $this -> decorator = $decorator; } abstract public function getAksesuar(); } class Tampon extends AbstractOtomobil { protected $aksesuar = 'mugen'; public function __construct( $decorator ) { parent::__construct( $decorator ); } public function getAksesuar() { return $this -> decorator -> getAksesuar().' + '. $this -> aksesuar;; } } class Egzos extends AbstractOtomobil { protected $aksesuar = 'remus'; public function __construct( $decorator ) { parent::__construct( $decorator ); } public function getAksesuar() { return $this -> decorator -> getAksesuar().' + '. $this -> aksesuar; } } class Jant extends AbstractOtomobil { protected $aksesuar = 'çelik'; public function __construct( $decorator ) { parent::__construct( $decorator ); } public function getAksesuar() { return $this -> decorator -> getAksesuar() .' + '. $this -> aksesuar; } } class Otomobil { private $aksesuar = 'Honda'; public function __construct() { } public function getAksesuar() { return $this -> aksesuar; } } $oto = new Tampon(new Egzos (new Jant(new Otomobil))); echo $oto->getAksesuar(); ?> |
aşağıdaki bağlantılardan konuda anlattım örneği ve daha gelişmiş bir uygulamayı çekebilirsiniz.
dosya : decorator design pattern
dosya : phplibraries.com : decorator
benzer konular:















design patterns