Why the Singleton mode is evil (translated) _php tutorial

Source: Internet
Author: User
Original link: http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx

Although this article is not written by me, I totally agree with the point in the article. Brian button is probably one of the most experienced people I know. I'm sure he'll like your feedback.

1. Singleton mode is often used to provide a global access point for some services
Yes, it can be done, but what is the price? As we all know, Singleton mode provides global access points for some services in your application, so you don't have to pass a reference to the service everywhere. What is the difference between this and a global variable? (Remember, global variables are bad, aren't they?) What will eventually happen is that the dependencies in your design are hidden in the code, and this dependency cannot be seen by examining the interfaces of your classes and methods. You have to check the code to know exactly what other objects are used in your class. This may not be clear (translator Note: Do not know if it is not so translated, the original: This is less clear than it could). Impulse to create a global thing to avoid the transmission of it is the smell of your design, this is not a global variable or a singleton function. If you examine your design more closely, you can almost always think of a better design that doesn't have to pass "stray data" around to every object and method.

2, Singleton mode allows you to limit the number of objects you create
That's true, but now you're mixing two different responsibilities into the same class, which violates the principle of single responsibility. A class should not care whether it is a singleton, it should only care about its business responsibilities. If you want to limit the instantiation capabilities of certain classes, create a factory or object builder that encapsulates the creation and limits the ability to create as you wish, so that creating responsibilities is separated from the responsibilities of the business entity.

3. The singleton mode facilitates tight coupling between classes
One of the basic properties that makes code testable is that it is loosely coupled to its surroundings. This property allows you to replace an alternate partner in the test process to achieve a specific test target (think of a mock object). The singleton pattern tightly couples the exact type of the Singleton object, losing the opportunity to replace it with polymorphism. A better choice, as discussed in the 1th above, is to change your design to allow you to pass objects ' references to your classes and methods, which can alleviate the coupling problem described above.

4. The singleton mode keeps the last state in the process of the program.
The persistent state is the enemy of unit testing. One of the things that makes unit testing effective is that each test must be independent of all other tests. If this is not the case, then the order of the test runs will affect the results of the test. This may cause the test to fail where it should not fail, or even worse, because the test runs in a sequence that causes the test to pass. This may hide the bugs and be evil. A good way to prevent States from carrying a test to another test is to avoid using static variables. The singleton pattern, by its very nature, relies on preserving an instance in a static variable. This is a challenge to test dependencies, and can be avoided by passing references to your classes and methods.

I hope this article has more or less explained my view of singleton mode. I have a small set of links found from Google or elsewhere, including Jim Hyslop and Herb Sutter, who share these ideas as well. If you like them then let me know.

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

http://www.bkjia.com/PHPjc/767675.html www.bkjia.com true http://www.bkjia.com/PHPjc/767675.html techarticle original link: http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx Although this article is not my writing, but I completely agree with the article's views. Brian button may be me ...

  • 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.