-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextFactory.php
42 lines (37 loc) · 878 Bytes
/
TextFactory.php
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
<?php
/**
* Created by PhpStorm.
* User: zcer
* Date: 2020/2/22
* Time: 下午7:15
*/
/**
* A factory manages shared flyweights. Clients should not instantiate them directly,
* but let the factory take care of returning existing objects or creating new ones.
*/
class TextFactory implements Countable
{
/**
* @var TextIF[]
*/
private $charPool = [];
public function get(string $name): TextIF
{
if (!isset($this->charPool[$name])) {
$this->charPool[$name] = $this->create($name);
}
return $this->charPool[$name];
}
private function create(string $name): TextIF
{
if (strlen($name) == 1) {
return new Character($name);
} else {
return new Word($name);
}
}
public function count(): int
{
return count($this->charPool);
}
}