Learn PHP Design Patterns PHP Implementation single case mode (singleton) _php tips

Source: Internet
Author: User

Guarantees that a class has only one instance and provides a global access point to access it.
The single case pattern has three features:
1, a class has only one instance
2. It must create this instance on its own
3. This example must be provided to the whole system by itself
One, single case pattern structure diagram

Ii. main roles in a single case model
Singleton defines a instance operation that allows the client to access its unique instance. Instance is a class method. The only instance that is responsible for creating it.
The advantages of a single case model
1. Controlled access to a unique instance
2. Narrowing the single case pattern of namespaces is an improvement on global variables. It avoids the global variable pollution namespaces that store unique instances
3. Allow for operation and presentation of the essence of a single instance class can have subclasses. It is also easy to configure an application with an instance of this extended class. You can configure the application at run time using an instance of the class you need.
4. Allow variable number of instances (multiple case mode)
5, more flexible than class operation
Four, the single case pattern applies the scene
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 through subclasses. And the user should be able to use an extended instance without changing the code.
V. Single case mode and other modes "
Factory approach Mode (Factory method mode): Singleton mode uses Factory mode to provide its own instance.
Abstract Factory Pattern: Abstract Factory mode factory can use a single example pattern to design a specific factory class as a single instance class.
Builder model (Builder model): The construction model can be designed as a single case model for concrete construction classes.
Examples of single case mode PHP

<?php
/**
 * Lazy Type Single case class/class
Singleton {
 
 /**
  * Static product variable Save global instance
  /
 private static $_ instance = NULL;
 
 /**
  * Privatization default construction method to ensure that the outside can not directly instantiate
 /Private Function __construct () {
 }
 
 /**
  * Static Factory method, Return the unique instance of this
  class
 /public static function getinstance () {
  if (Is_null (self::$_instance)) {
   self::$_ Instance = new Singleton ();
  }
 
  return self::$_instance;
 }
 
 /**
  * Prevents the user from cloning instances
 /Public Function __clone () {
  die (' clone isn't allowed. '). e_user_error);
 }
 
 /**
  * Test method *
 /Public Function test () {
  echo ' Singleton test! ';
 }
 
}
 
/**
 * Client
 /
class Client {
 
  /**
  * Main program.
  */public
 static function main () {
  $instance = singleton::getinstance ();
  $instance->test ();
 }
 
Client::main ();
? >

The above is the use of PHP to implement a single example mode of code, there are some of the concept of a single example of the distinction between the hope that the learning of everyone to help.

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.