02 / 07 / 2009

flyweight design pattern – php

ekleyen: Emre Çevik kategori: design patterns| php

Flyweight (sinek siklet) tasarım şablonu yapısal (structural patterns) tasarım şablonları grubunda yer alır. Flyweight kullanım amacı kullanılan nesne sayısının aşşağıya çekilmesidir. 1 tane nesne oluşturup farklı verileri bu nesneye method parametresi olarak göndeririz.


flyweight

1
2
3
4
5
6
7
8
9
10
11
12
class TextBox{
    private $name;
 
    public function __construct($name, $value=''){
        $this->name=$name;
        $this->value=$value;
    }
 
    public function createBox(){
        return '<input type="text" name="'.$this->name.'" value="'.$this->value.'"/>';
    }
}

textbox sınıfımızda textbox olusturmak için aşağıdaki şekilde kullanırız.

1
2
$nameBox=new TextBox('name');
$emailBox=new TextBox('email');

flyweight ve form sınıfımızı 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
 
class FlyWeight{
    private $array=array();
 
    public function addBox($name){
 
        if($this->array[$name]==NULL)
        {
            $this->array[$name]=new TextBox($name);
        }
 
        return $this->array[$name];
    }
}
 
class Form{
 
    private $array=array();
    public function addBox(TextBox $name){
        $this->array[]=$name;
    }
 
    public function display(){
        $html='<form method="post" action="post.php">';
        foreach($this->array as $textBox){
            $html.=$textBox->createBox().'<br />';
        }
 
        $html.='<input type="submit" value="gonder" /></form>';
        return $html;
    }
}

görüldüğü üzere artık nesnemizi sürekli oluşturmamıza gerek yoktur.

1
2
3
4
5
6
7
8
9
10
$textBox=new FlyWeight();
$name=$textBox->addBox('name');
$email=$textBox->addBox('email');
$box=$textBox->addBox('box');
 
$Form=new Form();
$Form->addBox($name);
$Form->addBox($email);
$Form->addBox($box);
echo $Form->display();

kodun ekran çıktısı

<form method=”post” action=”post.php”>
<input type=”text” name=”name” value=”"/>
<input type=”text” name=”email” value=”"/>
<input type=”text” name=”box” value=”"/>
<input type=”submit” value=”gonder” /></form>

tüm kodları düzenli şekilde 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
<?php
 
class TextBox{
    private $name;
 
    public function __construct($name, $value=''){
        $this->name=$name;
        $this->value=$value;
    }
 
    public function createBox(){
        return '<input type="text" name="'.$this->name.'" value="'.$this->value.'"/>';
    }
}
 
/*
$nameBox=new TextBox('name');
$emailBox=new TextBox('email');
*/
 
 
class FlyWeight{
    private $array=array();
 
    public function addBox($name){
 
        if($this->array[$name]==NULL)
        {
            $this->array[$name]=new TextBox($name);
        }
 
        return $this->array[$name];
    }
}
 
class Form{
 
    private $array=array();
    public function addBox(TextBox $name){
        $this->array[]=$name;
    }
 
    public function display(){
        $html='<form method="post" action="post.php">';
        foreach($this->array as $textBox){
            $html.=$textBox->createBox().'<br />';
        }
 
        $html.='<input type="submit" value="gonder" /></form>';
        return $html;
    }
}
 
 
$textBox=new FlyWeight();
$name=$textBox->addBox('name');
$email=$textBox->addBox('email');
$box=$textBox->addBox('box');
 
$Form=new Form();
$Form->addBox($name);
$Form->addBox($email);
$Form->addBox($box);
echo $Form->display();
?>
dosya : flyweight design pattern
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • BlinkList
  • blogmarks
  • description
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Technorati

benzer konular:

  1. facade design pattern – php
  2. adapter design pattern – php
  3. command design pattern – php
  4. composite design pattern – php
  5. iterator design pattern – php
  6. decorator design pattern – php
  7. bridge design pattern – php
  8. factory method design pattern – php
  9. builder design pattern – php
  10. observer design pattern – php


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

2 yorum

1 | can

01 / 07 / 2010 - 12:01

Avatar

[code]
class Form
{
private $args;
private $pattern = "\n%s\n";
private $var = Array();

public function __construct($name, $action, $method = 'POST', $type = null)
{
$this->args['id'] = $name;
$this->args['name'] = $name;
$this->args['action'] = $action;
$this->args['method'] = $method;
$this->args['type'] = $type;

$this->args['element'] = "";

}

public function __toString()
{

return (string )$this->generate();
}

public function generate()
{
foreach ((Array)$this->var as $key => $value)
{

$this->args['element'] .= "\t" . $value . "\n";

}
return vsprintf($this->pattern, $this->args);

}

public function __set($key, $value)
{

$this->var[$key] = $value;

}

public function __get($key)
{

return $this->var[$key];
}

}

$Form1 = new Form('Form1', "", 'POST');
$Form1->durum1 = ('');
$Form1->radio1 = '';
$Form1->durum2 = '';

echo $Form1;[/code]

şeklinde olursa arasında ki fark ne olur?

2 | Emre Çevik

02 / 07 / 2010 - 00:27

Avatar

bunları tek başına düşünmeyin bir sistem düşünün sistemde bir sürü parça var, buda o sistemin bir parçası.

hadi ben flyweight kullanıyım diye kullanmayın zaten.

Daha ayrıntılı bilgi için MVP Bursak Selim Şenyurt tarafından yazılmış bir yazıyı okuyabilirsin.

http://www.buraksenyurt.com/post/Tasarc4b1m-Desenleri-FlyWeight.aspx

yorum yaz

ne yapıyoruz

eğleniyoruz?

anket

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

sonuçlar

Loading ... Loading ...