Registry Mirror
搜索
Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
master
Breadcrumbs
PHPDesignPatterns
/
prototype.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
51 lines (45 loc) · 1.32 KB
master
Breadcrumbs
PHPDesignPatterns
/
prototype.php
Copy path
Top
File metadata and controls
Code
Blame
51 lines (45 loc) · 1.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
/**
* Created by PhpStorm.
* User: netAir
* Date: 17-7-20
* Time: 下午5:24
*/
/*
* 原型模式是创建型模式的一种,其特点在于通过“复制”一个已经存在的实例来返回新的实例,而不是新建实例。被复制的实例就是我们所称的“原型”,这个原
* 型是可定制的。原型模式多用于创建复杂的或者耗时的实例,因为这种情况下,复制一个已经存在的实例使程序运行更高效;或者创建值相等,只是命名不一
* 样的同类数据。(来自维基)
*/
class CD
{
public $title;
public $band;
public $trackList = [];
public function __construct(int $id)
{
//假装从数据库拿到了id为$id的数据,哎..太懒
$this->title = 'title' . $id;
$this->band = 'band' . $id;
}
public function buy()
{
var_dump($this);
}
}
class MixtapeCD extends CD
{
public function __clone()
{
$this->title = 'Mixtape';
}
}
$externalPurchaseInfoBandID = 12;
$bandMixProto = new MixtapeCD($externalPurchaseInfoBandID);
$externalPurchaseInfo = [];
$externalPurchaseInfo[] = ['brrr', 'goodbye'];
$externalPurchaseInfo[] = ['what it means', 'brrr'];
foreach ($externalPurchaseInfo as $mixed) {
$cd = clone $bandMixProto;
$cd->trackList = $mixed;
$cd->buy();
}
You can’t perform that action at this time.