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.

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
benzer konular:














design patterns