Adapter (adaptör) tasarım şablonu davranışsal (Structural Patterns) tasarım şablonudur ve var olan 2 sınıfı bir birine bağlama görevi görür. Böylece bu birbirine bağladığımız iki sınıfın eski yapılarını korumuş oluruz.
Konuyu daha iyi anlamak için örneğimize göz atalım.
Örneğimizde 2 adet sınıfımız olmalı ve biz Adapter sınıfı ile bunları bir birine bağlamalıyız. Örnek olarak 2 sınavda alınan notların ortalamasını yazan bir uygulama yapalım.

1. sınıfımız öğrencilerin bilgilerinin bulanacağı sınıf.
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 | class Ogrenci { public $isim; public $not1; public $not2; public function __construct( $isim, $not1, $not2 ) { $this->isim = $isim; $this->not1 = $not1; $this->not2 = $not2; } } class OgrenciNotlari { private $array = array(); public function kayitEkle($isim, $not1, $not2) { $this->array []= new Ogrenci( $isim, $not1, $not2 ); } public function kayitGetir() { return $this->array; } } ?> |
2. sınıfımız ise öğrencilerden gelen not bilgileri hesaplayacak olan sınıf olsun
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 | <?php abstract class AbstractOrtalama { abstract function mevcutGetir( ); abstract function isimGetir( $row ); abstract function not1Getir( $row ); abstract function not2Getir( $row ); } class Ortalama { private $data; public function __construct( $data ) { $this->data = $data; } public function hesapla() { $mevcut = $this->data->mevcutGetir(); for($i = 0; $i < $mevcut; $i++) { $ortalama = ( $this->data->not1Getir( $i ) + $this->data->not2Getir( $i ) ) / 2; echo $this->data->isimGetir( $i ) .' - '. $ortalama .'<br>'; } } } |
3. sınıfımız ise bu 2 sınıfı birbirine bağlayacak olan Adapter sınıfı
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 | class Adapter extends AbstractOrtalama { private $array; public function __construct( $rl ) { $this->array = $rl->kayitGetir(); } public function mevcutGetir() { return count($this->array); } public function isimGetir( $get ) { return $this->array[ $get ]->isim; } public function not1Getir( $get ) { return $this->array[ $get ]->not1; } public function not2Getir( $get ) { return $this->array[ $get ]->not2; } } |
evet şimdi bu sınıflar ile nasıl çalışacağımıza göz atalım
1 2 3 4 5 6 7 8 9 | $ogrenci = new OgrenciNotlari; // öğrenci sınıfını oluştur $ogrenci->kayitEkle('emre', 50, 70); // yeni ogrenci ekle $ogrenci->kayitEkle('erdem', 90, 80); // yeni ogrenci ekle $adapter = new Adapter( $ogrenci ); // adapter sınıfını oluştur - öğrenci bilgilerini adapter sınıfına sokuyoruz $hesapla = new Ortalama( $adapter ); // öğrenci bilgilerini bulunduran adapter sınıfını ortalama hesaplayan sınıfa gönderiyoruz $hesapla->hesapla(); |
uygulamamızın ekran çıktısı
emre – 60
erdem – 85
evet bu yazımızda burada son buluyor. umarım açıklayıcı olmuştur.
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 93 94 95 96 97 | <?php abstract class AbstractOrtalama { abstract function mevcutGetir( ); abstract function isimGetir( $row ); abstract function not1Getir( $row ); abstract function not2Getir( $row ); } class Ortalama { private $data; public function __construct( $data ) { $this->data = $data; } public function hesapla() { $mevcut = $this->data->mevcutGetir(); for($i = 0; $i < $mevcut; $i++) { $ortalama = ( $this->data->not1Getir( $i ) + $this->data->not2Getir( $i ) ) / 2; echo $this->data->isimGetir( $i ) .' - '. $ortalama .'<br>'; } } } class Ogrenci { public $isim; public $not1; public $not2; public function __construct( $isim, $not1, $not2 ) { $this->isim = $isim; $this->not1 = $not1; $this->not2 = $not2; } } class OgrenciNotlari { private $array = array(); public function kayitEkle($isim, $not1, $not2) { $this->array []= new Ogrenci( $isim, $not1, $not2 ); } public function kayitGetir() { return $this->array; } } class Adapter extends AbstractOrtalama { private $array; public function __construct( $rl ) { $this->array = $rl->kayitGetir(); } public function mevcutGetir() { return count($this->array); } public function isimGetir( $get ) { return $this->array[ $get ]->isim; } public function not1Getir( $get ) { return $this->array[ $get ]->not1; } public function not2Getir( $get ) { return $this->array[ $get ]->not2; } } $ogrenci = new OgrenciNotlari; $ogrenci->kayitEkle('emre', 50, 70); $ogrenci->kayitEkle('erdem', 90, 80); $adapter = new Adapter( $ogrenci ); $hesapla = new Ortalama( $adapter ); $hesapla->hesapla(); ?> |
dosya : adapter design pattern
benzer konular:














design patterns