Single-Case mode

Source: Internet
Author: User

  Note: This document reproduced, not their own writing, feel very clear, so reproduced

What is a pattern design? A beginner will be fooled by the name of the tall one at first. For the veteran with rich programming experience, pattern design is ubiquitous. Many of the frames of engagement are based on a variety of pattern designs. Simply put, in the process of writing code in the beginning often contact is process-oriented, simple BASIC programming. This time we tend to pursue the code to achieve a certain function is all right. How redundant the code is, whether the code is reusable, how efficient it is, and how well it can be achieved. But what really applies to real-world adoption is the code that is efficient, reusable, and easy for the team to develop. Based on these factors, you can't simply name the function as practiced hand and place the script casually. Pattern design tells you to provide a way for people to organize code, implement reusable code, make it easier for others to understand, and ensure code reliability.

In all pattern designs, there are three basic design patterns, Singleton mode, Factory mode, registered tree mode, and other patterns are often based on these patterns, and today brings a singleton pattern.

  What is a singleton mode ?

According to this name, it is easy to see that the Singleton pattern refers to the design pattern of only one object instance throughout the application.

  Why use singleton mode?

  PHP often deals with databases, which is not what we want to see if the connection objects are frequently established in the application and new operations are made, which consumes aniseed system memory resources. Moreover, in a team-work project, the singleton model can effectively avoid the different programmers new their own objects, resulting in artificial system consumption.

How to set up a singleton mode?

When you see this problem, believe that good programmers are likely to try to create singleton patterns on their own, rather than waiting for the experience of their predecessors. Different from other Bo friends to tell you what kind of pattern is a singleton mode, I am more willing and have the basic experience of object-oriented programming you consider how to set up a singleton mode.

We start with the topic, the singleton pattern is the design pattern of only one object instance . This is a very painful thing to do. The classes we normally create are not objects that can be created, or objects that cannot be created (abstract classes). It is necessary to have a class to create an object, and it cannot be an abstract class. This class prevents others from creating functions more than once. We naturally consider the beginning of the constructor function. However, each new operation invokes the constructor, which means that the object instance is created more than once. This is contrary to our original design. It is important to state that the constructor is private or protected in order to resolve the problem.

The constructor is declared private or protected, which is doomed to create an instance object by using new method. And we found that after this step, the prospect of solving the problem became clear. Why is it? Now that you can't create an object instance from the new method, we can only create an instance of the object through a method within the class. At this point we face an interesting problem of having chickens or eggs first. We tend to be the method of invoking the object after the object is created, and we need to invoke the method inside the class to create the object. The solution to a method that is not affected by the creation of an object is undoubtedly the use of the keyword--static.

What does it do to create a static method within a class? Regression topic: Make sure that only one instance object is created. How do you make sure there's only one? This is very simple, if you judge Ah. If there is a direct return, there is no one to create. Of course, this instance object is a static property of the class. At this point, the single-instance mode requires the completion of the functional implementation. Is it really done yet? If there is a class that inherits this class, declare the construction method public that's not a bad thing? It is necessary to add the final keyword before constructing the method.

Finally paste the single code, code interpretation is on the top ~ ~

<?phpclass single{public    $hash;    static protected $ins =null;    Final protected function __construct () {        $this->hash=rand (1,9999);    }    static public Function getinstance () {        if (self:: $ins instanceof Self) {            return self:: $ins;        }        Self:: $ins =new self ();        Return self:: $ins;    } }

  

The singleton pattern itself is not complex, but requires deep understanding. Many beginners will still sigh: crouching groove, the construction method is not always public AH ~ Crouching trough can also not create objects through new AH ~ Actually I want to say, regardless of the construction method is declared as public,private or protected, the final creation of the object will be called. Always is new creates an object instance, and the singleton mode creates the object with new, just in a different place, from outside the class to the class.

Finally to the study of a variety of subtle pattern design programmer said admire ~ ~

Single-Case mode

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.