【设计模式】工厂设计模式

小破孩
2022-08-02 / 0 评论 / 224 阅读 / 正在检测是否收录...

要是当操作类的参数变化时,只用改相应的工厂类就可以

工厂设计模式常用于根据输入参数的不同或者应用程序配置的不同来创建一种专门用来实例化并返回其对应的类的实例。

使用场景 :使用方法 new实例化类,每次实例化只需调用工厂类中的方法实例化即可。

优点 :由于一个类可能会在很多地方被实例化。当类名或参数发生变化时,工厂模式可简单快捷的在工厂类下的方法中 一次性修改,避免了一个个的去修改实例化的对象。

我们举例子,假设矩形、圆都有同样的一个方法,那么我们用基类提供的API来创建实例时,通过传参数来自动创建对应的类的实例,他们都有获取周长和面积的功能。

例子

<?php  

interface InterfaceShape   
{  
 function getArea();  
 function getCircumference();  
}  

/** 
* 矩形 
*/  
class Rectangle implements InterfaceShape  
{  
  private $width;  
  private $height;  

  public function __construct($width, $height)  
  {  
    $this->width = $width;  
    $this->height = $height;  
  }  

  public function getArea()   
  {  
    return $this->width* $this->height;  
  }  

  public function getCircumference()  
  {  
    return 2 * $this->width + 2 * $this->height;  
  }  
}  

/** 
* 圆形 
*/  
class Circle implements InterfaceShape  
{  
  private $radius;  

  function __construct($radius)  
  {  
    $this->radius = $radius;  
  }  


  public function getArea()   
  {  
    return M_PI * pow($this->radius, 2);  
  }  

  public function getCircumference()  
  {  
    return 2 * M_PI * $this->radius;  
  }  
}  

/** 
* 形状工厂类 
*/  
class FactoryShape   
{   
  public static function create()  
  {  
    switch (func_num_args()) {  
      case1:  
      return newCircle(func_get_arg(0));  
      case2:  
      return newRectangle(func_get_arg(0), func_get_arg(1));  
      default:  
        # code...  
        break;  
    }  
  }   
}  

$rect =FactoryShape::create(5, 5);  
// object(Rectangle)#1 (2) { ["width":"Rectangle":private]=> int(5) ["height":"Rectangle":private]=> int(5) }  
var_dump($rect);  
echo "<br>";  

// object(Circle)#2 (1) { ["radius":"Circle":private]=> int(4) }  
$circle =FactoryShape::create(4);  
var_dump($circle);  
0

评论 (0)

取消