22-04-17-프록시패턴-2부-패턴적용하기
본문 바로가기
CS Study/디자인패턴

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

by KyeongMin 2022. 4. 17.
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();
    }
}

https://github.com/3DPIT/3dpit.github.io/blob/master/_posts/DesignPattern/22-04-17-%ED%94%84%EB%A1%9D%EC%8B%9C%ED%8C%A8%ED%84%B4-2%EB%B6%80-%ED%8C%A8%ED%84%B4%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0.md

 

GitHub - 3DPIT/3dpit.github.io: A minimal, responsive, and powerful Jekyll theme for presenting professional writing

A minimal, responsive, and powerful Jekyll theme for presenting professional writing - GitHub - 3DPIT/3dpit.github.io: A minimal, responsive, and powerful Jekyll theme for presenting professional w...

github.com

 

728x90
반응형

댓글