C++ 享元模式 (FlyWeight Pattern)

2024-05-28 23:38

本文主要是介绍C++ 享元模式 (FlyWeight Pattern),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//UtilTool.h

//工具头文件

#pragma once
#include <iostream>
#include <string>
#include <map>
using namespace std;

//UObject.h

//根基类,本想多写一些东西的,奈何时间不允许#include "UtilTool.h"
class UObject
{
public :
    UObject(string name, string Description) :Name(name), Description(Description) { Id += 1; };
    static long long GetId();
    string GetName();
    string GetDescription();
    bool SetName(string name);
    bool SetDescription(string des);
    inline void ShowUObject()
    {
        cout <<"<--Id-->"<<Id<< "<--Name-->" << Name << "," << "<--Description-->" << Description <<endl;
    }
private:
    static long long Id;
    string Name;
    string Description;
};

//Texture.h

#include "UObject.h"
class Texture:UObject
{
public:
    Texture(int width =1024, int height =1024) :UObject(NULL, NULL),mWidth(width),mHeight(height) {};
    Texture(string name, string des, int width, int height) :UObject(name,des),mWidth(width),mHeight(height){}
    int mWidth;
    int mHeight;
    inline void ShowTexture()
    {
        UObject::ShowUObject();
        cout << "<--mWidth-->" << mWidth << "<--mHeight-->" << mHeight << endl;
    }
}

//Mesh.h

#include "UObject.h"
class Mesh:UObject
{
public:
    Mesh(string anim=NULL,string skt=NULL):UObject(NULL,NULL),Animation(anim),Skeleton(skt){};
    Mesh(string name, string des, string anim, string skeleton):UObject(name,des),Animation(anim),Skeleton(skeleton){};
    string Animation;
    string Skeleton;
    inline void ShowMesh()
    {
       UObject::ShowUObject();
       cout << "<--Animation-->" << Animation << "<--Skeleton-->" << Skeleton << endl;
    }
};

//RootTree.h

#include "UObject.h"
#include "Mesh.h"
#include "Texture.h"
#include "ResourceFactory.h"
class RootTree:UObject
{
public:
    RootTree(string name,string des):UObject(name,des)
    {
        if(name.length()!=0)
        mTreeMesh = ResourceFactory::GetMesh(name);
        mTreeTexture = ResourceFactory::GetTexture(name);
    }
    void ShowRootTree();
private:
    Mesh* mTreeMesh;
    Texture* mTreeTexture;
};

//ResourceFactory.h

#include "UtilTool.h"
class Mesh;
class Texture;
typedef pair<string, Mesh> PairMesh;
typedef pair<string, Texture> PairTexture;
class ResourceFactory
{
private:
     static map<string, Mesh> *mMapMeshs;
     static map<string, Texture>*mMapTextrues;
     ~ResourceFactory() { if(mMapMeshs!=NULL)delete mMapMeshs;if(mMapTextrues!=NULL) delete mMapTextrues; }
public:
    static Mesh* GetMesh(const string& name);
    static Texture* GetTexture(const string& name);
};

//UObject.cpp

#include "UObject.h"
long long UObject::Id = 0;
long long UObject::GetId()
{
    return Id;
}
string UObject::GetName()
{
    return Name;
}
string UObject::GetDescription()
{
    return Description;
}
bool UObject::SetName(string name)
{
    Name = name;
    return true;
}
bool UObject::SetDescription(string des)
{
    Description = des;
    return true;
}

//RootTree.cpp

#include "RootTree.h"
void RootTree::ShowRootTree()
{   
    if (!RootTree::UObject::GetName().empty())
        cout << "RootTree :" <<RootTree::UObject::GetName()<< "====================" << endl;
    if(mTreeMesh!=NULL)
    mTreeMesh->ShowMesh();
    if(mTreeTexture!=NULL)
    mTreeTexture->ShowTexture();
}

//ResourceFactory.cpp

#include "UtilTool.h"
#include "Mesh.h"
#include "Texture.h"
#include "ResourceFactory.h"
#include "UObject.h"
map<string, Mesh>* ResourceFactory::mMapMeshs = new map<string, Mesh>();
map<string, Texture>* ResourceFactory::mMapTextrues = new map<string, Texture>();
Mesh* ResourceFactory::GetMesh(const string& name)
{
    map<string, Mesh>::iterator itor;
    itor = (*mMapMeshs).find(name);
    if (itor != mMapMeshs->end())
    {
        return &itor->second;
    }
    else
    {
    Mesh* mesh = new Mesh("Mesh 0"+to_string(UObject::GetId()),"This is "+to_string(UObject::GetId())+"th Mesh","Basic Anim","root");
    mMapMeshs->insert(PairMesh(name,*mesh));
    return mesh;
    }
}
Texture* ResourceFactory::GetTexture(const string& name)
{
    map<string, Texture>::iterator itor;
    itor = mMapTextrues->find(name);
    if (itor != mMapTextrues->end())
    {
        return &itor->second;
    }
    else
    {
    Texture* texture = new Texture("Texture 0" +to_string(UObject::GetId()), "This is " + to_string(UObject::GetId()) + "th Mesh", 1024,1024);
    mMapTextrues->insert(PairTexture(name,*texture));
    return  texture;
    }

}

//test .cpp

#include "RootTree.h"
int main()
{
    string firstTree("aspen");
    string firstTreeDes("Populustremula");
    RootTree rt1(firstTree,firstTreeDes);
    rt1.ShowRootTree();
    string SecondTree("peach");
    string SecondTreeDes("Eattingquickly");
    RootTree rt2(SecondTree,SecondTreeDes);
    string ThirdTreeDes("shsodf");
    string ThirdTree("banana");
    RootTree rt3(ThirdTree,ThirdTreeDes);
    string FourthTree("aspen");
    string FourthTreeDec("dfasdfa");
    RootTree rt4(FourthTree,FourthTreeDec);
    rt2.ShowRootTree();
    rt3.ShowRootTree();
    rt4.ShowRootTree();
    system("pause");
    return 0;
}

项目目录结构:

执行结果:


这篇关于C++ 享元模式 (FlyWeight Pattern)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1012018

相关文章

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++