德玛西亚---简单工厂模式---PHP实现
2019-02-27 本文已影响0人
i灬Mango
英雄联盟系列,鸣谢【大话设计模式】大鸟与小菜
首先 看图听故事,
image.png
如采用活字印刷术:
image.png
image.png
================================================================
看图听故事环节结束
好想是get到了什么 ,那么。。。。。。先写一个【菜鸡版本】
coding............................
ok,六多兰剑盖伦出世
class Operation
{
public $intNumberA;
public $intNumberB;
public $operator;
public function validation($intNumberA, $intNumberB)
{
if ((is_int($intNumberA) && is_int($intNumberB)) == false) {
return "所输入值非int";
}
}
public function evaluation($intNumberA, $intNumberB, $operator)
{
$this->validation($intNumberA, $intNumberB);
switch ($operator)
{
case "+":
return $intNumberA + $intNumberB;
case "-":
return $intNumberA - $intNumberB;
case "*":
return $intNumberA * $intNumberB;
case "/":
if ($intNumberB == 0) {
return "0不能当分母!";
} else {
return $intNumberA / $intNumberB;
}
default :
return "暂未支持该运算";
}
}
}
$objectOperation = new Operation();
echo $objectOperation->evaluation(1, 2, '/');
好像对于实现功能来说 没啥问题了。
但是。。。没达到类似于活体印刷术的级别
也就是说 我6多兰的盖伦 被!单!杀!了!
叔可忍,他大娘都不能忍。
祭出大杀器---无尽之刃、饮血剑、阿塔玛之戟、幻影之舞、狂徒铠甲、自然之力
(面向对象思想 and 简单工厂模式)
coding...........................................
class Operation
{
public $intNumberA;
public $intNumberB;
public $operator;
public function validation($intNumberA, $intNumberB)
{
if ((is_int($intNumberA) && is_int($intNumberB)) == false) {
return false;
}
}
}
class OperationAdd extends Operation
{
public function __construct($intNumberA = 0, $intNumberB = 0)
{
$this->validation($intNumberA, $intNumberB);
}
public function getResult($intNumberA, $intNumberB)
{
return $intNumberA + $intNumberB;
}
}
class OperationSub extends Operation
{
public function __construct($intNumberA = 0, $intNumberB = 0)
{
$this->validation($intNumberA, $intNumberB);
}
public function getResult($intNumberA = 0, $intNumberB = 0)
{
return $intNumberA - $intNumberB;
}
}
class OperationMul extends Operation
{
public function __construct($intNumberA = 0, $intNumberB = 0)
{
$this->validation($intNumberA, $intNumberB);
}
public function getResult($intNumberA = 0, $intNumberB = 0)
{
return $intNumberA * $intNumberB;
}
}
class OperationDiv extends Operation
{
public function __construct($intNumberA = 0, $intNumberB = 0)
{
$this->validation($intNumberA, $intNumberB);
}
public function getResult($intNumberA = 0, $intNumberB = 0)
{
if ($intNumberB == 0) {
return "0不能当分母!";
} else {
return $intNumberA / $intNumberB;
}
}
}
class OperationFactory
{
public function createOperation($operator)
{
switch ($operator)
{
case "+":
$obj = new OperationAdd;
return $obj;
case "-":
$obj = new OperationAdd;
return $obj;
case "*":
$obj = new OperationAdd;
return $obj;
case "/":
$obj = new OperationAdd;
return $obj;
}
}
}
$operationFactory = new OperationFactory;
echo $operationFactory->createOperation('+')->getResult(1, 2);
all right 现在我们的德玛西亚之力 可以去1V9了
作者:mango.Young
转载请注明出处 谢谢。