Design Pattern in PHP: Singleton pattern)

Source: Internet
Author: User
Coderoncode.com20140127design-patterns-php-singletons.html Singleton mode is used to restrict class Instantiation to a single object, which is useful when the entire system only needs one object. The Singleton mode is designed to ensure that there is only one single (so you have to list the instances) class and there is a global access point. Through this Single Instance

Http://coderoncode.com/2014/01/27/design-patterns-php-singletons.html Singleton mode is used to restrict class Instantiation to a single object, which is useful when the entire system only needs one object. The Singleton mode is designed to ensure that there is only one single (so you have to list the instances) class and there is a global access point. Through this Single Instance

Http://coderoncode.com/2014/01/27/design-patterns-php-singletons.html

The Singleton mode is used to restrict class Instantiation to a single object, which is useful when the entire system only needs one object.

The Singleton mode is designed to ensure that there is only one single (so you have to list the instances) class and there is a global access point. Through this single instance, we have a global access point and can delay initialization.

A basic Singleton implementation looks like the following example:

/** Example taken from http://www.webgeekly.com/tutorials/php/how-to-create-a-singleton-class-in-php **/

Class User

{

// Hold an instance of the class

Private Static$ Instance;

// The singleton method

Public Static Function Singleton()

{

If(!Isset (self::$ Instance )){

Self::$ Instance= New_ CLASS __;

}

ReturnSelf::$ Instance;

}

}

$ User1=User::Singleton ();

$ User2=User::Singleton ();

$ User3=User::Singleton ();

?>

In the preceding example, all three variables point to the same object. The first call will produce an object, and subsequent calls will only return this object.

"In computer programming, delayed Initialization is a policy that never creates an object, computes a value, or other expensive processes until it is first required ."

Let's take a look at the obvious and ignored facts and explain why the singleton mode is regarded by many developers as an "anti-pattern ", although such a statement is highly dependent on the framework and language in use, the singleton mode in PHP is generally regarded as an "anti-pattern ".

"The Singleton mode is regarded by many people as an anti-pattern. The anti-pattern is a solution, but this solution is often ineffective and may even lead to a high-risk reaction ."

What is the singleton mode?

If you have read the singleton mode before, you may want to know what I'm talking about?

The Singleton mode is evil. It is an "anti-pattern" and cannot be used. It is not enough to say that the singleton mode is evil. We need to know why we need to avoid using the singleton mode. This is exactly what I want to clarify in this article.

The Singleton mode is regarded as an "anti-pattern" for several reasons. Let's take a look at some of the reasons:

Single responsibility model

The first problem we face when using a single model is that its use breaks the single responsibility principle.

The single-instance object has two responsibilities: use and control the number of instances produced, while the single responsibility principle points out that each class should have only one responsibility, and this responsibility should be fully encapsulated by the class.

Hidden dependency

What are hidden dependencies and how is this related to the singleton mode? If you have read my previous articles on dependency injection, we have seen how to pass dependencies to functions through parameters.

Any parameters received by a function are called visible dependencies. On the other hand, if a function needs something similar to a global variable (read a singleton) to operate, this dependency is considered hidden.

This indicates that if you do not look at the implementation of the actual function, the third party cannot know the hidden dependency.

"A visible dependency is a dependency that developers can see from class interfaces. If a dependency cannot be seen from the class interface, this dependency is a hidden dependency "-jenkov.com

Test

With these issues, we have a problem when we perform unit tests on our applications. Appropriate unit tests should be able to run as independently as databases. Singleton mode makes unit testing difficult even if it is not impossible, because Singleton mode has a global state.

This means that, once instantiated, this instance will only focus on this unit test. This may result in mutual influence between the test and the field.

There is a work und, that is, "Clean up" The Singleton after each test run. However, I found this is still a production taboo and troublesome.

It's not all bad, isn't it?

They cannot be all bad, don't they? Well, let's try to debate the benefits of the Order example model and in some cases they are useful:

1. debugging logs: almost all developers agree that this method should be applicable to every function and every part of the code. The Singleton mode can achieve this goal without compromising readability, testability, and maintainability.

2. file system and database access: the single-instance mode for accessing the file system and database is justified. If you need a global access point for a file system or database, the Singleton mode is helpful in providing flexibility and testability.

Conclusion

The Singleton mode, anti-pattern, and design pattern are generally not good or bad. What makes the singleton mode a "anti-pattern" is not because of the pattern itself, but because the singleton mode is often very easy to implement.

If it is improperly implemented (this is often the case), any mode may become an "anti-pattern ". Even so, it is difficult for modern PHP and modern frameworks to agree with the singleton model. Personally, for the singleton mode, I don't see any advantages of it, but I have seen many disadvantages.

Finally, I understand that Singleton is a controversial and biased topic. If you want to add any points mentioned in this Article or provide useful cases, I would like to see your comments after this article.

For more articles please follow my personal blog: http://www.nomoneynowife.com

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.