Template_Method_Pattern - 8BitsCoding/RobotMentor GitHub Wiki
์ ์
ํ ํ๋ฆฟ ๋ฉ์๋ : ์ผ์ ํ ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ง ์๊ตฌ์ฌํญ์ ํ ํ๋ฆฟ ๋ฉ์๋ ํจํด์ ์ด์ฉํ์ฌ ๊ตฌํํ ์ ์๋ค.
์๊ณ ๋ฆฌ์ฆ์ ๊ตฌ์กฐ๋ฅผ ๋ฉ์๋์ ์ ์ ํ๊ณ ํ์ ํด๋์ค์์ ์๊ณ ๋ฆฌ์ฆ ๊ตฌ์กฐ์ ๋ณ๊ฒฝ์์ด ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ ์ ํ๋ ํจํด
์ธ์ ์ฐ๋?
- ๊ตฌํํ๋ ์๊ณ ๋ฆฌ์ฆ์ด ์ผ์ ํ ํ๋ก์ธ์ค๊ฐ ์๋ค.
- ๊ตฌํํ๋ ์๊ณ ๋ฆฌ์ฆ์ด ๋ณ๊ฒฝ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
์ด๋ค ๋จ๊ณ๋ก ๋๋์ด์ ์๋๊ฐ?
- ์๊ณ ๋ฆฌ์ฆ์ ์ฌ๋ฌ ๋จ๊ณ๋ก ๋๋๋ค.
- ๋๋ ์ง ์๊ณ ๋ฆฌ์ฆ์ ๋จ๊ณ๋ฅผ ๋ฉ์๋๋ก ์ ์ธํ๋ค.
- ์๊ณ ๋ฆฌ์ฆ์ ์ํํ ํ ํ๋ฆฟ ๋ฉ์๋๋ฅผ ๋ง๋ ๋ค.
- ํ์ ํด๋์ค์์ ๋๋ ์ง ๋ฉ์๋๋ค์ ๊ตฌํํ๋ค.
์์๋ก ์ค๋ช
์ญ์ ์ด๋ ต๋ค.. ์ฝ๋๋ก ๋ณด๋๊ฒ ์ ํ
์๊ตฌ์ฌํญ
- ์ ์ ๊ฒ์์ ์ ์์ ๊ตฌํํด์ฃผ์ธ์
- ์ ์ ๊ฐ ๊ฒ์์ ์์ ๋ค์์ ๊ณ ๋ คํฉ๋๋ค.
- ๋ณด์ ๊ณผ์ : ๋ณด์ ๊ด๋ จ ๋ถ๋ถ์ ์ฒ๋ฆฌ
- ์ธ์ฆ ๊ณผ์ : user name ๊ณผ password๊ฐ ์ผ์นํ๋์ง ํ์ธ
- ๊ถํ ๊ณผ์ : ์ ์์๊ฐ ์ ๋ก ํ์์ธ์ง ๋ฌด๋ฃํ์์ธ์ง ๊ฒ์ ๋ง์คํฐ ์ธ์ง ํ์ธ
- ์ ์ ๊ณผ์ : ์ ์์์๊ฒ ์ปค๋ฅ์ ์ ๋ณด๋ฅผ ๋๊ฒจ์ค๋ค.
// ๋๋ ์ง ์๊ณ ๋ฆฌ์ฆ์ ๋จ๊ณ๋ฅผ ๋ฉ์๋๋ก ์ ์ธ
class AbstGameConnectHelper {
protected: // ์๊ณ ๋ฆฌ์ฆ์ด ์ธ๋ถ์ ๋
ธ์ถ๋๋ฉด ์๋๊ธฐ์ protected(ํ์ํด๋์ค ์ฌ์ ์ธ๊ฐ๋ฅํ๊ฒํจ์ผ๋ก)๋ก ์ ์ธ(private๊ณผ ๋ค๋ฆ!)
virtual string doSecurity(string st);
virtual bool authentication(string id, string password);
virtual int authorization(string userName);
virtual string connection(string info);
public:
string requestConnection(string str);
}
string AbstGameConnectHelper::requestConnection(string str)
{
// ๋ณด์ ์์
string decodedInfo = doSecurity(str);
// id, password๋ฅผ ๋ฐ์
// id, password๋ decoedInfo์์ ํ์ฑํ๋ค๊ณ ๊ฐ์ ํ๋ค.
// ์ธ์ฆ ์์
if(!authentication(id, password)) {
// Error
}
// ๊ถํ ๊ณผ์
int i = authorization(id);
switch(i) {
case 1: // ๊ฒ์๋ง์คํฐ
break;
case 2: // ์ ์
break;
default;
break;
}
return connection(decodedInfo);
}
//๊ตฌํ์์
class DefaultGameConnectHelper : public AbstGameConnectHelper{
// ์ฌ๊ธฐ์ ์ค์ ๊ตฌํ์ ์ค์
protected:
string doSecurity(string st);
bool authentication(string id, string password);
int authorization(string userName);
string connection(string info);
}
int main() {
AbstGameConnectHelper helper = new DefaultGameConnectHelper();
helper->requestConnection("์์ด๋ ์ํธ ๋ฑ ์ ์ ์ ๋ณด");
// ์ฌ๊ธฐ์ ์ฃผ๋ชฉํด์ผ ํ ์ ์ doSecurity, authentication, authorization, connection์๋ ์ง์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅ ํ๋ค๋ ์ ์ด๋ค.
}
// ์ถ๊ฐ DefaultGameConnectHelper2๋ก ๋ณ๊ฒฝํ๋ค๋ฉด?
int main() {
AbstGameConnectHelper helper = new DefaultGameConnectHelper2();
// ๊ฐ๋จํ๊ฒ DefaultGameConnectHelper2๋ก ๋ณ๊ฒฝ
helper->requestConnection("์์ด๋ ์ํธ ๋ฑ ์ ์ ์ ๋ณด");
}