PHP design patterns: Detailed explanation of singleton pattern and factory pattern

怪我咯
Release: 2023-03-13 17:48:01
Original
1300 people have browsed it

1. Singleton mode

The singleton mode is a commonly used softwaredesign pattern. It contains only one special class called a singleton class in its core structure. The singleton mode can ensure that there is only one instance of a class in the system and that the instance is easy to access from the outside world, thereby facilitating control of the number of instances and saving system resources. If you want only one object of a certain class to exist in the system, the singleton pattern is the best solution.

Advantages

1. Instance control

The singleton mode prevents other objects from instantiating copies of their own singleton objects, thereby ensuring that all objects have access to a unique instance.

2. Flexibility

Because the class controls the instantiation process, the class can flexibly change the instantiation process.

This article mainly introduces the relevant knowledge of single mode and factory mode in PHP. It has a very good reference value. Let’s take a look at it with the editor.

The reasons for using the singleton mode in php

I use Most operations of PHP deal with various databases, including mysql, redis, memcache and other relational and non-relational databases, so there will be a large number of connected databases in an application. If you do not use the singleton mode, you will need a new operation every time, but every new operation will consume a lot of memory resources and system resources, and every time you open and close the database connection, it is a great test and test for the database. waste.

Requires a static member variable that holds the only instance of the class (usually $instance is a private variable)

Constructor and clone function must be declared private, in order to prevent The external program new class loses the meaning of singleton mode

Must provide a public static method to access this instance, thereby returning a reference to the only instance

//设计模式
//单例模式 都是使用同一个对象
//class Ren
//{
// public $name;
// private static $dx; //存储对象
//
// private function construct()
// {
//
// }
//
// public static function DuiXiang()
// {
//  if(empty(self::$dx))
//  {
//   self:;$dx=new Ren();
//  }
//  return self::$dx;
// }
//}
//
//$r= Ren::DuiXiang();
//把构造函数变为私有
//做了一个方法来间接造对象
//在该方法里面加限制
Copy after login

2. Factory pattern

The factory pattern is a pattern that is specifically responsible for instantiating a large number of classes with a common interface (or a common abstract parent class) without knowing in advance which class to instantiate each time. It defines an interface for creating objects, and subclasses decide which class to instantiate.

Advantages
The factory class is the key to the entire pattern. It contains the necessary logical judgment to determine which specific class of objects should be created based on the information given by the outside world. By using the factory class, the outside world can Get rid of the embarrassing situation of directly creating specific product objects, and only need to be responsible for "consuming" objects. It doesn't matter how these objects are created and organized. Clarifying their respective responsibilities and rights is conducive to the optimization of the entire software architecture.
Disadvantages
Because the factory class concentrates the creation logic of all instances, it violates the principle of high cohesion responsibility allocation and concentrates all creation logic into one factory class; the classes it can create can only be considered in advance Yes, if you need to add a new class, you need to change the factory class.
When the number of specific product categories in the system continues to increase, there may be a need for the factory class to create different instances according to different conditions. This judgment of conditions and judgment of specific product types are intertwined, which makes it difficult to avoid the spread of module functions and is very detrimental to system maintenance and expansion;
These shortcomings have been overcome to a certain extent in the factory method pattern.
Usage scenarios
The factory class is responsible for creating fewer objects than ;
Customers only know the parameters passed into the factory class and do not care about how to create objects (logic);
Because Simple factories can easily violate the principle of high cohesion responsibility allocation, so they are generally only used in very simple situations.

Usually, the factory pattern has a key construct, which is a static method named Factory according to the general principle. However, this is just a principle. The factory method can be named arbitrarily. This static method can also accept parameters of any data. It must be Return an object.

class YunSuan
{
 public $a;
 public $b;
 function Suan()
 {
  echo "对两个数进行运算";
 }
class gongchang
{ //功能 : 给一个参数,返回一个对象
 static function chanpin($name)
 {
  switch($name)
  {
   case "'+":
    return new jia();
    break;
   case "-";
    return new jian();
    break;
  }
 }
}
$a=gongchang::chanpin("+");
Copy after login

The above is the detailed content of PHP design patterns: Detailed explanation of singleton pattern and factory pattern. For more information, please follow other related articles on the PHP Chinese website!

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