Flyweight(享元)--对象结构型模式

Posted 沪马农

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flyweight(享元)--对象结构型模式相关的知识,希望对你有一定的参考价值。

1.意图

    运用共享技术有效地支持大量细粒度的对象。

2.动机

    Flyweight模式描述了如何共享对象,使得可以细粒度地使用它们,而无需高昂的代价。flyweight是一个共享对象,它可以同时在多个场景(context)中使用,并且在每个场景中flyweight都可以作为一个独立的对象---这一点与非共享对象的实例没有区别。

3.适用性

  • 一个应用程序使用了大量的对象。
  • 完全由于使用大量的对象,造成很大的存储开销。
  • 对象的大多数状态都可变为外部状态。
  • 如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。
  • 应用程序不依赖于对象标识。由于Flyweight对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。

4.结构图

    

 

5.代码实例

    

//Flyweight.h



#include <string>

class FlweightGraphicBase
{
public:
    virtual void PrintColor()=0;
protected:
    std::string m_Color;
};

class FlweightWhiteGraphic : public FlweightGraphicBase
{
public:
    FlweightWhiteGraphic();
    void PrintColor();
};

class FlweightBlackGraphic : public FlweightGraphicBase
{
public:
    FlweightBlackGraphic();
    void PrintColor();
};
//FlyweigtFactory.h



#include <string>
#include <memory>
#include <map>

class FlweightGraphicBase;

class FlyweightFactory
{
public:
    std::shared_ptr<FlweightGraphicBase> GetFlyweight(std::string sColor);
    void SetFlyweight(std::string sColor,
        std::shared_ptr<FlweightGraphicBase> pFlyweight);

private:
    std::map <std::string,std::shared_ptr<FlweightGraphicBase>> m_mapFlyweight;
};
//Flyweight.cpp

#include "Flyweight.h"
#include <iostream>

FlweightWhiteGraphic::FlweightWhiteGraphic()
{
    m_Color = "White";
}


void FlweightWhiteGraphic :: PrintColor()
{
    std::cout<<"Color is :" << m_Color << std::endl;
}


FlweightBlackGraphic::FlweightBlackGraphic()
{
    m_Color = "Black";
}


void FlweightBlackGraphic :: PrintColor()
{
    std::cout<<"Color is :" << m_Color << std::endl;
}
//FlyweightFactory.cpp


#include "FlyweightFactory.h"


std::shared_ptr<FlweightGraphicBase> FlyweightFactory::GetFlyweight(std::string sColor)
{
    auto iter = m_mapFlyweight.find(sColor);
    if(iter == m_mapFlyweight.end())
    {
        return nullptr;
    }

    return iter->second;
}

void FlyweightFactory::SetFlyweight(std::string sColor,
        std::shared_ptr<FlweightGraphicBase> pFlyweight)
{
    m_mapFlyweight[sColor] = pFlyweight;
}
//Client.cpp



#include "Flyweight.h"
#include "FlyweightFactory.h"
#include <iostream>

void Insert(std::string sColor,
    int index,std::shared_ptr<FlweightGraphicBase> pFlweightGraphic)
{
    std::cout<<"Posion " << index << " Insert: " << sColor << std::endl ;
}

int main()
{
    std::shared_ptr<FlyweightFactory> pFlyweightFactory(new FlyweightFactory);
    std::shared_ptr<FlweightWhiteGraphic> pFlweightWhiteGraphic(new FlweightWhiteGraphic);
    std::shared_ptr<FlweightBlackGraphic> pFlweightBlackGraphic(new FlweightBlackGraphic);

    pFlyweightFactory->SetFlyweight("White",pFlweightWhiteGraphic);
    pFlyweightFactory->SetFlyweight("Black",pFlweightBlackGraphic);

    auto pFlyweight = pFlyweightFactory->GetFlyweight("White");
    Insert("White",0,pFlyweight);

    pFlyweight = pFlyweightFactory->GetFlyweight("Black");
    Insert("Black",1,pFlyweight);

    pFlyweight = pFlyweightFactory->GetFlyweight("White");
    Insert("White",2,pFlyweight);

    pFlyweight = pFlyweightFactory->GetFlyweight("Black");
    Insert("Black",3,pFlyweight);

    while(1);

}

6.测试结果 

    

     实例代码中 所有插入的Graphic 共享黑白两种颜色,位置信息存储在外部。

7.效果

  • 存储节约,和以下因素有关:
  • 1.因为共享,实例总数减少的数目。
  • 2.对象内部状态的平均数目。
  • 3.外部状态是计算的还是存储的。

 

以上是关于Flyweight(享元)--对象结构型模式的主要内容,如果未能解决你的问题,请参考以下文章

Flyweight(享元)--对象结构型模式

享元模式---Flyweight

享元模式--Flyweight

23种设计模式之享元模式(FlyWeight)

享元模式flyweight

享元模式