Php pattern design-Singleton pattern

Source: Internet
Author: User
: This article mainly introduces the single-instance mode of php pattern design. if you are interested in PHP tutorials, refer to it. What is pattern design? At the beginning, beginners will be caught by the tall name. For laruence with rich programming experience, pattern design is everywhere. Many frameworks are designed based on various patterns. Simply put, the process-oriented and simple basic programming is often exposed at the beginning of the code writing process. At this time, we tend to pursue the goal of implementing a function by code. How redundant the code is, whether the code can be reused, and how efficient the code is. However, the code that is truly applied to reality is highly efficient and reusable and easy to develop by the team. Based on these factors, you cannot name a function name or place a script as you do with your hands. Pattern design provides a way of thinking for people to organize code, so that code can be reused to make it easier for others to understand and ensure code reliability.

In all Mode designs, there are three basic design models: Singleton mode, Factory mode, and registration tree mode. other models are often based on these models. Today we bring about Singleton mode.

What is Singleton mode?

Based on this name, we can easily understand that the singleton mode refers to the design mode with only one object instance in the entire application.

Why is the singleton mode used?

Php often deals with databases. if connection objects are frequently established in applications and new operations are performed, the system memory resources will be consumed. this is not what we want to see. In other words, in a team collaboration project, the singleton mode can effectively avoid the new objects of different programmers, resulting in human system consumption.

How to create a singleton mode?

When looking at this problem, I believe that good programmers may try to create a singleton mode as required, rather than waiting for the experience of their predecessors. Different from other bloggers, what kind of mode is Singleton mode? I prefer to think about how to build a singleton mode with basic object-oriented programming experience.

First, we start from the question. The Singleton mode is the design mode with only one object instance. This is a pain point. The classes we normally create cannot create many objects or (abstract classes) objects ). To create an object, a class is required and cannot be an abstract class. This class should prevent others from creating functions multiple times. We naturally consider starting with constructors. However, each new Operation calls the constructor, that is, the object instance is created multiple times. This is contrary to our original design intention. Make sure that the constructor is private or protected to solve this problem.

The constructor is declared as private or protected, which is doomed to fail to create an instance object through the new method. What's more, we found that after this step, the prospects for solving the problem become clear? Why? Since object instances cannot be created using the new method, we can only create object instances using methods in the class. At this time, we are faced with an interesting problem of having chicken or eggs first. We usually call the object method after creating an object. at this time, we need to call the methods in the class to create an object. There is no doubt that a solution that can be called without being affected by the creation of objects is the use of the keyword-static.

What do I do when I create a static method in the class? Regression topic: make sure that only one instance object is created. How can we ensure there is only one? This is very simple. if you want to judge it. If it exists, it is returned directly. if it does not exist, create one by yourself. Of course, this instance object is a static attribute of the class. So far, the singleton mode requires the function implementation. Is it done? Not counting yet ~ If a class inherits this class and declares the constructor as public, isn't it a bad thing? It is necessary to add the final keyword before the constructor.

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

 hash=rand(1,9999);    }    staticpublicfunction getInstance(){        if (self::$ins instanceof self) {            return self::$ins;        }        self::$ins=new self();        return self::$ins;    } }

The Singleton mode is not complex, but requires in-depth understanding. Many beginners still lament that the constructor is not always public ~ You can still create objects without using new in the slot ~ In fact, I want to say that no matter whether the constructor is declared as public, private, or protected, it will be called when the object is created. It is always new to create an object instance, and the singleton mode also uses new to create an object, just change the place, from outside the class to inside the class.

Finally, I would like to greet programmers who have developed various exquisite pattern designs ~~

The above introduces the php pattern design Singleton pattern, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.