- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- PHP/OOP/Flyweightパターン へ行く。
- 1 (2012-01-18 (水) 18:25:49)
class Foo {
private $val = 0;
private $pool = array();
public function getVal($key) {
if (!isset($this->pool[$key])) {
$this->pool[$key] = $this->create();
}
return $this->pool[$key];
}
private function create() {
return ++$this->val;
}
public function clear($key) {
unset($this->pool[$key]);
}
}
$f = new Foo;
print $f->getVal(1) . "\n"; # 1
print $f->getVal(1) . "\n"; # 1
print $f->getVal(2) . "\n"; # 2
print $f->getVal(1) . "\n"; # 1
$f->clear(1);
print $f->getVal(1) . "\n"; # 3