-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.php
79 lines (64 loc) · 1.59 KB
/
template.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
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
<?php
/**
* Created by PhpStorm.
* User: netAir
* Date: 17-7-22
* Time: 上午10:40
*/
//模板模式,由最终方法定义大致过程的框架,再由各个子类完成其子方法的具体内容.
abstract class SaleItemTemplate
{
public $price = 0;
final public function setPriceAdjustments()
{
$this->price += $this->taxAddition();
$this->price += $this->oversizedAddition();
}
protected function oversizedAddition()
{
return 0;
}
abstract protected function taxAddition();
}
class CD extends SaleItemTemplate
{
public $band;
public $title;
public function __construct(string $band, string $title, float $price)
{
$this->band = $band;
$this->title = $title;
$this->price = $price;
}
protected function taxAddition()
{
return round($this->price * .05, 2);
}
}
class BandEndorsedCaseOfCereal extends SaleItemTemplate
{
public $band;
public function __construct(string $band, string $price)
{
$this->band = $band;
$this->price = $price;
}
protected function taxAddition()
{
return 0;
}
protected function oversizedAddition()
{
return round($this->price * .20, 2);
}
}
$externalTitle = 'title';
$externalBand = 'band';
$externalCDPrice = 12.99;
$externalCerealPrice = 90;
$cd = new CD($externalBand, $externalTitle, $externalCDPrice);
$cd->setPriceAdjustments();
var_dump($cd->price);
$cereal = new BandEndorsedCaseOfCereal($externalBand, $externalCerealPrice);
$cereal->setPriceAdjustments();
var_dump($cereal->price);