27 / 06 / 2009

facade design pattern – php

ekleyen: Emre Çevik kategori: design patterns| php

Facede (cephe) tasarım şablonu yapısal ( Structural Patterns ) tasarım şablonları grubunda yer alır. Facade kullanmaktaki amacımız yeni birşey yaratmak değil, işleyen sistemi daha basit bir şekilde çözmektir.

Bazı işlemleri yapmak için 4-5 tane nesne türetebiliriz ve bu nesnelerin fonksiyonlarını kullanırız. Bir işlem için yazacağımız kod 10-15 satır bulabilir ve bu işlemide sıklıkla tekrarlıyor olabilirz. Bu işlem icin bir arayüz tasarlayıp orada yaparsak zaman açısından fayda sağlarız.

Eğer Facade kullanıyorsak sistemimiz daha basit ve daha kolay anlaşılır olmalıdır bunu daha önce söylemiştik eğer Facade kullanmadan önceki oluşturduğumuz nesne ve ilişki sayısında bir değişiklik olmuyorsa Facade kullanmamızın bir anlamı yoktur yani Facade tasarımımızda bir yanlışlık vardır.

Facade ile ilgili söyleyebileceğim en önemli kural ise Facade sistemden bağımsız olarak çalışmalıdır. Eğer siz Facade arayüzünü sistemden çıkardığınızda sistemde bozulma oluyor ise bu noktadada bir yanlışlık vardır. Bu durumda Facade sistemin değişmez bir parçası haline gelmiş olur.

Facade kullanmak sistem üzerinde hakimiyetimizi biraz azaltabilir fakat kazandığımız zaman buna değecektir.

Evet şimdi Facade ile ilgili bir örnek yapalım. Örneğimizde 4 adet log saklama yöntemimiz olsun (email, xml, text, sql) ve bu yöntemlerin dördünü birden kullanalım.

facade

1- Loglama yapan alt sınıfları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
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
class EmailLog
{
    public function __construct()
    {
        echo "email bağlantısı oluşturuldu<br>";
    }
 
    public function add( $message )
    {
        echo "email mesaj eklendi : $message <br>";
    }
 
    public function close()
    {
        echo "email bağlantısı kapatıldı<br>";
    }
}
 
class XMLLog
{
    public function __construct( $fileName )
    {
        echo "$fileName log oluşturuldu<br>";
    }
 
    public function add( $message )
    {
        echo "xml loga mesaj eklendi : $message <br>";
    }
 
    public function close()
    {
        echo "xml log kapatıldı<br>";
    }
}
 
class TextLog
{
    public function __construct( $fileName )
    {
        echo "$fileName log oluşturuldu<br>";
    }
 
    public function add( $message )
    {
        echo "text loga mesaj eklendi: $message <br>";
    }
 
    public function close()
    {
        echo "text log kapatıldı<br>";
    }
}
 
class SqlLog
{
    public function __construct()
    {
        echo "sql baglantısı oluşturuldu<br>";
 
    }
 
    public function add( $message )
    {
        echo "sql mesaj eklendi: $message <br>";
    }
 
    public function close()
    {
        echo "sql bağlantısı kapatıldı<br>";
    }
}

2- Alt sınıflara yapmaları gereken kuralları gönderecek olan Facade 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
class Facade
{
    private $xmlLog = null;
    private $textLog = null;
    private $emailLog = null;
    private $sqlLog = null;
 
    public function __construct(){}
 
    public function start( $fileName )
    {
        $this->xmlLog = new XMLLog( $fileName.".xml" );
        $this->textLog = new TextLog( $fileName.".txt" );
        $this->emailLog = new EmailLog( $fileName.".txt" );
        $this->sqlLog = new SqlLog( $fileName.".txt" );
    }
 
    public function add( $message )
    {
        $this->xmlLog->add( $message );
        $this->textLog->add( $message );
        $this->emailLog->add( $message );
        $this->sqlLog->add( $message );
    }
 
    public function end()
    {
        $this->xmlLog->close();
        $this->textLog->close();
        $this->emailLog->close();
        $this->sqlLog->close();
    }
 
    public static function instance()
    {
        static $inst = null;
        if ( !isset( $inst ) ) $inst = new Facade();
        return $inst;
    }
}

evet şimdi bu sınıflar ile nasıl çalışacağımıza göz atalım

1
2
3
4
5
Facade::instance()->start( 'logdosyasi.txt' );
Facade::instance()->add( 'log1 siteye ziyaretci geldi' );
Facade::instance()->add( 'log2 form doldurdu' );
Facade::instance()->add( 'log3 siteden çıktı' );
Facade::instance()->end();

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

logdosyasi.xml log oluşturuldu
logdosyasi.txt log oluşturuldu
email bağlantısı oluşturuldu
sql baglantısı oluşturuldu
xml loga mesaj eklendi : log1 siteye ziyaretci geldi
text loga mesaj eklendi: log1 siteye ziyaretci geldi
email mesaj eklendi : log1 siteye ziyaretci geldi
sql mesaj eklendi: log1 siteye ziyaretci geldi
xml loga mesaj eklendi : log2 form doldurdu
text loga mesaj eklendi: log2 form doldurdu
email mesaj eklendi : log2 form doldurdu
sql mesaj eklendi: log2 form doldurdu
xml loga mesaj eklendi : log3 siteden çıktı
text loga mesaj eklendi: log3 siteden çıktı
email mesaj eklendi : log3 siteden çıktı
sql mesaj eklendi: log3 siteden çıktı
xml log kapatıldı
text log kapatıldı
email bağlantısı kapatıldı
sql bağlantısı kapatıldı

evet gördüğünüz gibi işimizi baya kolaylaştırmış olduk. kolay gelsin.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
class EmailLog
{
    public function __construct()
    {
        echo "email bağlantısı oluşturuldu<br>";
    }
 
    public function add( $message )
    {
        echo "email mesaj eklendi : $message <br>";
    }
 
    public function close()
    {
        echo "email bağlantısı kapatıldı<br>";
    }
}
 
class XMLLog
{
    public function __construct( $fileName )
    {
        echo "$fileName log oluşturuldu<br>";
    }
 
    public function add( $message )
    {
        echo "xml loga mesaj eklendi : $message <br>";
    }
 
    public function close()
    {
        echo "xml log kapatıldı<br>";
    }
}
 
class TextLog
{
    public function __construct( $fileName )
    {
        echo "$fileName log oluşturuldu<br>";
    }
 
    public function add( $message )
    {
        echo "text loga mesaj eklendi: $message <br>";
    }
 
    public function close()
    {
        echo "text log kapatıldı<br>";
    }
}
 
class SqlLog
{
    public function __construct()
    {
        echo "sql baglantısı oluşturuldu<br>";
 
    }
 
    public function add( $message )
    {
        echo "sql mesaj eklendi: $message <br>";
    }
 
    public function close()
    {
        echo "sql bağlantısı kapatıldı<br>";
    }
}
 
class Facade
{
    private $xmlLog = null;
    private $textLog = null;
    private $emailLog = null;
    private $sqlLog = null;
 
    public function __construct(){}
 
    public function start( $fileName )
    {
        $this->xmlLog = new XMLLog( $fileName.".xml" );
        $this->textLog = new TextLog( $fileName.".txt" );
        $this->emailLog = new EmailLog();
        $this->sqlLog = new SqlLog();
    }
 
    public function add( $message )
    {
        $this->xmlLog->add( $message );
        $this->textLog->add( $message );
        $this->emailLog->add( $message );
        $this->sqlLog->add( $message );
    }
 
    public function end()
    {
        $this->xmlLog->close();
        $this->textLog->close();
        $this->emailLog->close();
        $this->sqlLog->close();
    }
 
    public static function instance()
    {
        static $inst = null;
        if ( !isset( $inst ) ) $inst = new Facade();
        return $inst;
    }
}
 
 
 
Facade::instance()->start( 'logdosyasi' );
Facade::instance()->add( 'log1 siteye ziyaretci geldi' );
Facade::instance()->add( 'log2 form doldurdu' );
Facade::instance()->add( 'log3 siteden çıktı' );
Facade::instance()->end();
?>
dosya : faccade design pattern
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • BlinkList
  • blogmarks
  • description
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Technorati

benzer konular:

  1. state design pattern – php
  2. flyweight design pattern – php
  3. observer design pattern – php
  4. decorator design pattern – php
  5. adapter design pattern – php
  6. bridge design pattern – php
  7. proxy design pattern – php
  8. command design pattern – php
  9. template method design pattern – php
  10. iterator design pattern – php


(No Ratings Yet)
Loading ... Loading ...

henüz yorum yazılmadı

yorum yaz

ne yapıyoruz

eğleniyoruz?

anket

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

sonuçlar

Loading ... Loading ...