Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4847edd

Browse files
committed
Multiton
1 parent 95041d0 commit 4847edd

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

‎Multiton/index.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/**
4+
* Design pattern "Multiton" (Creational)
5+
* This is demo code
6+
* See for details: http://maxsite.org/page/php-patterns
7+
*/
8+
9+
/**
10+
* Base
11+
*/
12+
trait MultitonTrait
13+
{
14+
private static $list = [];
15+
16+
public static function getInstance(string $instance = 'default')
17+
{
18+
if (empty(self::$list[$instance])) self::$list[$instance] = new static();
19+
20+
return self::$list[$instance];
21+
}
22+
23+
private function __construct()
24+
{
25+
}
26+
private function __clone()
27+
{
28+
}
29+
private function __wakeup()
30+
{
31+
}
32+
}
33+
34+
/**
35+
* Class for sample
36+
*/
37+
class Foo
38+
{
39+
/**
40+
* Add MultitonTrait to this class
41+
*/
42+
use MultitonTrait;
43+
44+
/**
45+
* Class's own methods...
46+
*/
47+
private $var = [];
48+
49+
public function addVal($v)
50+
{
51+
$this->var[] = $v;
52+
}
53+
54+
public function getVal()
55+
{
56+
return $this->var;
57+
}
58+
}
59+
60+
61+
/**
62+
* demo use
63+
* see Singleton for other samples
64+
*/
65+
66+
echo '<pre>'; // for print in browser
67+
68+
# $foo = new Foo(); // Error: Call to private Foo::__construct()
69+
70+
/**
71+
* get instance "Alpha"
72+
*/
73+
$foo1 = Foo::getInstance('Alpha');
74+
75+
/**
76+
* add values
77+
*/
78+
$foo1->addVal('first');
79+
$foo1->addVal('second');
80+
81+
/**
82+
* control
83+
*/
84+
print_r($foo1->getVal());
85+
/*
86+
Array
87+
(
88+
[0] => first
89+
[1] => second
90+
)
91+
*/
92+
93+
/**
94+
* new instance "Beta"
95+
*/
96+
$foo2 = Foo::getInstance('Beta');
97+
$foo2->addVal('1000');
98+
$foo2->addVal('2000');
99+
100+
/**
101+
* control
102+
*/
103+
print_r($foo2->getVal());
104+
/*
105+
Array
106+
(
107+
[0] => 1000
108+
[1] => 2000
109+
)
110+
*/
111+
112+
/**
113+
* new instance "default"
114+
*/
115+
$foo3 = Foo::getInstance();
116+
$foo3->addVal('Abcde');
117+
$foo3->addVal('12345');
118+
119+
/**
120+
* control
121+
*/
122+
print_r($foo3->getVal());
123+
/*
124+
Array
125+
(
126+
[0] => Abcde
127+
[1] => 12345
128+
)
129+
*/
130+
131+
# end of file

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ See details on http://maxsite.org/page/php-patterns
77
* "Factory method" / "Virtual Constructor" (Creational)
88
* "Abstract factory" (Creational)
99
* "Singleton" (Creational)
10+
* "Multiton" (Creational)
1011

1112

1213
(c) MaxSite.org, 2019, http://maxsite.org/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /