Learn PHP Design patterns PHP Implementation of the Flyweight mode (_php) skills

Source: Internet
Author: User
Tags abstract learn php php example

I. Intention
using shared technology to effectively support a large number of fine-grained objects
The meta mode changes the storage overhead of the object
II. pattern Structure of the element

The main characters in the mode of enjoying meta
abstract (Flyweight) role:This role is the superclass of all of the specific meta classes, specifying the public interfaces that need to be implemented for these classes. Operations that require the presence of a foreign state can be passed in by calling business as arguments
specific (concreteflyweight) role:Implements the flyweight interface and pulls back the storage space for internal state (if any). The Concreteflyweight object must be shareable. The state it stores must be internal.
non-shared specific (unsharedconcreteflyweight) role:Not all flyweight subclasses need to be shared. Flyweigth makes sharing possible, but it does not force sharing.
The Flyweightfactory Factory (The) Role:Responsible for creating and managing the privilege role. This role must ensure that the object of the privilege is properly shared by the system
Client Role:This role needs to maintain a reference to all of the object's privileges. This role needs to store the external state of all the objects
Iv. Advantages and disadvantages of the mode of the privilege
of the META modeAdvantages:Flyweight mode can significantly reduce the number of objects in memory.
of the META modeDisadvantages:
1, Flyweight mode makes the system more complex
2, Flyweigth mode will be the state of the object of the foreign, and read the external state to make the running time slightly longer
Five, the mode of enjoying the application of the scene
Use the flyweight mode when the following conditions are true:
1. An application uses a large number of objects
2, completely because of the use of a large number of objects, resulting in a large storage overhead
3, most of the object's State can become an external state
4. If you delete the external state of an object, you can replace many groups of objects with a relatively small number of shared objects
5, the application does not depend on the object identity.
the mode of enjoying meta and other modes
Single case Mode (Singleton): The client is to refer to the object, which is created or obtained by a factory object, and each time a client references a single object, it is possible to refer to the desired object through the same factory object. Therefore, you can design the Meta factory as a singleton mode, which guarantees that the client only references one factory instance. Because all of the privilege objects are managed uniformly by one factory object, it is not necessary to refer to multiple factory objects at the client. Regardless of the exclusive mode or the role of the Meta factory in the composite mode, it can be designed to be a single case pattern and will have no effect on the result.
Composite mode: The compound-element mode is actually a combination of the pure-element mode and the synthetic mode. A single object can be shared as a leaf object, and a composite object can be used as a tree branch object, so you can add a clustered management method in a composite-privilege role.
Seven, enjoy meta mode PHP sample

<?php/** * Abstract class Flyweight {/** * Schematic method * @param string $state external state */abstract Publi
C function operation ($state);
 
 }/** * Specific privilege role */class Concreteflyweight extends Flyweight {private $_intrinsicstate = null; /** * Construction Method * @param string $state Internal state */Public function __construct ($state) {$this->_intrinsicstate = $stat
 E The public Function operation ($state) {echo ' concreteflyweight operation, intrinsic state = '. $this->_intrinsicst Ate. ' Extrinsic state = '. $state.
 ' <br/> '; }/** * does not share the specific element, the client directly calls * * Class Unsharedconcreteflyweight extends Flyweight {private $_intrinsicstate = null
 
 ; /** * Construction Method * @param string $state Internal state */Public function __construct ($state) {$this->_intrinsicstate = $stat
 E The public Function operation ($state) {echo ' unsharedconcreteflyweight operation, intrinsic state = '. $this->_int Rinsicstate. ' Extrinsic state = '. $state. ' <br/>';
 
 }/** * Enjoy the yuan factory role * * Class Flyweightfactory {private $_flyweights;
 Public Function __construct () {$this->_flyweights = array (); The Public Function getflyweigth ($state) {if (Isset ($this->_flyweights[$state))} {return $this->_flyweights[
  $state];
  else {return $this->_flyweights[$state] = new Concreteflyweight ($state);
  }}/** * Client/class Client {/** * Main program.
  */public static function main () {$flyweightFactory = new flyweightfactory ();
  $flyweight = $flyweightFactory->getflyweigth (' state A ');
 
  $flyweight->operation (' other State A ');
  $flyweight = $flyweightFactory->getflyweigth (' state B ');
 
  $flyweight->operation (' other State B ');
  /* Do not share the object, separate call/$uflyweight = new Unsharedconcreteflyweight (' state A ');
 $uflyweight->operation (' other State A ');
 } client::main ();?>

The

Eight, composite-element-mode
Composite-Privilege Mode object is not shared by some simple elements that are combined by using a composite pattern to represent objects that are represented by the
Composite role. However, a composite element object can be decomposed into multiple combinations that are itself a purely exclusive object.
Nine, compound-enjoy-mode PHP example

<?php/** * Abstract class Flyweight {/** * Schematic method * @param string $state external state */abstract Publi
C function operation ($state);
 
 }/** * Specific privilege role */class Concreteflyweight extends Flyweight {private $_intrinsicstate = null; /** * Construction Method * @param string $state Internal state */Public function __construct ($state) {$this->_intrinsicstate = $stat
 E The public Function operation ($state) {echo ' concreteflyweight operation, intrinsic state = '. $this->_intrinsicst Ate. ' Extrinsic state = '. $state.
 ' <br/> ';
 
 }/** * does not share the specific element, the client directly calls/class Unsharedconcreteflyweight extends Flyweight {private $_flyweights;
 /** * Construction Method * @param string $state Internal state */Public function __construct () {$this->_flyweights = array (); The public Function operation ($state) {foreach ($this->_flyweights as $flyweight) {$flyweight->operation ($st
  ATE); } Public Function Add ($state, Flyweight $flyweight) {$this->_flyweights[$state] = $flyweight;
 
 }/** * Enjoy the yuan factory role * * Class Flyweightfactory {private $_flyweights;
 Public Function __construct () {$this->_flyweights = array (); Public Function Getflyweigth ($state) {if (Is_array ($state)) {//Composite mode $uFlyweight = new Unsharedconcreteflyweig
 
   HT ();
   foreach ($state as $row) {$uFlyweight->add ($row, $this->getflyweigth ($row));
  return $uFlyweight;
   else if (is_string ($state)) {if (Isset ($this->_flyweights[$state)) {return $this->_flyweights[$state];
   else {return $this->_flyweights[$state] = new Concreteflyweight ($state);
  } else {return null;
  }}/** * Client/class Client {/** * Main program.
  */public static function main () {$flyweightFactory = new flyweightfactory ();
  $flyweight = $flyweightFactory->getflyweigth (' state A ');
 
  $flyweight->operation (' other State A ');
  $flyweight = $flyweightFactory->getflyweigth (' state B '); $flywEight->operation (' other State B ');
  /* Compound object/$uflyweight = $flyweightFactory->getflyweigth (' State A ', ' state B '));
 $uflyweight->operation (' other State A '); } client::main ();?>

Ten, PHP in the status of the mode of enjoying the element
In contrast to other patterns, the flyweight model does not have much meaning in the existing version of PHP, because the lifecycle of PHP is page-level, that is, starting with a PHP file will load the required resources, and when executed, all of these resources will be released. And generally we don't let a page execute too long.

The above is the use of PHP to implement the pattern of the code, there are some of the concept of the pattern of the benefit of the distinction, 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.