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

墨辰丷
Release: 2023-03-26 14:40:01
Original
1427 people have browsed it

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

name = $n; 
   }
  public function getname(){ //取变量$name的值
      return $this->name; 
   }
 }
 
 
 $a = Single::getinstance();
 $b = Single::getinstance();
 $a->setname('hello world');
 $b->setname('good morning');
 echo $a->getname();//good morning
 echo $b->getname();//good morning
?>
Copy after login

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.

a = 20;
$ope->b = 10;
echo $ope->opera();
Copy after login

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
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!