PHP implements singleton pattern and observer pattern

Implementation of the singleton pattern:

The singleton mode in PHP is often used in the database connection part, which saves a lot of new operations and saves a lot of resources. The singleton pattern can also be used in global configuration classes.

The singleton pattern, as the name implies, means that there is only one instance, which requires preventing external objects from being instantiated at will.

The main points of the singleton pattern in PHP:

1. The constructor must be private to prevent external illegal new operations.

2. There needs to be a private static class member variable that stores instances of this class.

3. The clone function needs to be privatized to prevent external cloning.

4. There is a public static method to obtain an instance of this class.

code show as below:

<?php

/**
*PHP implements the singleton pattern
*/
class Db
{
    // Save this class object 
    private  static $instance;
     // Test information 
    private $message;
     // Private constructor 
    private function __construct($message) {
        $this->message = $message;
    }
    // Prevent being cloned 
    private function __clone() {}
     // Method to get instance of this class 
    public  static function getInstance($message) {
         if (! self::$instance instanceof self) {
            self::$instance = new self($message);
        }
        return self::$instance;
    }
    public function getMessage() {
        echo $this->message;
    }
}
$db1 = Db::getInstance( " I am the first instance! " );
$db1->getMessage();
echo "<br/>";
$db2 = Db::getInstance( " I am the second instance! " );
$db2->getMessage();

?>

 

The results are as follows:

I am the first instance!
I am the first instance!

Implementation of the observer pattern:

There are mainly two types of objects in the observer pattern, one is the subject (or the observed) and the observer. The subject provides the interface for registering the observer and notifying the observer.

A simple usage scenario is booking tickets:

When booking a ticket, there are usually many follow-up operations, such as: record log, SMS notification, send points and so on. When the topic changes, the ticket is booked here, and the observers will be notified to perform the corresponding operation, which is to call the observer's method.

code show as below:

 

<?php

/**
*PHP implements the observer pattern
*/

/* Subject interface */ 
interface Subject
{
    public function register(Observer $observer);
    public function notify($type);
}

/* Observer interface */ 
interface Observer
{
    public function watch($type);
}

/* Subject implementation class */ 
class Order implements Subject
{
    public $_observers = array();
    public function register(Observer $observer) {
        $this->_observers[] = $observer;
    }
    public function notify($type) {
        foreach($this->_observers as $observer) {
            $observer->watch($type);
        }
    }
}

/* Three observer implementation classes */

class Log implements Observer
{
    public  function watch( $type ) {
         echo "Log received notification: $type <br/>" ;
    }
}

class Message implements Observer
{
    public  function watch( $type ) {
         echo "SMS received notification: $type <br/>" ;
    }
}

class Reward implements Observer
{
    public  function watch( $type ) {
         echo "Reward received notification: $type <br/>" ;
    }
}

define('EVENT_ORDER', 1);
$order = new Order();
$order->register(new Log());
$order->register(new Message());
$order->register(new Reward());
$order->notify(EVENT_ORDER);

?>

 

operation result:

Log received notification: 1
SMS received notification: 1
Reward received notification: 1

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325356865&siteId=291194637