Strategy (strateji) tasarım şablonu davranışsal (Behavioral Patterns) tasarım şablonudur. Bir işlem için birden fazla sınıf olduğunda elde olan parametrelere göre doğru sınıfın çağırılmasını sağlar. Bu işlem çalışma zamanında gerçekleştirilir. Strategy, prototype aksine çok fazla kullanılan bir kalıptır. Ufak ve büyük çaplı bir çok projede kullanabilir.
Şimdi bu tasarım şablonunu nerelerde kullanabileceğimizi düşünelim.
veritabanı işlemlerinde
3 değişik veritabanı kullandığımızı düşünelim mysql, mssql, oracle ve bu üç veri tabanına gerektiği zaman bağlantı kuracağız. ilk olarak bağlantıyı saglayan kodları tek bir sınıf altında toplarız ve bu sınıf sayesinde istenilen alt sınıfa ile bağlantı kurarız.
mail işlemlerinde
mesela bir web sitemiz var sitemizden kullanıcılara mail gönderiyoruz. mail gönderirken nasıl göndereceğimiz seçtiriyoruz. gönderme yöntemleri ise smtp ve sendmail olsun. bu işlemdede strategy tasarım şablonunu kullanmak doğru olacaktır.
localizasyon işlemlerinde
siteye giren kullanıcının diline göre lokalizasyon ayarlarını yaptırmak için yine bu tasarım şablonunu kullanabiliriz.
bu örnekleri çoğaltmak mümkün biz örnek bir uygulama yaparak konuyu daha iyi kavramaya çalışalım.

ilk olarak mail sınıfımızı oluşturuyoruz bu sınıf ile mail içeriği üzerinde yapacağımız işlemleri yapıyoruz.
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 | class Mail { private $from; private $to; private $body; function __construct($from_in, $to_in, $body_in) { $this->from = $from_in; $this->to = $to_in; $this->body = $body_in; } function getFrom() { return $this->from; } function getTo() { return $this->to; } function getBody() { return $this->body; } } |
bu mail içeriğini gönderecek olan smpt ve sendmail sınıflarımızı oluşturuyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | interface IMail { public function send($mailContent); } class Smtp implements IMail { public function send($mailContent) { echo 'Mail Smtp ile gönderildi<br>'; echo 'smtp->gonder('.$mailContent->getFrom().','.$mailContent->getTo().','.$mailContent->getBody().')<br>'; } } class SendMail implements IMail { private $passw = 'abcde'; public function send($mailContent) { echo 'Mail SendMail ile gönderildi<br>'; echo 'sendmail->gonder('.$mailContent->getFrom().','.$mailContent->getTo().','.$mailContent->getBody().','.$this->passw.')<br>'; } } |
son olarak hangi gönderme sınıfı seçecek olan strategy sınıfını oluşturuyoruz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class MailStrategy { private $strategy = NULL; public function __construct($strategy_in) { switch ($strategy_in) { case "smtp": $this->strategy = new Smtp(); break; case "sendmail": $this->strategy = new SendMail(); break; } } public function send($mailContent) { return $this->strategy->send($mailContent); } } |
şimdi sınıfı nasıl kullanacağımıza bakalım
1 2 3 4 5 6 7 8 9 10 | $mail = new Mail('gonderen@gondere.com', 'alan@alan.com', 'body'); $strategy_smtp = new MailStrategy('smtp'); $strategy_sendmail = new MailStrategy('sendmail'); echo '--- SMTP ---<br>'; $strategy_smtp->send($mail); echo '--- SENDMAIL ---<br>'; $strategy_sendmail->send($mail); |
bu uygulamanın ekran çıktısı aşağıdaki gibidir.
— SMTP —
Mail Smtp ile gönderildi
smtp->gonder(gonderen@gondere.com,alan@alan.com,body)
— SENDMAIL —
Mail SendMail ile gönderildi
sendmail->gonder(gonderen@gondere.com,alan@alan.com,body,abcde)
hepsini bir bütün olarak yazarsak
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 | <?php class MailStrategy { private $strategy = NULL; //bookList is not instantiated at construct time public function __construct($strategy_in) { switch ($strategy_in) { case "smtp": $this->strategy = new Smtp(); break; case "sendmail": $this->strategy = new SendMail(); break; } } public function send($mailContent) { return $this->strategy->send($mailContent); } } interface IMail { public function send($mailContent); } class Smtp implements IMail { public function send($mailContent) { echo 'Mail Smtp ile gönderildi<br>'; echo 'smtp->gonder('.$mailContent->getFrom().','.$mailContent->getTo().','.$mailContent->getBody().')<br>'; } } class SendMail implements IMail { private $passw = 'abcde'; public function send($mailContent) { echo 'Mail SendMail ile gönderildi<br>'; echo 'sendmail->gonder('.$mailContent->getFrom().','.$mailContent->getTo().','.$mailContent->getBody().','.$this->passw.')<br>'; } } class Mail { private $from; private $to; private $body; function __construct($from_in, $to_in, $body_in) { $this->from = $from_in; $this->to = $to_in; $this->body = $body_in; } function getFrom() { return $this->from; } function getTo() { return $this->to; } function getBody() { return $this->body; } } $mail = new Mail('gonderen@gondere.com', 'alan@alan.com', 'body'); $strategy_smtp = new MailStrategy('smtp'); $strategy_sendmail = new MailStrategy('sendmail'); echo '--- SMTP ---<br>'; $strategy_smtp->send($mail); echo '--- SENDMAIL ---<br>'; $strategy_sendmail->send($mail); ?> |
benim anlatacaklarım burada son buluyor yine bir önceki yazımda olduğu gibi bilgi alabileceğiniz bir kaç site adresini aşağıda veriyorum.
Five common PHP design patterns
PHP and Design Patterns
Design Patterns – Strategy and Bridge
dosya : strategy design pattern
benzer konular:
- bridge design pattern – php
- observer design pattern – php
- iterator design pattern – php
- chain of responsibility design pattern- php
- command design pattern – php
- memento design pattern – php
- visitor design pattern – php
- state design pattern – php
- flyweight design pattern – php
- template method design pattern – php














design patterns