디자인 패턴
-
어댑터 패턴 (Adapter Pattern)디자인 패턴 2022. 7. 5. 17:55
일상에서 220V 코드를 110V 콘센트에 꽂을 때 어댑터를 사용해 본 경험이 있을것이다. 어댑터 패턴도 위 경험과 매우 유사한 사례라고 할 수 있다. 어댑터 패턴 (Adapter Pattern) 구조 패턴인 어댑터 패턴을 설명하면, Client 가 사용하는 Interface 는 정해져있는데 내가 작성한 코드(Adaptee)는 해당 Interface 를 따르지 않을 때 Client 와 Adaptee 사이의 간극을 Adapter 로 매꿔서 Adaptee 를 재사용할 수 있도록 하는 패턴이다. 즉, 기존 코드를 클라이언트가 사용하는 인터페이스의 구현체로 바꿔주는 패턴이다. 어댑터 패턴 (Adapter Pattern) 적용 전 LoginHandler, UserDetails, UserDetailsService ..
-
프로토타입 (Prototype) 패턴디자인 패턴 2022. 6. 29. 22:03
기존의 객체를 응용해서 새로운 인스턴스를 만들때 사용된다. 네트워크를 거치거나 db 를 거쳐서 만들어야 하는 등의 복잡한 과정을 거쳐 인스턴스를 만드는 경우 리소스가 많이 드는데, 이미 만들어진 객체를 가지고 복제를 해서 새로운 인스턴스를 만들면 비용을 줄일 수 있다. 아래 그림과 같이 복제 기능을 갖추고 있는 기존 인스턴스를 프로토타입으로 사용해 새 인스턴스를 만들 수 있다. 프로토타입 패턴 실습 GithubRepository public class GithubRepository { private String user; private String name; public String getUser() { return user; } public void setUser(String user) { this.u..
-
추상 팩토리 (Abstract factory) 패턴디자인 패턴 2022. 6. 19. 17:30
팩토리 패턴이란? 펙토리 메서드 패턴과 추상 팩토리 패턴 두가지로 구분할 수 있으며, 모두 객체 생성을 위한 디자인 패턴이다. 간단하게 이 둘의 차이를 보자. 팩토리 메소드 패턴 객체를 생성하기 위한 인터페이스를 정의하는데, 어떤 클래스의 인스턴스를 만들지는 서브클래스에서 결정하게 만든다. 즉 팩토리 메소드 패턴을 이용하면 클래스의 인스턴스를 만드는 일을 서브클래스에게 맡기는 것. https://hyokeun0419.tistory.com/101 (해당 포스팅을 참고하자.) 추상 팩토리 패턴 인터페이스를 이용하여 서로 연관된, 또는 의존하는 객체를 구상 클래스를 지정하지 않고도 생성. 추상 팩토리 패턴이란? 서로 관련있는 여러 객체를 만들어주는 인터페이스 구체적으로 어떤 클래스의 인스턴스를 사용하는지 감출..
-
팩토리 메소드 패턴 (Factory Method Pattern)디자인 패턴 2022. 6. 14. 11:08
팩토리 메소드 패턴? 조건에 따른 객체 생성을 팩토리 클래스로 위임하여, 팩토르 클래스에서 객체를 생성하는 패턴 구체적으로 어떤 인스턴스를 만들지는 서브 클래스가 정한다. 다양한 구현체가 있고, 그 중에서 특정한 구현체를 만들 수 있는 다양한 팩토리 제공할 수 있다. 객체의 생성 코드를 별도의 클래스/메서드로 분리함으로써 객체 생성의 변화에 대비하는 데 유용하다. 먼저, 팩토리 메소드 패턴이 적용되지 않은 코드를 살펴보자. public class Client { public static void main(String[] args) { // white_ship 생성 Ship white_ship = ShipFactory.orderShip("white_ship", "hyoklee@naver.com"); Sys..
-
싱글톤 패턴 (Singleton Pattern)디자인 패턴 2022. 6. 4. 17:27
싱글톤 패턴이란? 인스턴스를 오직 하나만 제공하는 패턴 생성자가 여러차례 호출되더라도 계속해서 같은 객체를 리턴한 한다. 환경 세팅에 대한 정보 등 인스턴스가 여러개 일때 문제가 발생할 수 있는 경우가 있는데, 이 때 싱글톤 패턴을 사용하여 해결 기본 싱글톤 패턴 구현 package singleton; public class Settings { private static Settings instance; private Settings() {} public static Settings getInstance() { if (this.instance == null) { this.instance = new Settings(); } return this.instance; } } 1. 생성자를 private으로 만든..
-
프록시(proxy) 패턴디자인 패턴 2022. 5. 3. 15:24
프록시와 리얼 서브젝트가 공유하는 인터페이스가 있고, 클라이언트는 해당 인터페이스 타입으로 프록시를 사용한다. 클라이언트는 프록시를 거쳐서 리얼 서브젝트를 사용하기 때문에 프록시는 리얼 서브젝트에 대한 접근을 관리거나 부가기능을 제공하거나, 리턴값을 변경할 수도 있다. 리얼 서브젠트는 자신이 해야 할 일만 하면서(SRP) 프록시를 사용해서 부가적인 기능(접근 제한, 로깅, 트랜잭션, 등)을 제공할 때 이런 패턴을 주로 사용한다. 참고 백기선 - 더 자바, 코드를 조작하는 다양한 방법 https://www.oodesign.com/proxy-pattern.html https://en.wikipedia.org/wiki/Proxy_pattern https://en.wikipedia.org/wiki/Single_..
-
발행-구독 패턴디자인 패턴 2022. 4. 30. 20:10
발행(publisher) 구독(subscriber) 패턴은 옵저버(Observer) 패턴과 유사한 부분이 많다. 발행-구독 패턴이 무엇인지, 옵저버 패턴과 어떤 차이가 있는지 살펴보자. 발행구독 패턴 발행자와 구독자가 있고 그 사이에 브로커(=메시지 큐)가 존재하는 형태이다. 이것의 특징을 나열하면 아래와 같다. 1. 발행자 메시지의 수신자가 정해져 있지 않다. 2. 메시지는 정해진 범주에 따라서 구독을 신청한 수신자에게 전달이 된다. 3. 수신자는 발행자에 대한 정보 없이 원하는 메시지를 수신할 수 있다. 4. 메시지큐 패러다임과 마치 형제같은 관계로 대형 메시지 지향 미들웨어 솔루션의 일부라고 한다. 발행자(=pub)와 구독자(=sub) 은 아래와 같은 특징을 가진다. 1. pub 이 sub 의 선언..