博客
关于我
强烈建议你试试无所不能的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;
}

项目目录结构:

执行结果:

你可能感兴趣的文章
剑指offer 58. 链表中环的入口结点
查看>>
剑指offer 59. 把字符串转换成整数
查看>>
剑指offer 60. 不用加减乘除做加法
查看>>
leetcode 热题 Hot 100-3. 合并两个有序链表
查看>>
leetcode 热题 Hot 100-4. 对称二叉树
查看>>
Leetcode C++《热题 Hot 100-12》226.翻转二叉树
查看>>
Leetcode C++《热题 Hot 100-13》234.回文链表
查看>>
Leetcode C++《热题 Hot 100-14》283.移动零
查看>>
Leetcode C++《热题 Hot 100-15》437.路径总和III
查看>>
Leetcode C++《热题 Hot 100-16》448.找到所有数组中消失的数字
查看>>
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-27》17.电话号码的字母组合
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>