享元模式

2021-05-10  本文已影响0人  突然的自我_39c1

1.1. 模式动机

面向对象技术可以很好地解决一些灵活性或可扩展性问题,但在很多情况下需要在系统中增加类和对象的个数。当对象数量太多时,将导致运行代价过高,带来性能下降等问题。

1.2. 模式定义

享元模式(Flyweight Pattern):运用共享技术有效地支持大量细粒度对象的复用。系统只使用少量的对象,而这些对象都很相似,状态变化很小,可以实现对象的多次复用。由于享元模式要求能够共享的对象必须是细粒度对象,因此它又称为轻量级模式,它是一种对象结构型模式。

1.3. 模式结构

享元模式包含如下角色:

Flyweight

1.4. 时序图

seq_Flyweight

1.5. 代码分析

#include <iostream>
#include "ConcreteFlyweight.h"
#include "FlyweightFactory.h"
#include "Flyweight.h"
using namespace std;

int main(int argc, char *argv[])
{
    FlyweightFactory factory;
    Flyweight * fw = factory.getFlyweight("one");
    fw->operation();

    Flyweight * fw2 = factory.getFlyweight("two");
    fw2->operation();
    //aready exist in pool
    Flyweight * fw3 = factory.getFlyweight("one");
    fw3->operation();
    return 0;
}

///////////////////////////////////////////////////////////
//  FlyweightFactory.cpp
//  Implementation of the Class FlyweightFactory
//  Created on:      06-十月-2014 20:10:42
//  Original author: colin
///////////////////////////////////////////////////////////

#include "FlyweightFactory.h"
#include "ConcreteFlyweight.h"
#include <iostream>
using namespace std;

FlyweightFactory::FlyweightFactory(){

}

FlyweightFactory::~FlyweightFactory(){

}

Flyweight* FlyweightFactory::getFlyweight(string str){
    map<string,Flyweight*>::iterator itr = m_mpFlyweight.find(str);
    if(itr == m_mpFlyweight.end())
    {
        Flyweight * fw = new ConcreteFlyweight(str);
        m_mpFlyweight.insert(make_pair(str,fw));
        return fw;  
    }
    else
    {
        cout << "aready in the pool,use the exist one:" << endl;
        return itr->second;
    }       
}

///////////////////////////////////////////////////////////
//  ConcreteFlyweight.h
//  Implementation of the Class ConcreteFlyweight
//  Created on:      06-十月-2014 20:10:42
//  Original author: colin
///////////////////////////////////////////////////////////

#if !defined(EA_C0AF438E_96E4_46f1_ADEC_308EF16E11D1__INCLUDED_)
#define EA_C0AF438E_96E4_46f1_ADEC_308EF16E11D1__INCLUDED_

#include "Flyweight.h"
#include <string>
using namespace std;

class ConcreteFlyweight : public Flyweight
{

public:
    ConcreteFlyweight(string str);
    virtual ~ConcreteFlyweight();

    virtual void operation();

private:
    string intrinsicState;

};
#endif // !defined(EA_C0AF438E_96E4_46f1_ADEC_308EF16E11D1__INCLUDED_)

///////////////////////////////////////////////////////////
//  ConcreteFlyweight.cpp
//  Implementation of the Class ConcreteFlyweight
//  Created on:      06-十月-2014 20:10:42
//  Original author: colin
///////////////////////////////////////////////////////////

#include "ConcreteFlyweight.h"
#include <iostream>
using namespace std;

ConcreteFlyweight::ConcreteFlyweight(string str){
    intrinsicState = str;
}

ConcreteFlyweight::~ConcreteFlyweight(){

}

void ConcreteFlyweight::operation(){
    cout << "Flyweight[" << intrinsicState << "] do operation." << endl; 
}

运行结果:

Flyweight_run

1.6. 模式分析

享元模式是一个考虑系统性能的设计模式,通过使用享元模式可以节约内存空间,提高系统的性能。

享元模式的核心在于享元工厂类,享元工厂类的作用在于提供一个用于存储享元对象的享元池,用户需要对象时,首先从享元池中获取,如果享元池中不存在,则创建一个新的享元对象返回给用户,并在享元池中保存该新增对象。

享元模式以共享的方式高效地支持大量的细粒度对象,享元对象能做到共享的关键是区分内部状态(Internal State)和外部状态(External State)。

1.7. 实例

1.8. 优点

享元模式的优点

1.9. 缺点

享元模式的缺点

1.10. 适用环境

在以下情况下可以使用享元模式:

1.11. 模式应用

享元模式在编辑器软件中大量使用,如在一个文档中多次出现相同的图片,则只需要创建一个图片对象,通过在应用程序中设置该图片出现的位置,可以实现该图片在不同地方多次重复显示。

1.12. 模式扩展

单纯享元模式和复合享元模式

享元模式与其他模式的联用

1.13. 总结

摘自:https://www.doudianyun.com/2020/06/%E7%BB%93%E6%9E%84%E5%9E%8Bc-5-%E4%BA%AB%E5%85%83%E6%A8%A1%E5%BC%8F/

上一篇 下一篇

猜你喜欢

热点阅读