ํด๋น ๋ด์ฉ์ ์๋ ๋ธ๋ก๊ทธ ํฌ์คํ ์ ์ฐธ์กฐํ์์ต๋๋ค.
https://memostack.tistory.com/60
Design Pattern - Template Method Pattern (ํ ํ๋ฆฟ ๋ฉ์๋ ํจํด)
Template Method Pattern ํ ํ๋ฆฟ ๋ฉ์๋ ํจํด์ ์ผ์ ํ ๊ตฌ์กฐ์ ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ง ๊ฒฝ์ฐ, ์ฌ์ฉํ๊ธฐ ์ข์ ๋์์ธ ํจํด ์ฝ๋์ ์ค๋ณต์ ์ค์ผ ์ ์๊ณ ์ ์ง๋ณด์ํ๊ธฐ์๋ ํธ๋ฆฌํจ. ์ผ์ ํ ๊ตฌ์กฐ์ ํ๋ก์ธ์ค? ํน์
memostack.tistory.com
ํ ํ๋ฆฟ ๋ฉ์๋ ํจํด?
- ์ผ์ ํ ๊ตฌ์กฐ์ ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ง ๊ฒฝ์ฐ, ์ฌ์ฉํ๊ธฐ ์ข์ ๋์์ธ ํจํด
- ์ฝ๋์ ์ค๋ณต์ ์ค์ผ ์ ์๊ณ , ์ ์ง๋ณด์ํ๊ธฐ ํธ๋ฆฌํจ.
์ผ์ ํ ๊ตฌ์กฐ?
ํน์ ์๊ณ ๋ฆฌ์ฆ์ด ํญ์ A -> B -> C ์์ผ๋ก ์ํํ๋ค๋ฉด, ๊ฐ ์์ ์ ๋ชจ์ ํ๋์ ๋ฉ์๋๋ก ๊ตฌํ
์์
์๊ตฌ์ฌํญ
- ๊ณ์ ๋ก๊ทธ์ธ์ ํ๋ค.
- ๋ก๊ทธ์ธ์ ๋์ ์์๋ ์๋์ ๊ฐ๋ค.
- ์ฌ์ฉ์์ id, pw ๋ณตํธํ
- ์ ํจํ id์ pw์ธ์ง ํ์ธ
- ๊ถํ ํ์ธ
- ์ ์
Template Method๋ก ConnectionManager(๋ถ๋ชจ ํด๋์ค)
import java.util.Map;
public abstract class ConnectionManager {
protected abstract Map<String, String> decodedInfo(Map<String, String> rawUserInfo);
protected abstract boolean authentication(String username, String password);
protected abstract int authorization(String username);
protected abstract Object getConnection(String username, int authority);
public Object requestConnection(Map<String, String> rawUserInfo) {
// 1. ์ํธํ ๋ ์ ์ ์ ๋ณด ๋ณตํธํ
final Map<String, String> userInfo = decodedInfo(rawUserInfo);
final String username = userInfo.get("username");
final String password = userInfo.get("password");
// 2. ์์ด๋์ ํจ์ค์๋ ์ธ์ฆ
if(!authentication(username,password)) {
throw new RuntimeException("์ธ์ฆ ์คํจ!");
}
// 3. ๊ถํ ํ์ธ
final int authority = authorization(username);
// 4. ์๋น์ค ์ ์
return getConnection(username,authority);
}
}
- ๊ณ์ ์ ์์ ์ํ 4๋จ๊ณ๋ฅผ ์ถ์ ๋ฉ์๋(abstract)๋ก ๋ง๋ ๋ค.
- 4๋จ๊ณ์ ๊ฐ ์์ ์ ์์ ๋ฐ์ ๊ฐ์ฒด์์๋ง ๊ตฌํํ ์ ์๋๋ก protected ๊ถํ์ ์ฌ์ฉ
- requestConnection() ๋ฉ์๋๋ฅผ ํตํด ์ถ์ํ ํด๋์ 4๊ฐ์ง ๋ฉ์๋๋ฅผ ์ฐจ๋ก๋๋ก ์ํ
DefaultConnection(์์ ํด๋์ค)
package ํ
ํ๋ฆฟ๋ฉ์๋;
import java.util.Map;
public class DefaultConnection extends ConnectionManager {
@Override
protected Map<String, String> decodedInfo(Map<String, String> rawUserInfo) {
System.out.println("์ ์ ์ ๋ณด ๋ณตํธํ");
final Map<String, String> encodedUserInfo = rawUserInfo;
return encodedUserInfo;
}
@Override
protected boolean authentication(String username, String password) {
System.out.println("์ ์ ์ ๋ณด ์ธ์ฆ ํ์ธ");
if("yang".equals(username)) return true;
return false;
}
@Override
protected int authorization(String username) {
System.out.println("๊ถํ ์ ๋ณด ํ์ธ");
int authority = 0;
if("yang".equals(username)) {
authority = 1;
}
switch(authority) {
case 0 : System.out.println("- ๊ถํ : ์ผ๋ฐ ์ฌ์ฉ์"); break;
case 1 : System.out.println("- ๊ถํ : ๊ด๋ฆฌ์"); break;
case 2 : System.out.println("- ๊ถํ : ์ํผ ๊ด๋ฆฌ์"); break;
case 3 : System.out.println("- ๊ถํ : ์ฌ๋ฐ๋ฅด์ง ์์ ๊ถํ"); break;
}
return authority;
}
@Override
protected Object getConnection(String username, int authority) {
System.out.println(username + " ์ ์!");
final Object connectionObj = new Object();
return connectionObj;
}
}
- ConnectionManager(abstract)์ 4๊ฐ์ง ์ถ์ ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํ์ฌ ๊ตฌ์ฒด์ ์ผ๋ก ๊ตฌํ
Main
package ํ
ํ๋ฆฟ๋ฉ์๋;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
final ConnectionManager cm = new DefaultConnection();
final Map<String,String> userInfo = new HashMap<>();
userInfo.put("username", "yang");
userInfo.put("password", "yangkongmi");
final Object connectionObj = cm.requestConnection(userInfo);
}
}
- requestConnection() ๋ฉ์๋ ํ๋๋ฅผ ํธ์ถํ๋ฉด 4๋จ๊ณ๋ฅผ ๋ชจ๋ ์ํํจ.
์ ์ ์ ๋ณด ๋ณตํธํ
์ ์ ์ ๋ณด ์ธ์ฆ ํ์ธ
๊ถํ ์ ๋ณด ํ์ธ
- ๊ถํ : ๊ด๋ฆฌ์
yang ์ ์!