<?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
/**
* 抽象享元角色
*/
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();
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有