博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 享元模式 (FlyWeight Pattern)
阅读量:20803 次
发布时间:2019-12-03

本文共 4589 字,大约阅读时间需要 15 分钟。

//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;
}

项目目录结构:

执行结果:

你可能感兴趣的文章
Android studio 53 文件下载
查看>>
android studio 54 下载进度条
查看>>
android studio 70 歌曲服务器搭建 歌曲app 完整代码(发布版)
查看>>
Android单击事件处理与监听003
查看>>
vb 读取mysql所有表名_vb怎么列举出一个mdb数据库里面所有表名?
查看>>
mysql行级锁升级_mysql innodb 行级锁升级
查看>>
c 调用mysql密码为空_C语言连MySQL - osc_srnunz15的个人空间 - OSCHINA - 中文开源技术交流社区...
查看>>
mysql怎么分组查询所有数据库_Mysql-4 分组查询与子查询
查看>>
mysql 多列union_Mysql联合查询UNION和UNION ALL的使用介绍
查看>>
mysql导数据出指定数量_mysql导出指定数据或部份数据的方法
查看>>
java thread 多线程_java用Thread方式创建多线程
查看>>
java 注解与反射_Java注解与反射直接上手
查看>>
java按钮退出_java – 如何在此程序中添加退出按钮?怎么样“清楚”?
查看>>
python土味情话_Python 将土味情话语录设置为桌面壁纸
查看>>
java ip 范围内打卡_定位地理位置PHP判断员工打卡签到经纬度是否在打卡之内
查看>>
与java线程有关的,线程多少和什么有关?大神们表示有话要说!
查看>>
php正则表达式 匹配数字,正则表达式之匹配数字范围
查看>>
php中带?错误,参考-此错误在PHP中意味着什么?
查看>>
php生成链接列表,根据URL链接和抛文本生成链接<a>标签的PHP函数
查看>>
matlab里inline定义矩阵,Matlab中的inline函数_matlab中inline函数
查看>>