728x90
반응형
01.프록시패턴 적용
01.1 코드 수정 없이 적용하기
GameServiceProxy 추가
public class GameServiceProxy extends GameService{
@Override
public void startGame(){
long before = System.currentTimeMillis();
super.startGame();
System.out.println(System.currentTimeMillis()-before);
}
}
- before
-
public class Client{ public static void main(String[] args){ GameService gameService = new GameService(); gameService.startGame(); } }
- after
-
public class Client{ public static void main(String[] args){ GameService gameService = new GameServiceProxy(); gameService.startGame(); } }
01.2 UML그대로 적용해보기
subject
- before
-
public class GameService{ public void startGame(){ System.out.println("이 자리에 오신 여러분을 진심으로 환영합니다."); } }
- after
- GameService 의 경우 왠만하면 이런것은 인터페이스로 구현
-
public interface GameService{ void startGame(); }
realSubject
public class DefaultGameService implement GameService{
@Override
public void startGame(){
System.out.println("이 자리에 오신 여러분을 진심으로 환영합니다.")
}
}
GameServiceProxy
- before
-
public class GameServiceProxy extends GameService{ @Override public void startGame(){ long before = System.currentTimeMillis(); super.startGame(); System.out.println(System.currentTimeMillis()-before); } }
- after
-
public class GameServiceProxy implement GameService{ private GameService gameService; public GameServiceProxy(GameService gameService){ this.gameServie = gameService; } @Override public void startGame(){ long before = System.currentTimeMillis(); gameService.startGame(); System.out.println(System.currentTimeMillis()-before); } }
Client
- before
-
public class Client{ public static void main(String[] args){ GameService gameService = new GameServiceProxy(); gameService.startGame(); } }
- after
-
public class Client{ public static void main(String[] args){ GameService gameService = new GameServiceProxy(new DefaultGameService()); gameService.startGame(); } }
- 이렇게 하면 게임 서비스는 그대로 일을 하고 다른 기능등을 추가 할 수 있음
02.다른기능을 추가하는 경우
public class GameServiceProxy implement GameService{
private GameService gameService;
@Override
public void startGame(){
long before = System.currentTimeMillis();
if(this.gameService == null){
this.gameService = new DefaultGameService();
}
gameService.startGame();
System.out.println(System.currentTimeMillis()-before);
}
}
- 위와 같이 해주는 경우 클라이언트를 이렇게 그냥 해도됨
public class Client{
public static void main(String[] args){
GameService gameService = new GameServiceProxy();
gameService.startGame();
}
}
728x90
반응형
'CS Study > 디자인패턴' 카테고리의 다른 글
22-04-17-프록시패턴-4부-자바와스프링에서찾아보는패턴 (0) | 2022.04.17 |
---|---|
22-04-17-프록시패턴-3부-장점과단점 (0) | 2022.04.17 |
22-04-17-프록시패턴-1부-패턴소개 (0) | 2022.04.17 |
22-04-12-플라이웨이트패턴-4부-자바와스프링에서찾아보는패턴 (0) | 2022.04.12 |
22-04-12-플라이웨이트패턴-3부-장점과단점 (0) | 2022.04.12 |
댓글