`

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

阅读更多

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

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

 

享元模式的定义

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

1、抽象享元(Flyweight)角色

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

2、具体享元(ConcreteFlyweight)角色

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

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

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

4、享元工厂(FlyweightFactory)角色

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

5、客户端(Client)角色

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

 

其类图如下:


太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]

 

 

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

2
1
分享到:
评论

相关推荐

    享元模式 Flyweight Pattern

    享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。

    设计模式(C#)之享元模式(Flyweight Pattern)

    设计模式(C#)之享元模式(Flyweight Pattern),运用共享技术有效地支持大量细粒度的对象。

    享元模式flyweight

    NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/1033359

    设计模式学习笔记--Flyweight享元模式.docx

    设计模式学习笔记--Flyweight享元模式.docx设计模式学习笔记--Flyweight享元模式.docx设计模式学习笔记--Flyweight享元模式.docx设计模式学习笔记--Flyweight享元模式.docx设计模式学习笔记--Flyweight享元模式.docx

    C++设计模式之享元模式(Flyweight)

    主要为大家详细介绍了C++设计模式之享元模式Flyweight,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    php设计模式 FlyWeight (享元模式)

    享元模式英文称为“Flyweight Pattern”,我非常感谢将Flyweight Pattern翻译成享元模式的那位强人,因为这个词将这个模式使用的方式明白得表示了出来;如果翻译成为羽量级模式或者蝇量级模式等等,虽然可以含蓄的...

    第15章_享元模式.ppt

    在享元模式中可以共享的相同内容称为内部状态(Intrinsic State),而那些需要外部...在享元模式中通常会出现工厂模式,需要创建一个享元工厂来负责维护一个享元池(Flyweight Pool)用于存储具有相同内部状态的享元对象。

    C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式) (Level 300)

    C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式) (Level 300)

    结构型模式之共享元模式(Flyweight)

    6共享元模式(Flyweight) 用意:以共享的方式高效地支持大量的细粒度对象

    C#面向对象设计模式纵横谈\12 结构型模式Flyweight享元模式.zip

    在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要... 这是第12节:结构型模式Flyweight享元模式

    C#版 24种设计模式

    适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...

    C#设计模式之Flyweight

    设计模式 Flyweight

    学习php设计模式 php实现享元模式(flyweight)

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

    Python设计模式之享元模式原理与用法实例分析

    享元模式(Flyweight Pattern):运用共享技术有效地支持大量细粒度的对象. 下面是一个享元模式的demo: #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'Andy' """ 大话设计模式 设计模式——享元模式 ...

    C++设计模式课件13_Flyweight_享元模式.pdf

    C++设计模式课件13_Flyweight_享元模式.pdf

    享元模式代码示例

    享元模式的示例代码和文档,学习享元模式的参考资料。

    用Java实现23种设计模式

    享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3. 行为型模式 责任链模式(Chain of Responsibility Pattern) 命令模式(Command Pattern) 解释器模式(Interpreter Pattern) 迭代器模式...

    C#设计模式_设计模式_C#_

    享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型: 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 16. 观察者模式(Observer Pattern) 17. ...

    Java设计模式,并加上个人理解

    1. 设计模式 1.1 含义 1.2 作用 1.3 设计原则 1.4 分类 2. 简单工厂模式 (SimpleFactoryPattern) ...15. 享元模式 (Flyweight Pattern) 16. 桥接模式 (Bridge Pattern) 17. 观察者模式 (Observer Pattern)

Global site tag (gtag.js) - Google Analytics