亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP設(shè)計模式之工廠方法設(shè)計模式實(shí)例分析

 更新時間:2018年04月25日 10:39:02   作者:編程人,在天涯  
這篇文章主要介紹了PHP設(shè)計模式之工廠方法設(shè)計模式,結(jié)合實(shí)例形式分析了工廠方法設(shè)計模式的概念、原理以及php一般工廠方法模式與參數(shù)化工廠方法模式具體實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP設(shè)計模式之工廠方法設(shè)計模式。分享給大家供大家參考,具體如下:

一、什么是工廠方法模式

作為一種創(chuàng)建型設(shè)計模式,工廠方法模式就是要創(chuàng)建“某種東西”。對于工廠方法,要創(chuàng)建的“東西”是一個產(chǎn)品,這個產(chǎn)品與創(chuàng)建它的類之間不存在綁定。實(shí)際上,為了保持這種松耦合,客戶會通過一個工廠發(fā)出請求,再由工廠創(chuàng)建所請求的產(chǎn)品。利用工廠方法模式,請求者只發(fā)出請求,而不具體創(chuàng)建產(chǎn)品。

二、什么時候使用工廠方法模式

如果實(shí)例化對象的子類可能改變,就要使用工廠方法模式。

三、一般工廠方法模式

使用一般工廠方法模式時,客戶只包含工廠的引用,一個工廠生產(chǎn)一種產(chǎn)品。增加一種產(chǎn)品的同時需要增加一個新工廠類和一個新產(chǎn)品類。

<?php
/**
*  一般工廠方法設(shè)計模式
**/
//工廠抽象類
abstract class Factory
{
  protected abstract function produce();
  public function startFactory()
  {
    $pro = $this->produce();
    return $pro;
  }
}
//文本工廠
class TextFactory extends Factory
{
  protected function produce()
  {
    $textProduct = new TextProduct();
    return $textProduct->getProperties();
  }
}
//圖像工廠
class ImageFactory extends Factory
{
  protected function produce()
  {
    $imageProduct = new ImageProduct();
    return $imageProduct->getProperties();
  }
}
//產(chǎn)品類接口
interface Product
{
  public function getProperties();
}
//文本產(chǎn)品
class TextProduct implements Product
{
  private $text;
  function getProperties()
  {
    $this->text = "此處為文本";
    return $this->text;
  }
}
//圖像產(chǎn)品
class ImageProduct implements Product
{
  private $image;
  function getProperties()
  {
    $this->image = "此處為圖像";
    return $this->image;
  }
}
//客戶類
class Client
{
  private $textFactory;
  private $imageFactory;
  public function __construct()
  {
    $this->textFactory = new TextFactory();
    echo $this->textFactory->startFactory() . '<br />';
    $this->imageFactory = new ImageFactory();
    echo $this->imageFactory->startFactory() . '<br />';
  }
}
$client = new Client();
/*運(yùn)行結(jié)果:
此處為文本
此處為圖像
*/
?>

四、參數(shù)化工廠方法模式

使用參數(shù)化工廠方法模式時,客戶包含工廠和產(chǎn)品的引用,發(fā)出請求時需要指定產(chǎn)品的種類,一個工廠生產(chǎn)多種產(chǎn)品。增加一種產(chǎn)品時只需要增加一個新產(chǎn)品類即可。

<?php
/**
*  參數(shù)化工廠方法設(shè)計模式
**/
//工廠抽象類
abstract class Factory
{
  protected abstract function produce(Product $product);
  public function startFactory(Product $product)
  {
    $pro = $this->produce($product);
    return $pro;
  }
}
//工廠實(shí)現(xiàn)
class ConcreteFactory extends Factory
{
  protected function produce(Product $product)
  {
    return $product->getProperties();
  }
}
//產(chǎn)品類接口
interface Product
{
  public function getProperties();
}
//文本產(chǎn)品
class TextProduct implements Product
{
  private $text;
  public function getProperties()
  {
    $this->text = "此處為文本";
    return $this->text;
  }
}
//圖像產(chǎn)品
class ImageProduct implements Product
{
  private $image;
  public function getProperties()
  {
    $this->image = "此處為圖像";
    return $this->image;
  }
}
//客戶類
class Client
{
  private $factory;
  private $textProduct;
  private $imageProduct;
  public function __construct()
  {
    $factory = new ConcreteFactory();
    $textProduct = new TextProduct();
    $imageProduct = new ImageProduct();
    echo $factory->startFactory($textProduct) . '<br />';
    echo $factory->startFactory($imageProduct) . '<br />';
  }
}
$client = new Client();
/*運(yùn)行結(jié)果:
此處為文本
此處為圖像
*/
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論