Learn PHP Design patterns PHP Implementation state mode _php skills

Source: Internet
Author: User

I. Intention
Allows an object to change its behavior when its internal state changes. Object seems to have modified its class
The position of the state pattern varies in the state of the object

Second, the state mode structure diagram

Third, main role in state mode
abstract State role: Defines an interface that encapsulates the behavior of a particular state of an environment object
Specific state (concretestate) role: Each specific state class implements the behavior of a state of the environment (context)
Environment (context) role: Defines the interface to which the client is interested, and retains an instance of a specific State class. An instance of this specific State class gives the existing state of this environment object
Four, the advantages and disadvantages of the state mode
1, it will localize the behavior associated with a particular state
2, it makes the state transition display
3 , the state object can be shared
Five, the status mode applies to the scene
1, the behavior of an object depends on its state, and it must change its behavior according to state at run time
2, An operation contains large, multiple-branched conditional statements that depend on the state of the object. This state is usually represented by one or more enumerated constants. Typically, there are multiple operations that contain this same conditional structure. The state pattern pattern places each conditional branch into a separate class. This allows you to take the state of an object as an object of its own, an object that can vary independently of other objects
Six, state mode and other modes
Single mode ( Singleton mode): A specific state object is usually a single case pattern
"Flyweight mode": The meta mode explains when and how to share state objects
VII, State mode PHP example

<?php/** * Abstract status Role/interface state {/** * Method Example */Public function handle (context $context);
 
  /** * Specific status role A * Single Instance class */class Concretestatea implements state {/* * Unique example * * private static $_instance = null;  Private Function __construct () {}/** * Static Factory method, returning a unique instance of this class/public static function getinstance () {if (Is_null (self::$_instance))
    {self::$_instance = new Concretestatea ();
  return self::$_instance;
    The public function handle (context $context) {echo ' concrete sate A handle method<br/> ';
  $context->setstate (Concretestateb::getinstance ()); }/** * Specific status Role B * Singleton class/class Concretestateb implements state {/* Unique instance * * private static $_instance = Nu
 
  ll Private Function __construct () {}/** * Static Factory method, returning a unique instance of this class/public static function getinstance () {if (
    Is_null (self::$_instance)) {self::$_instance = new concretestateb (); Return Self::$_instanCE
    The public function handle (context $context) {echo ' concrete sate B handle method<br/> ';
  $context->setstate (Concretestatea::getinstance ());
 
  }/** * Environment role */class Context {private $_state;
  /** * defaults to Statea/Public function __construct () {$this->_state = concretestatea::getinstance ();
  The Public Function SetState (state $state) {$this->_state = $state;
  The Public Function request () {$this->_state->handle ($this);
   }/** * Client/class Client {/** * Main program.
    * * public static function main () {$context = new context ();
    $context->request ();
    $context->request ();
    $context->request ();
  $context->request (); } client::main ();?>

The above is the use of PHP to implement the state mode of the code, there are some of the concept of state mode of separation, I hope to help you learn.

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.