search
Homephp教程php手册PHP设计模式享元模式

PHP设计模式——享元模式

声明:本系列博客参考资料《大话设计模式》,作者程杰。

享元模式使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件;它适合用于只是因重复而导致使用无法令人接受的大量内存的大量物件。通常物件中的部分状态是可以分享。常见做法是把它们放在外部数据结构,当需要使用时再将它们传递给享元。

 

UML类图:

 

角色分析:

享元工厂角色(FWFactory):创建并管理BlogModel对象。

所有具体享元父接口角色(BolgModel):接受并作用与外部状态。

具体享元角色(JobsBlog):具体变化点,为内部对象增加储存空间。

 

代码实现:

 

<!--?php
/**
 * Created by PhpStorm.
 * User: LYL
 * Date: 2015/5/16
 * Time: 12:00
 */

/**所有享元父接口角色
 * Interface IBlogModel
 */
interface IBlogModel
{
    function showTime();
    function showColor();
}

/**乔布斯的博客模板
 * Class JobsBlog
 */
class JobsBlog implements IBlogModel
{
    function showTime()
    {
        echo 纽约时间:5点整<br/-->;
    }

    function showColor()
    {
        echo 

Jobs

; } } /**雷军博客模板 * Class LeiJunBlog */ class LeiJunBlog implements IBlogModel { function showTime() { echo 北京时间:17点整
; } function showColor() { echo

雷军

; } } /**博客模板工厂 * Class BlogFactory */ class BlogFactory { private $model=array(); function getBlogModel($name) { if(isset($this->model[$name])) { echo 我是缓存里的
; return $this->model[$name]; } else { try { echo 我是新创建的
; $class=new ReflectionClass($name); $this->model[$name]=$class->newInstance(); return $this->model[$name]; } catch(ReflectionException $e) { echo 你要求的对象我不能创建哦。
; return null; } } } }
客户端调用代码:

 

 

header(Content-Type:text/html;charset=utf-8);
//------------------------门面模式测试代码------------------
require_once ./Flyweight/Flyweight.php;
$factory=new BlogFactory();
$jobs=$factory->getBlogModel(JobsBlog);
if($jobs)
{
    $jobs->showTime();
    $jobs->showColor();
}


$jobs1=$factory->getBlogModel(JobsBlog);
if($jobs1)
{
    $jobs1->showTime();
    $jobs1->showColor();
}


$leijun=$factory->getBlogModel(LeiJunBlog);
if($leijun)
{
    $leijun->showTime();
    $leijun->showColor();
}


$leijun1=$factory->getBlogModel(LeiJunBlog);
if($leijun1)
{
    $leijun1->showTime();
    $leijun1->showColor();
}

$aFanda=$factory->getBlogModel(aFanda);
if($aFanda)
{
    $aFanda->showTime();
    $aFanda->showColor();
}

 

优点:

1.减少运行时对象实例的个数,节省内存

2.将许多“虚拟”对象的状态集中管理

缺点:

一旦被实现,单个的逻辑实现将无法拥有独立而不同的行为

 

适用场景:

当一个类有许多的实例,而这些实例能被同一方法控制的时候,我们就可以使用享元模式。


 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor