PHP single sample mode and factory mode

Source: Internet
Author: User
The Singleton mode refers to the design mode in which the entire application class has only one object instance. Concept of Singleton mode

The Singleton mode refers to the design mode in which the entire application class has only one object instance.

Features of Singleton mode


A class has only one instance in the entire application.

Class must create this instance by itself

You must provide this instance to the entire system.

Reasons for using Singleton mode in php

Most of my php operations are related to various databases, including mysql, redis, memcache, and other relational and non-relational databases. Therefore, a large number of database connection operations exist in an application, if the Singleton mode is not used, the new operation is required each time, but the new operation consumes a lot of memory resources and system resources, in addition, each time you open or close a database connection, it is a great test and waste for the database.


A static member variable (usually $ instance is a private variable) that stores a unique instance of the class)

Constructor and clone functions must be declared as private. to prevent external programs from creating new classes and thus losing the meaning of Singleton mode

You must provide a public static method to access this instance to return a reference of a unique instance.

// Design mode // Singleton mode uses the same object // class Ren // {// public $ name; // private static $ dx; // storage object // private function _ construct () // {//} // public static function DuiXiang () // {// if (empty (self: $ dx) // {// self:; $ dx = new Ren (); //} // return self:: $ dx; //} // $ r = Ren: DuiXiang (); // Convert the constructor to private. // A method is used to indirectly create objects. // add restrictions to this method.

Factory

It refers to a class that contains a method specifically used to create other objects. the factory class is crucial in the practice of polymorphism programming. it allows dynamic replacement of classes, modification of configurations, it usually makes applications more flexible, and it is important to be familiar with advanced PHP developers in the factory mode.

The factory mode is usually used to return different classes that conform to similar interfaces. a common usage of the factory is to create a multi-state provider, this allows us to decide which class should be instantiated based on the application logic or configuration settings. for example, we can use such a provider to extend a class without restructuring other parts of the application, to use the new extension name.

Generally, the Factory mode has a key structure and is named as the static method of Factory according to the general principle. However, this is only a principle, and the Factory method can be named arbitrarily, this static method can also accept any data parameter and must return an object.

Class YunSuan {public $ a; public $ B; function Suan () {echo "calculate two numbers ";}
Class gongchang {// function: returns an object static function chanpin ($ name) {switch ($ name) {case "'+": return new jia (); break; case "-"; return new jian (); break ;}}$ a = gongchang: chanpin ("+ ");

For more articles about the single-instance mode and factory mode in PHP, refer to the Chinese PHP website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.