-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.php
More file actions
87 lines (75 loc) · 1.82 KB
/
Demo.php
File metadata and controls
87 lines (75 loc) · 1.82 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
declare(strict_types=1);
interface InsuranceInterface
{
// 计算价格
public function cal(): float;
// 获取险种名字
public function name(): string;
// 获取详情
public function info(): array;
}
class ChinaLifeInsurance implements InsuranceInterface
{
public function cal(): float
{
// TODO: Implement cal() method.
return 2;
}
public function info(): array
{
// TODO: Implement info() method.
return [
'money' => 10.5,
'year' => 10,
];
}
public function name(): string
{
// TODO: Implement name() method.
return 'ChinaLifeInsurance';
}
}
class PingAnInsurance implements InsuranceInterface
{
public function cal(): float
{
// TODO: Implement cal() method.
return 1;
}
public function info(): array
{
// TODO: Implement info() method.
return [
'money' => 10,
'year' => 10,
];
}
public function name(): string
{
// TODO: Implement name() method.
return 'PingAnInsurance';
}
}
class StrategyFactory
{
/**
* @param $strategyName
* @return InsuranceInterface
*/
public static function getInstance($strategyName): InsuranceInterface
{
$className = __NAMESPACE__ . '\\' . $strategyName . 'Insurance';
if (class_exists($className)) {
return new $className();
}
throw new \RuntimeException('不存在该保险');
}
}
$insuranceName = 'ChinaLife';
$insuranceFactory = StrategyFactory::getInstance($insuranceName);
echo 'insuranceName: ' . $insuranceFactory->name();
echo '<br>';
echo 'insuranceInfo: ' . json_encode($insuranceFactory->info(), 320);
echo '<br>';
echo 'insuranceCal: ' . $insuranceFactory->cal();