Home  >  Article  >  Backend Development  >  PHP pattern design singleton pattern

PHP pattern design singleton pattern

WBOY
WBOYOriginal
2016-07-30 13:30:291157browse

 What is pattern design? Beginners will be intimidated by this lofty name at first. For veterans with rich programming experience, pattern design is everywhere. Many contact frameworks are designed based on various patterns. To put it simply, in the process of writing code, what you often come into contact with at the beginning is process-oriented, simple and basic programming. At this time, what we often pursue is that everything will be fine if the code can achieve a certain function. It doesn’t matter how redundant the code is, whether it is reusable, or how efficient it is, as long as it can achieve the function. However, what is really applied in practice and is more adopted by everyone is the code that is efficient, reusable, and easy for team development. Based on these factors, you cannot just name functions and place scripts casually like you are practicing. Pattern design advice is to provide people with an idea for organizing code, to achieve reusable code, to make the code easier to understand by others, and to ensure code reliability.

 In all pattern designs, there are three basic design patterns, singleton pattern, factory pattern, and registration tree pattern. Other patterns are often based on these patterns. Today we bring the singleton pattern.

What is singleton pattern?

Based on this name, we can easily understand that the singleton pattern refers to a design pattern in which there is only one object instance in the entire application.

Why use singleton mode?

php often deals with databases. If you frequently establish connection objects and perform new operations in your application, a large amount of system memory resources will be consumed, which is not what we want to see. Furthermore, in team cooperation projects, the singleton mode can effectively prevent different programmers from newing their own objects, causing artificial system consumption.

 How to create a singleton pattern?

When I see this problem, I believe that excellent programmers are likely to try to create a singleton pattern according to the requirements instead of waiting for the experience of their predecessors. Unlike other bloggers who tell you what kind of pattern is a singleton pattern, I prefer to think about how to build a singleton pattern yourself with you who have basic experience in object-oriented programming.

Let’s start from the title first. The singleton pattern is a design pattern with only one object instance. This is very painful. The classes we usually create can either create many objects or cannot create objects (abstract classes). To create an object, a class is required, and it cannot be an abstract class. This class is to prevent others from creating functions multiple times. We naturally considered starting with the constructor. However, each new operation will call the constructor, which means that the object instance will be created multiple times. This is contrary to our original design intention. Be sure to declare the constructor as private or protected here to solve this problem.

  If the constructor is declared as private or protected, it is destined to be unable to create an instance object through the new method. And we found that after this step of processing, the prospects for solving the problem became clear? why? Since object instances cannot be created through the new method, we can only create object instances through methods within the class. At this time we are faced with an interesting chicken or egg problem. We often call the object's method after creating the object. At this time, we need to call the method in the class to create the object. The solution to a method that can be called regardless of whether the object is created is undoubtedly to use the keyword --static.

 What does creating a static method within a class accomplish? Back on topic: Make sure you only create one instance object. How to ensure that there is only one? This is very simple, just judge if. If it exists, return it directly. If it doesn't exist, create one yourself. Of course, this instance object is a static property of the class. At this point, the functions required by the singleton mode are implemented. Is it really completed? Not yet~ If a class inherits this class, wouldn't it be a bad thing to declare the constructor as public? Then it is necessary to add the final keyword before the constructor method.

 Finally, the singleton mode code is pasted, and the code explanations are all above~~

<?<span>php
</span><span>class</span><span> Single{
    </span><span>public</span><span>$hash</span><span>;
    </span><span>static</span><span>protected</span><span>$ins</span>=<span>null</span><span>;
    </span><span>final</span><span>protected</span><span>function</span><span> __construct(){
        </span><span>$this</span>->hash=<span>rand</span>(1,9999<span>);
    }

    </span><span>static</span><span>public</span><span>function</span><span> getInstance(){
        </span><span>if</span> (self::<span>$ins</span><span> instanceof self) {
            </span><span>return</span> self::<span>$ins</span><span>;
        }
        self</span>::<span>$ins</span>=<span>new</span><span> self();
        </span><span>return</span> self::<span>$ins</span><span>;
    } 
}</span>

 The singleton mode itself is not complicated, but it requires in-depth understanding. Many beginners still sigh: Damn it, the constructor is not always public~ Damn it, you can create objects without using new~ In fact, the author wants to say that no matter whether the constructor is declared as public, private or protected, the object is finally created will be called whenever. New is always used to create object instances. Singleton mode also uses new to create objects, but it just changes the place, from outside the class to inside the class.

Finally, I would like to express my admiration to the programmers who have developed various exquisite pattern designs~~

The above introduces the singleton mode of PHP mode design, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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