Design mode (ii) Single-piece mode singleton (created)

Source: Internet
Author: User
Tags vars zend framework

SINGLETON (Single piece)-Object creation mode

In almost all object-oriented programs, there are always objects in the class that need to be unique, for example, a connection to a database through a database handle is exclusive. You want to share the database handle in your application because it is an overhead when the connection is kept open or closed. Again like everyone most often use of IM, such as QQ, in the same computer, an account can only have a unique login.

1. Questions

How do you ensure that an instance of a particular class is unique (it is the only instance of the class) and that the instance is easy to access?

2. Solution

1) Global variables: A global variable allows an object to be accessed, but it does not prevent you from instantiating multiple objects. Because any of your code can modify global variables, this will inevitably cause more debugging surprises. In other words, there are always some problems with the state of global variables.

2) static methods of class constructor private and class itself: let the class itself be responsible for saving its only instance (static variable). This class guarantees that no other instance can be created (by intercepting a request to create a new object), and that it can provide a way to access the instance (a static method). This is the singleton mode.


3. Applicability
Single-piece mode can be used in the following cases
1) When a class can have only one instance and the customer can access it from a well-known access point.

2) When this unique instance should be extensible by subclasses, and the customer should be able to use an extended instance without changing the code.

4. Achieve:

UML Structure:

Code:

[PHP]View PlainCopyprint?
  1. <?php
  2. Class Singleton {
  3. static private $_instance = null; Static members save unique instances
  4. /** 
  5. * Private constructor, guaranteed not to be accessed externally
  6. *
  7. */
  8. Private function __construct () {}
  9. /** 
  10. * The static method will create the operation of this instance and ensure that only one instance is created
  11. *
  12. * @return Unknown
  13. */
  14. public static function getinstance () {
  15. if (!self::$_instance) {
  16. Self::$_instance = new self ();
  17. }
  18. return self::$_instance;
  19. }
  20. }

5. Effects

The singleton model has many advantages :
1) controlled access to a unique instance, because the Singleton class encapsulates its only instance, so it can tightly control how and when the customer accesses it.
2) Reduce the name space, Singleton mode is an improvement to the global variables. It avoids those global variables that store unique instances of the pollution name space.
3) It is easy to configure an application with an instance of the extension class, allowing the refinement of the Singleton class to operate and represent a subclass. You can configure your app at run time with an instance of the class you need.
4) Allow variable number of instances this mode allows you to easily change your mind and allow multiple instances of the Singleton class. In addition, you can use the same method to control the number of instances used by your app. Only operations that allow access to singleton instances need to be changed.


6. Single-piece mode can be multiple instances

One-piece mode does not mean that a class can have only one instance. Let's say we're using a Web request or process. A class for a user ID can have only one instance. In the following example, our user class can have multiple instances, each of which corresponds to a UID. The instance list is registered to the static variable $_instance and associated with the UID. The simplest example is the QQ that we mentioned earlier, in the same computer, can use multiple account login, but an account can only have a unique login.

Code:

[PHP]View PlainCopyprint?
  1. <?php
  2. Class User {
  3. static private $_instance = Array (); Static members save unique instances
  4. private $_uid;
  5. /** 
  6. * Private constructor, guaranteed not to be accessed externally
  7. *
  8. */
  9. Private function __construct ($uid) {
  10. $this->_uid = $uid;
  11. }
  12. /** 
  13. * The static method will create the operation of this instance and ensure that only one instance is created
  14. *
  15. * @return Unknown
  16. */
  17. public static function getinstance ($uid = 0) {
  18. if (!self::$_instance | | |!isset (self::$_instance[$uid])) {
  19. Self::$_instance[$uid] = new self ($uid);
  20. }
  21. return self::$_instance[$uid];
  22. }
  23. }

Application

The Zend_controller_front front-end controller in the Zend Framework is designed with a unit price pattern:

Zend_controller_front is the organizer of the Zend_controller_controller system, which is the implementation of the Frontcontroller design pattern.

Zend_controller_front handles all requests accepted by the server and is ultimately responsible for assigning the request to Actioncontroller (zend_controller_action)

[PHP]View PlainCopyprint?
    1. $frontController = Zend_controller_front::getinstance ();
    2. $frontController->addmoduledirectory ("parameters");
    3. $frontController->dispatch ();

Design mode (ii) Single-piece mode singleton (created)

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.