zoukankan      html  css  js  c++  java
  • 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.外部状态是计算的还是存储的。
  • 相关阅读:
    关于jpa example使用
    文件下载
    文件夹下的文件根据最后修改时间排序
    前端验证图片是否加载成功
    LocalDate获取当天,本月第一天,本月最后一天,今年第一天,今年最后一天
    将word文档合成一张图片输出
    easyui前端分页与layui前端分页
    Java线程池源码流程图
    hexo发布到gitee和github上及主题优化
    【JVM之美】垃圾收集算法
  • 原文地址:https://www.cnblogs.com/mgp200866130/p/5575227.html
Copyright © 2011-2022 走看看