April 17, 2022
01.프록시패턴 적용
01.1 코드 수정 없이 적용하기
GameServiceProxy 추가
01.2 UML그대로 적용해보기
subject
realSubject
GameServiceProxy
Client
02.다른기능을 추가하는 경우
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();
}
}
before
public class GameService{
public void startGame(){
System.out.println("이 자리에 오신 여러분을 진심으로 환영합니다.");
}
}
after
public interface GameService{
void startGame();
}
public class DefaultGameService implement GameService{
@Override
public void startGame(){
System.out.println("이 자리에 오신 여러분을 진심으로 환영합니다.")
}
}
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);
}
}
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();
}
}
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();
}
}