Last active
June 19, 2021 11:13
-
-
Save masaki-shimura/6e7d81961ae3914a4c790847cbbebbd9 to your computer and use it in GitHub Desktop.
【デザインパターン】プロキシ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
複合パターン のサンプルプログラム | |
説明 | |
本人オブジェクトの代わりに、代理人オブジェクトが一部の仕事をこなす方式 | |
*/ | |
#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