디자인 패턴
-
커맨드 패턴 (Command Pattern)디자인 패턴 2022. 8. 2. 23:38
커맨드 패턴 (Command Pattern) 요청을 캡슐화하여 호출자(invoker) 와 수신자 (receiver) 를 분리하는 패턴 요청을 하는 쪽과 요청을 처리하는 쪽을 디커플링 시킨다. 요청을 처리하는 방법이 바뀌더라도 호출자 코드는 변경되지 않는다. 커맨드 패턴 적용 전 불을 껏다 켰다 할 수 있는 Button 클래스가 있고 불에 해당하는 Light 클래스가 있다고 해보자. public class Light { private boolean isOn; public void on() { System.out.println("불을 켭니다."); this.isOn = true; } public void off() { System.out.println("불을 끕니다."); this.isOn = false; ..
-
책임 연쇄 패턴 (Chain of Responsibility Pattern)디자인 패턴 2022. 8. 2. 18:27
책임 연쇄 패턴 (Chain of Responsibility Pattern) 요청을 보내는 쪽(sender)과 요청을 처리하는(receiver) 쪽을 분리하는 패턴 핸들러 체인을 사용해서 요청을 처리한다. 클라이언트로부터의 요청을 처리할 수 있는 처리 객체를 Chain 으로 만들어 결합을 느슨하게 하기 위해 만들어진 패턴. 일반적으로 요청을 처리할 수 있는 객체를 찾을때 까지 집한 안에서 요청을 전달한다. 책임 연쇄 패턴 적용 전 클라이언트가 특정 핸들러를 사용할 때, 이 클라이언트가 해당 핸들러를 사용할 수 있는 사용자 인지 인증해야 하는 로직이 있다고 가정해보자. 클라이언트 코드는 아래와 같다. public class Client { public static void main(String[] args..
-
프록시 패턴 (Proxy Pattern)디자인 패턴 2022. 7. 22. 22:51
프록시 패턴 (Proxy Pattern) 특정 객체에 대한 접근을 제어하거나 기능을 추가할 수 있는 패턴 특정 객체에 접근하기 전에 프록시 객체를 먼저 지난 후 접근하게 된다. 초기화 지연, 접근 제어, 로깅, 캐싱 등 다양하게 응용하여 사용할 수 있다. 프록시 패턴 적용 전 Client 가 startGame 이 실행되고 종료되기까지 얼마나 시간이 걸리는지 알기 위해서는 Client 코드의 main 시작 부분과 startGame 마지막 부분에 시간을 재면 된다. public class Client { public static void main(String[] args) { GameService gameService = new GameService(); gameService.startGame(); } } ..
-
플라이웨이트 패턴 (Flyweight Pattern)디자인 패턴 2022. 7. 22. 21:18
플라이웨이트 패턴 (Flyweight Pattern) 객체를 가볍게 만들어 메모리 사용을 줄이는 패턴 자주 변화는 속성과 변하지 않는 속성을 분리하고 변하지 않는 속성을 캐시하여 재사용해 메모리 사용을 줄일 수 있다. 플라이웨이트 패턴 적용 전 편집기를 만든다 가정하고 글자들을 관리하기 위해 Character 클래스를 만들었다. public class Character { char value; String color; int fontSize; String fontFamily; public Character(char value, String color,String fontFamily, int fontSize) { this.value = value; this.color = color; this.fontSiz..
-
퍼사드 패턴 (Facade Pattern)디자인 패턴 2022. 7. 19. 22:08
퍼사드 패턴 (Facade Pattern) 퍼사드 패턴은 복잡한 서브 시스템 의존성을 최소화 하는 방법이다. 클라이언트가 사용해야 하는 복잡한 서브 시스템 의존성을 간단한 인터페이스로 추상화 할 수 있다. 클라이언트가 서브 시스템 클래스나 메서드를 직접 사용하는 것이 아니라 중간에서 복잡한 서브 시스템을 감추고 우리가 사용하는 기능에 대해서만 인터페이스 또는 클래스로 압축시켜 사용하게 해준다. [참고] 퍼사드란 원래 건물의 입구 쪽을 바라보는 건물의 전경 이라는 뜻이며, 건물의 외벽을 본다고 건물의 안이 어떤지는 알 수 없다. 퍼사드 패턴 적용 전 자바는 이메일 관련 라이브러리가 제공되는데, 클라이언트가 라이브러리에 대해 지나치게 많이 알아야 한다 라는 문제가 발생한다. 아래와 같이 클라이언트가 많은것..
-
데코레이터 패턴(Decorator Pattern)디자인 패턴 2022. 7. 19. 18:18
데코레이터 패턴(Decorator Pattern) 기존 코드를 변경하지 않고 부가기능을 추가하는 패턴 상속이 아닌 위임을 사용해서 보다 유연하게 (런타임에) 부가 기능을 추가하는 것도 가능하다. 데코레이터 패턴 적용 전 웹 사이트에서 사용자들이 작성한 Comment 를 관리하는 어플리케이션이 있다 가정해보자. Comment 를 관리하는 CommentService 는 아래와 같으며 addComment 는 Comment 를 출력하는 메서드이다. public class CommentService { public void addComment(String comment) { System.out.println(comment); } } 만약, "..." 과 특수기호는 관리하고 싶지 않다면 그에 맞는 기능을 추가해야..
-
컴포짓 패턴 (Composite Pattern)디자인 패턴 2022. 7. 12. 23:05
컴포짓 패턴 (Composite Pattern) 그룹 전체와 개별 객체를 동일하게 처리할 수 있는 패턴. 클라이언트 입장에서는 ‘전체’나 ‘부분’이나 모두 동일한 컴포넌트로 인식할 수는 계층 구조 를 만든다. (Part-Whole Hierarchy) 객체들의 관계를 트리 구조로 표현하며 사용자가 단일 객체와 복합 객체를 모두 동일하게 다룰 수 있도록 구조화한 패턴이다. 즉, 클라이언트 입장에선 전체 부분인지, 전체의 맨 마지막 부분인지, 특정 부분인지 모르고 인터페이스를 통해 사용하게끔 한다. 왜 필요한가? 데이터를 다루다보면 계층형 트리 자료구조로 저장되고 이를 다루게되는 경우가 종종 생긴다. 이 때 composite 패턴을 사용하면 클라이언트 측에서 모든 데이터를 모르더라도 복잡한 트리구조를 쉽게 다..
-
브릿지 패턴 (Bridge Pattern)디자인 패턴 2022. 7. 12. 14:04
브릿지 패턴 (Bridge Pattern) 추상적인 것과 구체적인 것을 구분하는 패턴 하나의 계층 구조일 때 보다 각기 나누었을 때 독립적인 계층 구조로 발전시킬 수 있다. 하나의 거대한 클래스나 기능부/구현부 로만 분리되어 있던 어플리케이션을 기능부/구현부 각각 독립적인 계층화 구조로 나누는 설계방식을 말한다. 브릿지 패턴 (Bridge Pattern) 적용 전 Champion 인터페이스는 모든 캐릭터(챔피언)가 사용할 수 있는 기능을 인터페이스로 정의해놨다. public interface Champion { void move(); void skillQ(); void skillW(); void skillE(); void skillR(); } 아리 라는 캐릭터는 특정 Skin 을 선택할 수 있는데, 이 ..