-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFactory.php
More file actions
64 lines (54 loc) · 1.11 KB
/
SimpleFactory.php
File metadata and controls
64 lines (54 loc) · 1.11 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
<?php
//简单工厂模式
//简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例
abstract class Operation
{
private $numA, $numB;
public function __set($name, $value)
{
$this->$name = $value;
}
public function __get($name)
{
return $this->$name;
}
abstract public function getResult();
}
class OperateAdd extends Operation
{
public function getResult()
{
$res = $this->numA + $this->numB;
return $res;
}
}
class OperateSub extends Operation
{
public function getResult()
{
return $this->numA - $this->numB;
}
}
class SimpleFactory
{
public static function createOperate( $op)
{
switch($op)
{
case '+':
$operate = new OperateAdd();
break;
case '-':
$operate = new OperateSub();
break;
default:
break;
}
return $operate;
}
}
$oper = SimpleFactory::createOperate('+');
$oper->numA = 5;
$oper->numB = 10;
echo $oper->getResult();
?>