Home  >  Article  >  Backend Development  >  Learn PHP design patterns PHP implements flyweight mode (flyweight)_php skills

Learn PHP design patterns PHP implements flyweight mode (flyweight)_php skills

WBOY
WBOYOriginal
2016-05-16 20:03:37967browse

1. Intention
Use sharing technology to effectively support a large number of fine-grained objects
Flyweight mode changes the storage overhead of objects
2. Flyweight mode structure diagram

3. Main characters in flyweight mode
Abstract Flyweight role: This role is the super class of all concrete flyweight classes, and specifies the public interfaces that need to be implemented for these classes. Those operations that require external state can be passed in as parameters by calling the business
Concrete Flyweight role: Implements the Flyweight interface and pulls back storage space for internal state (if any). ConcreteFlyweight objects must be shareable. The state it stores must be internal
UnsharedConcreteFlyweight role: Not all Flyweight subclasses need to be shared. Flyweigth makes sharing possible, but it doesn't force sharing.
FlyweightFactory role: Responsible for creating and managing Flyweight roles. This role must ensure that flyweight objects can be properly shared by the system
Client role: This role needs to maintain a reference to all flyweight objects. This character needs to store the external state of all flyweight objects by itself
4. Advantages and Disadvantages of Flyweight Model
Advantages of flyweight mode: Flyweight mode can significantly reduce the number of objects in memory.
Disadvantages of flyweight mode:
1. Flyweight mode makes the system more complex
2. Flyweigth mode externalizes the state of the flyweight object, and reading the external state makes the running time slightly longer
5. Applicable Scenarios of Flyweight Mode
Use Flyweight mode when the following conditions are true:
1. An application uses a large number of objects
2. Completely due to the use of a large number of objects, a large storage overhead is caused
3. Most of the states of the object can be changed to external states
4. If you delete the external state of an object, you can replace many groups of objects with relatively few shared objects
5. The application does not depend on object identification.
6. Flyweight mode and other modes
Singleton mode: The client wants to reference a flyweight object, which is created or obtained through a factory object. Every time the client references a flyweight object, it can be referenced through the same factory object. The required flyweight object. Therefore, the flyweight factory can be designed in singleton mode, which ensures that the client only references one factory instance. Because all flyweight objects are managed uniformly by a factory object, there is no need to reference multiple factory objects on the client side. Whether it is a simple flyweight mode or a flyweight factory role in a composite flyweight mode, it can be designed as a singleton mode, and it will not have any impact on the results.
Composite mode: The composite flyweight mode is actually a combination of the simple flyweight mode and the composite mode. A simple flyweight object can be used as a leaf object and can be shared, while a composite flyweight object can be used as a branch object, so an aggregation management method can be added to the composite flyweight role.
7. Flyweight mode PHP example

<&#63;php
/**
 * 抽象享元角色
 */
abstract class Flyweight {
 
 /**
  * 示意性方法
  * @param string $state 外部状态
  */
 abstract public function operation($state);
}
 
/**
 * 具体享元角色
 */
class ConcreteFlyweight extends Flyweight {
 
 private $_intrinsicState = null;
 
 /**
  * 构造方法
  * @param string $state 内部状态
  */
 public function __construct($state) {
  $this->_intrinsicState = $state;
 }
 
 public function operation($state) {
  echo 'ConcreteFlyweight operation, Intrinsic State = ' . $this->_intrinsicState
  . ' Extrinsic State = ' . $state . '<br />';
 }
 
}
 
/**
 * 不共享的具体享元,客户端直接调用
 */
class UnsharedConcreteFlyweight extends Flyweight {
 
 private $_intrinsicState = null;
 
 /**
  * 构造方法
  * @param string $state 内部状态
  */
 public function __construct($state) {
  $this->_intrinsicState = $state;
 }
 
 public function operation($state) {
  echo 'UnsharedConcreteFlyweight operation, Intrinsic State = ' . $this->_intrinsicState
  . ' Extrinsic State = ' . $state . '<br />';
 }
 
}
 
/**
 * 享元工厂角色
 */
class FlyweightFactory {
 
 private $_flyweights;
 
 public function __construct() {
  $this->_flyweights = array();
 }
 
 public function getFlyweigth($state) {
  if (isset($this->_flyweights[$state])) {
   return $this->_flyweights[$state];
  } else {
   return $this->_flyweights[$state] = new ConcreteFlyweight($state);
  }
 }
 
}
 
/**
 * 客户端
 */
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');
 
  /* 不共享的对象,单独调用 */
  $uflyweight = new UnsharedConcreteFlyweight('state A');
  $uflyweight->operation('other state A');
 }
 
}
 
Client::main();
&#63;>

8. Composite flyweight mode
The composite flyweight mode object is composed of some simple flyweights using the composition mode
The object represented by the composite flyweight role cannot be shared, but a composite flyweight object can be decomposed into multiple combinations of simple flyweight objects.
9. Composite Flyweight Mode PHP Example

<&#63;php
/**
 * 抽象享元角色
 */
abstract class Flyweight {
 
 /**
  * 示意性方法
  * @param string $state 外部状态
  */
 abstract public function operation($state);
}
 
/**
 * 具体享元角色
 */
class ConcreteFlyweight extends Flyweight {
 
 private $_intrinsicState = null;
 
 /**
  * 构造方法
  * @param string $state 内部状态
  */
 public function __construct($state) {
  $this->_intrinsicState = $state;
 }
 
 public function operation($state) {
  echo 'ConcreteFlyweight operation, Intrinsic State = ' . $this->_intrinsicState
  . ' Extrinsic State = ' . $state . '<br />';
 }
 
}
 
/**
 * 不共享的具体享元,客户端直接调用
 */
class UnsharedConcreteFlyweight extends Flyweight {
 
 private $_flyweights;
 
 /**
  * 构造方法
  * @param string $state 内部状态
  */
 public function __construct() {
  $this->_flyweights = array();
 }
 
 public function operation($state) {
  foreach ($this->_flyweights as $flyweight) {
   $flyweight->operation($state);
  }
 }
 
 public function add($state, Flyweight $flyweight) {
  $this->_flyweights[$state] = $flyweight;
 }
 
}
 
/**
 * 享元工厂角色
 */
class FlyweightFactory {
 
 private $_flyweights;
 
 public function __construct() {
  $this->_flyweights = array();
 }
 
 public function getFlyweigth($state) {
  if (is_array($state)) { // 复合模式
   $uFlyweight = new UnsharedConcreteFlyweight();
 
   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;
  }
 }
 
}
 
/**
 * 客户端
 */
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');
 
  /* 复合对象*/
  $uflyweight = $flyweightFactory->getFlyweigth(array('state A', 'state B'));
  $uflyweight->operation('other state A');
 }
 
}
 
Client::main();
&#63;>

10. The status of flyweight mode in PHP
Compared with other modes, Flyweight mode does not make much sense in the current version of PHP, because the life cycle of PHP is at the page level, that is, starting from the execution of a PHP file, the required resources will be loaded. When the execution is completed, All these resources will be released, and generally speaking we will not let a page execute for too long.

The above is the code for using PHP to implement the flyweight mode. There are also some conceptual distinctions about the flyweight mode. I hope it will be helpful to everyone's learning.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn