学习php设计模式 php实现享元模式(flyweight)
《PHP实战:学习php设计模式 php实现享元模式(flyweight)》要点: PHP教程一、意图 PHP教程 PHP教程三、享元模式中主要角色 PHP教程
<?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();
?>
PHP教程八、复合享元模式 PHP教程
<?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,客户端直接调用
*/
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();
?>
PHP教程十、PHP中享元模式的地位 PHP教程以上就是使用php实现享元模式的代码,还有一些关于享元模式的概念区分,希望对大家的学习有所赞助. 欢迎参与《PHP实战:学习php设计模式 php实现享元模式(flyweight)》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |