Skip to content

Instantly share code, notes, and snippets.

@masaki-shimura
Last active June 19, 2021 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaki-shimura/6e7d81961ae3914a4c790847cbbebbd9 to your computer and use it in GitHub Desktop.
Save masaki-shimura/6e7d81961ae3914a4c790847cbbebbd9 to your computer and use it in GitHub Desktop.
【デザインパターン】プロキシ
/*
複合パターン のサンプルプログラム
説明
本人オブジェクトの代わりに、代理人オブジェクトが一部の仕事をこなす方式
*/
#include <iostream>
using namespace std;
class File
{
public:
virtual void draw() = 0;
string getFileName()
{
return m_fileName;
}
protected:
string m_fileName;
};
class NpcInfoFile : public File
{
public:
NpcInfoFile(string fileName)
{
this->m_fileName = fileName;
}
void draw() override
{
cout << "NpcInfo " + m_fileName + "\n";
}
};
class NpcInfoFileProxy: public File
{
public:
NpcInfoFileProxy(string _fileName)
{
this->m_fileName = _fileName;
}
void draw() override
{
getFile().draw();
}
private:
NpcInfoFile getFile()
{
if(m_pNpcInfoFile != nullptr)
{
return *m_pNpcInfoFile;
}
m_pNpcInfoFile = new NpcInfoFile(m_fileName);
return *m_pNpcInfoFile;
}
NpcInfoFile* m_pNpcInfoFile = nullptr;
};
int main(void){
// Your code here!
NpcInfoFileProxy _npcInfoFileProxy("npcInfo.txt");
string fileName = _npcInfoFileProxy.getFileName();
_npcInfoFileProxy.draw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment