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

相关文章

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

C++常见容器获取头元素的方法大全

《C++常见容器获取头元素的方法大全》在C++编程中,容器是存储和管理数据集合的重要工具,不同的容器提供了不同的接口来访问和操作其中的元素,获取容器的头元素(即第一个元素)是常见的操作之一,本文将详细... 目录一、std::vector二、std::list三、std::deque四、std::forwa

C++字符串提取和分割的多种方法

《C++字符串提取和分割的多种方法》在C++编程中,字符串处理是一个常见的任务,尤其是在需要从字符串中提取特定数据时,本文将详细探讨如何使用C++标准库中的工具来提取和分割字符串,并分析不同方法的适用... 目录1. 字符串提取的基本方法1.1 使用 std::istringstream 和 >> 操作符示

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程