30 / 06 / 2009

builder design pattern – php

ekleyen: Emre Çevik kategori: design patterns| php

Builder (inşaatçı) tasarım şablonu yapısal (creational patterns) tasarım şablonları grubunda yer alır. Builder kullanmaktaki amaç kompleks yapıdaki bir nesneyi parçalamak birden fazla nesne ile oluşturmaktır.

Bir kahve makinası düşünelim parayı atıyoruz ve karşımıza seçim listesi geliyor seçim listesinde kahve, çay, ıhlamur vb. içecekler var. seçimimizi yapıyoruz makina seçtiğimiz içeceği hazırlıyor ve bize veriyor.

afiyet olsun.

şimdi yukarıdaki orneğimizi builder tasarım şablonu kullanarak tasarlamaya çalışalım.

builder

ana sınıfımı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
class Icecek {
    private $Su = NULL;
    private $Seker = NULL;
    private $Aroma = NULL;
    function __construct() {
    }
 
    function Olustur() {
      return $this->Su.'-'.$this->Seker.'-'.$this->Aroma;
    }
 
    function SuEkle($string){
      $this->Su = $string;
    }
 
    function SekerEkle($string){
      $this->Seker = $string;
    }
 
    function AromaEkle($string){
      $this->Aroma = $string;
    }
 
}

builder sınıfımı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
abstract class AbstractBuilder {
    abstract function Getir();
}
 
class Builder extends AbstractBuilder {
    private $icecek = NULL;
    function __construct() {
      $this->icecek = new Icecek();
    }
 
 
    function SuEkle($string){
      $this->icecek->SuEkle($string);
    }
 
    function SekerEkle($string){
      $this->icecek->SekerEkle($string);
    }
 
    function AromaEkle($string){
      $this->icecek->AromaEkle($string);
    }
 
    function Getir() {
      return $this->icecek->Olustur();
    }
}

director sınıfımı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
abstract class AbstractDirector {
    abstract function __construct(AbstractBuilder $builder);
    abstract function Hazirla();
    abstract function Getir();
}
 
 
class KahveDirector extends AbstractDirector {
    private $builder = NULL;
    public function __construct(AbstractBuilder $builder) {
	     $this->builder = $builder;
    }
 
    function Hazirla()
    {
        $this->builder->AromaEkle('100 gr Kahve');
        $this->builder->SekerEkle('20 gr seker');
        $this->builder->SuEkle('300 gr su');
    }
 
    function Getir()
    {
        return $this->builder->Getir();
    }
}
 
class CayDirector extends AbstractDirector {
    private $builder = NULL;
    public function __construct(AbstractBuilder $builder) {
	     $this->builder = $builder;
    }
 
    function Hazirla()
    {
        $this->builder->AromaEkle('200 gr cay');
        $this->builder->SekerEkle('30 gr seker');
        $this->builder->SuEkle('200 gr su');
    }
 
    function Getir()
    {
        return $this->builder->Getir();
    }
}

kodlarımızla nasıl çalışacağımıza bakalım

1
2
3
4
5
6
7
8
9
  $Builder = new Builder();
  $KahveDirector = new KahveDirector($Builder);
  $KahveDirector->Hazirla();
  echo $KahveDirector->Getir();
 
  $Builder = new Builder();
  $CayDirector = new CayDirector($Builder);
  $CayDirector->Hazirla();
  echo $CayDirector->Getir();

uygulamamızın ekran çıktısı

300 gr su-20 gr seker-100 gr Kahve
200 gr su-30 gr seker-200 gr cay

tüm kodlarını düzgün bir biçimde tekrar yazalı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
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
<?php
 
abstract class AbstractBuilder {
    abstract function Getir();
}
 
abstract class AbstractDirector {
    abstract function __construct(AbstractBuilder $builder);
    abstract function Hazirla();
    abstract function Getir();
}
 
 
 
class Builder extends AbstractBuilder {
    private $icecek = NULL;
    function __construct() {
      $this->icecek = new Icecek();
    }
 
 
    function SuEkle($string){
      $this->icecek->SuEkle($string);
    }
 
    function SekerEkle($string){
      $this->icecek->SekerEkle($string);
    }
 
    function AromaEkle($string){
      $this->icecek->AromaEkle($string);
    }
 
    function Getir() {
      return $this->icecek->Olustur();
    }
}
 
class KahveDirector extends AbstractDirector {
    private $builder = NULL;
    public function __construct(AbstractBuilder $builder) {
	     $this->builder = $builder;
    }
 
    function Hazirla()
    {
        $this->builder->AromaEkle('100 gr Kahve');
        $this->builder->SekerEkle('20 gr seker');
        $this->builder->SuEkle('300 gr su');
    }
 
    function Getir()
    {
        return $this->builder->Getir();
    }
}
 
class CayDirector extends AbstractDirector {
    private $builder = NULL;
    public function __construct(AbstractBuilder $builder) {
	     $this->builder = $builder;
    }
 
    function Hazirla()
    {
        $this->builder->AromaEkle('200 gr cay');
        $this->builder->SekerEkle('30 gr seker');
        $this->builder->SuEkle('200 gr su');
    }
 
    function Getir()
    {
        return $this->builder->Getir();
    }
}
 
 
  $Builder = new Builder();
  $KahveDirector = new KahveDirector($Builder);
  $KahveDirector->Hazirla();
  echo $KahveDirector->Getir();
 
  $Builder = new Builder();
  $CayDirector = new CayDirector($Builder);
  $CayDirector->Hazirla();
  echo $CayDirector->Getir();
?>
dosya : builder design pattern
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • BlinkList
  • blogmarks
  • description
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Technorati

benzer konular:

  1. singleton design pattern – php
  2. visitor design pattern – php
  3. decorator design pattern – php
  4. adapter design pattern – php
  5. proxy design pattern – php
  6. iterator design pattern – php
  7. facade design pattern – php
  8. flyweight design pattern – php
  9. prototype design pattern – php
  10. composite design pattern – php


(2 votes, average: 5.00 out of 5)
Loading ... Loading ...

2 yorum

1 | can

03 / 09 / 2009 - 14:41

Avatar

new icecek tanımlı değil , bilgin olsun

2 | Emre Çevik

03 / 09 / 2009 - 15:56

Avatar

uyarı icin tesekkurler..

icecek sınıfı en ustte mecvuttur onu en tüm kodlara eklemeyi unutmuşuz. kullanacak olanlar ekleyebilirler.

yorum yaz

ne yapıyoruz

eğleniyoruz?

anket

design pattern yazılarını nasıl buldunuz?

sonuçlar

Loading ... Loading ...