22-04-17-프록시패턴-2부-패턴적용하기

22-04-17-프록시패턴-2부-패턴적용하기

목차

01.프록시패턴 적용

01.1 코드 수정 없이 적용하기

GameServiceProxy 추가

01.2 UML그대로 적용해보기

subject

realSubject

GameServiceProxy

Client

02.다른기능을 추가하는 경우

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

    public interface GameService{
    	void startGame();
    }
    • GameService 의 경우 왠만하면 이런것은 인터페이스로 구현

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();
    }
}

Written by@[KyeongMinPark]
Docker, C++, C#, Java, Golang으로 개발 모니터링운영 및 개발, 자원수집기 Beat & Exporter 개발 Gitlab Runner CI/CD & Hugo 연동과 테스트코드및 등을 공부와 개발중 ORM, TDD, BDD, DDD, DesignPattern, WebAssembly Studying

GitHub