懂得分享的享元模式(Flyweight Pattern)

今天学习一个对php来说意义不大的模式——享元模式

竟然意义不大为什么要学?我一直认为设计模式是不针对语言的,相对于目前的php意义不大,并不表示其本身没有意义,其在编译型的语言里还是有很多的用处的。

 

享元模式的定义

享元模式是池技术的重要实现方式,其定义为:使用共享对象可有效地支持大量的细粒度的对象。太简单了,通俗地说就是把经常要使用到的对象共享起来,而不去从新创建而占用内存。其大致构成:

1、抽象享元(Flyweight)角色

此角色是所有的具体享元类的超类,为这些类规定出需要实现的公共接口。那些需要外蕴状态的操作可以通过调用商业以参数形式传入

2、具体享元(ConcreteFlyweight)角色

实现Flyweight接口,并为内部状态(如果有的话)拉回存储空间。ConcreteFlyweight对象必须是可共享的。它所存储的状态必须是内部的

3、不共享的具体享元(UnsharedConcreteFlyweight)角色

并非所有的Flyweight子类都需要被共享。Flyweigth使共享成为可能,但它并不强制共享。

4、享元工厂(FlyweightFactory)角色

负责创建和管理享元角色。本角色必须保证享元对象可能被系统适当地共享

5、客户端(Client)角色

本角色需要维护一个对所有享元对象的引用。本角色需要自行存储所有享元对象的外部状态

 

其类图如下:

懂得分享的享元模式(Flyweight Pattern)
太easy了,实现代码如下:

 

<?php
/**
 *  抽象享元角色
 */
abstract class Flyweight {
	abstract public function operation( $state );
}
/**
 * 具体享元角色
 */
class ConcreteFlyweight extends Flyweight {
	private $_intrinsicState = null;
	public function __construct( $state ) {
		$this->_intrinsicState = $state;
	}
	public function operation( $state ) {
		echo 'ConcreteFlyweight operation, Intrinsic State = ' . $this->_intrinsicState. ' Extrinsic State = ' . $state . "\n";
	}
}
/**
 * 不共享的具体享元,客户端直接调用
 */
class UnsharedConcreteFlyweight extends Flyweight {
	private $_intrinsicState = null;
	public function __construct( $state ) {
		$this->_intrinsicState = $state;
	}
	public function operation( $state ) {
		echo 'UnsharedConcreteFlyweight operation, Intrinsic State = ' . $this->_intrinsicState. ' Extrinsic State = ' . $state . "\n";
	}
}
/**
 * 享元工厂角色
 */
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 );
		}
	}
}
$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' );

?>
运行结果:
ConcreteFlyweight operation, Intrinsic State = state A Extrinsic State = other state A
ConcreteFlyweight operation, Intrinsic State = state B Extrinsic State = other state B
UnsharedConcreteFlyweight operation, Intrinsic State = state A Extrinsic State = other state A
[Finished in 0.3s]

 

 

 

享元模式的优点

享元模式可以大大减少应用程序创建的对象,降低程序内存的占用,增强程序的性能,但它同时也提高了系统的复杂性,需要分离出外部状态和内部状态,而且外部状态具有固化特性,不应该随内部状态改变而改变,否则导致系统的逻辑混乱。

 

 

享元模式的使用场景

1、系统中存在大量的相似对象

2、细粒度的对象都具备较接近的外部状态,而且内部状态与环境无关,也就是说对象没有特定身份。

3、需要缓冲池的场景

 

享元模式的扩展

1、结合单例模式

2、Composite模式

复合享元模式实际上是单纯享元模式与合成模式的组合。单纯享元对象可以作为树叶对象来讲,是可以共享的,而复合享元对象可以作为树枝对象,因此在复合享元角色中可以添加聚集管理方法

 

<?php
abstract class Flyweight {
	abstract public function operation( $state );
}
/**
 * * 具体享元角色
 */
class ConcreteFlyweight extends Flyweight {
	private $_intrinsicState = null;
	public function __construct( $state ) {
		$this->_intrinsicState = $state;
	}
	public function operation( $state ) {
		echo 'ConcreteFlyweight operation, Intrinsic State = ' . $this->_intrinsicState . ' Extrinsic State = ' . $state . "\n";
	}
}
/**
 * * 不共享的具体享元,客户端直接调用
 */
class UnsharedConcreteFlyweight extends Flyweight {
	private $_flyweights;
	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;
		}
	}
}
$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' );
?>
运行结果:
ConcreteFlyweight operation, Intrinsic State = state A Extrinsic State = other state A
ConcreteFlyweight operation, Intrinsic State = state B Extrinsic State = other state B
ConcreteFlyweight operation, Intrinsic State = state A Extrinsic State = other state A
ConcreteFlyweight operation, Intrinsic State = state B Extrinsic State = other state A
[Finished in 0.2s]

 

 

享元模式主要解决的是对象的共享问题,如何建立多个可共享的细粒度对象则是其关注的重点。

你可能感兴趣的:(设计模式,PHP,享元模式)