Home  >  Article  >  Backend Development  >  Detailed explanation and examples of singleton mode and factory mode in PHP

Detailed explanation and examples of singleton mode and factory mode in PHP

墨辰丷
墨辰丷Original
2018-05-18 11:26:371571browse

This article mainly introduces the relevant information of PHP singleton mode and factory mode in detail, which has certain reference value. Interested friends can refer to it

1. Singleton mode and Called the responsibility pattern, it is used to create a single functional access point in the program. In layman's terms, the instantiated object is unique.
All singleton patterns have at least the following three common elements:

1. They must have a constructor and must be marked private
2. They have a saving class Static member variables of the instance
3. They have a public static method to access this instance

The singleton class cannot be directly instantiated in other classes, but can only be instantiated by itself. . It does not create a copy of the instance, but returns a reference to the instance stored internally in the singleton class.

Singleton pattern instance

<?php
class Single {
  private $name;//声明一个私有的实例变量
  private function __construct(){//声明私有构造方法为了防止外部代码使用new来创建对象。
  
  }
 
  static public $instance;//声明一个静态变量(保存在类中唯一的一个实例)
  static public function getinstance(){//声明一个getinstance()静态方法,用于检测是否有实例对象
   if(!self::$instance) self::$instance = new self();
   return self::$instance;
  }
 
  public function setname($n){ //给变量$name赋值
      $this->name = $n; 
   }
  public function getname(){ //取变量$name的值
      return $this->name; 
   }
 }
 
 
 $a = Single::getinstance();
 $b = Single::getinstance();
 $a->setname(&#39;hello world&#39;);
 $b->setname(&#39;good morning&#39;);
 echo $a->getname();//good morning
 echo $b->getname();//good morning
?>

2. The factory pattern is a class that has certain methods for creating objects for you. This allows you to use factory classes to create objects instead of using new directly.

So if you want to change the type of object created, you only need to change the factory.

<?php
//工厂类(关于运算符的操作)
class factory{
 public static function dx($ope){
  switch($ope){
   case "+":
    return new plus();
    break;
   case "-":
    return new sub();
    break;
   case "%":
    return new rem();
    break;
  }
 }
} 
$ope = factory::dx("+");
$ope->a = 20;
$ope->b = 10;
echo $ope->opera();

Factory Pattern Example

class Factory {//创建一个基本的工厂类
  static public function fac($id){//创建一个返回对象实例的静态方法
   if(1 == $id) return new A();
   elseif(2==$id) return new B();
   elseif(3==$id) return new C();
   return new D();
  }
 }
 
 interface FetchName {//创建一个接口
  public function getname();//
 }
 
 class A implements FetchName{
  private $name = "AAAAA";
  public function getname(){ 
      return $this->name; 
    }
 }
 
 class C implements FetchName{
  private $name = "CCCCC";
  public function getname(){
      return $this->name; 
    }
 }
 class B implements FetchName{
  private $name = "BBBBB";
  public function getname(){ 
      return $this->name;
    }
 }
 
 class D implements FetchName{
  private $name = "DDDDD";
  public function getname(){ 
      return $this->name; 
    }
}
 
 
 $o = Factory::fac(6);//调用工厂类中的方法
 if($o instanceof FetchName){
 echo $o->getname();//DDDDD
 }
 
 $p=Factory::fac(3);
 echo $p->getname();//CCCCC
?>

Related recommendations:

Singleton pattern of php pattern design

Adapter pattern of php pattern design

A brief analysis of php singleton mode, a brief analysis of php mode_PHP tutorial

The above is the detailed content of Detailed explanation and examples of singleton mode and factory mode in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn