Proxy (vekil) tasarım şablonu yapısal (structural patterns) tasarım şablonları gurubunda yer alır. Kullanım amacı başka bir nesneye kontrol ve erişim için vekillik sağlamaktır.

şimdi öğreniğimizi inceleyelim.
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 | <?php interface GeneratorInterface { public function showImage(); } // realsubject sınıfı class ImageGenerator implements GeneratorInterface { private $image; function __construct($image) { $this->image = $image; } function showImage() { echo $this->image.' resmi yuklendi<br>'; } } // proxy subject sınıfı class ImageGeneratorProxy implements GeneratorInterface { private $generator = null; private $image; function __construct($image) { $this->image = $image; } function showImage() { if($this->generator == null) { $this->generator = new ImageGenerator($this->image); echo "yeni sınıf olusturuldu - "; } else { echo "eski sınıf yuklendi - "; } $this->generator->showImage(); } } |
örneğimizi çalıştırırsak
1 2 3 4 5 6 | $proxy1 = new ImageGeneratorProxy('a'); $proxy2 = new ImageGeneratorProxy('b'); $proxy1->showImage(); $proxy2->showImage(); $proxy1->showImage(); |
ekran çıktısı
yeni sınıf olusturuldu – a resmi yuklendi
yeni sınıf olusturuldu – b resmi yuklendi
eski sınıf yuklendi – a resmi yuklendi
örnektede görüldüğü gibi ImageGeneratorProxy vekil atayarak ImageGenerator’ın showImage() fonksiyonunu çağırdık.
dosya : proxy design pattern
benzer konular:














design patterns