[디자인 패턴] 6 데코레이터 패턴(Decorator Pattern)

류재성's avatar
May 29, 2024
[디자인 패턴] 6 데코레이터 패턴(Decorator Pattern)
 

1. 데코레이터 패턴이란?

 
💡
데코레이터 패턴(Decorator Pattern)은 객체의 기능을 동적으로 확장할 수 있는 유연한 방법이다. 이 패턴은 상속 대신 구성(composition)을 사용하여 객체에 추가적인 책임을 부여한다. 데코레이터 패턴을 사용하면, 다양한 기능을 조합하여 새로운 동작을 만들 수 있다.
 
알림을 보내는 서비스를 구현한다.
 
package ex06.notification; public class BasicNotifier { public void send(){ System.out.println("기본 알림"); } }
 
package ex06; import ex06.notification.BasicNotifier; /** * 목표 : 기능 확장(데코레이터 패턴) -> 알림서비스 개발하기 * */ public class App { public static void main(String[] args) { BasicNotifier basicNotifier = new BasicNotifier(); basicNotifier.send(); } }
 
기본 알림을 제공하는 코드는 이렇게 구현하면 된다.
 
notion image
 
하지만 알림의 경우가 많아지면 어떻게 될까?
 
package ex06; import ex06.notification.BasicNotifier; import ex06.notification.EmailNotifier; import ex06.notification.Notifier; import ex06.notification.SmsNotifier; /** * 목표 : 기능 확장(데코레이터 패턴) -> 알림서비스 개발하기 * */ public class App { public static void main(String[] args) { // BasicNotifier basicNotifier = new BasicNotifier(); // basicNotifier.send(); // 1.기본알림 +이메일알림 +문자알림 Notifier basicNotifier = new BasicNotifier(); basicNotifier.send(); Notifier emailNotifier = new EmailNotifier(); emailNotifier.send(); Notifier smsNotifier = new SmsNotifier(); smsNotifier.send(); System.out.println("__end"); // 2. 기본알림 + 문자알림 + 이메일 알림 순으로 Notifier basicNotifier2 = new BasicNotifier(); basicNotifier.send(); Notifier smsNotifier2 = new SmsNotifier(); smsNotifier.send(); Notifier emailNotifier2 = new EmailNotifier(); emailNotifier.send(); } }
 
경우의 수에 따라 객체를 만들어야 한다.
 
 
notion image
 
 
이를 해결하기 위해 데코레이터 패턴을 사용한다.
 

2. 구현하기

 
package ex06.notification; public interface Notifier { public void send(); }
 
인터페이스 Notifier 를 만든다.
 
package ex06.notification; public class BasicNotifier implements Notifier { public void send(){ System.out.println("기본 알림"); } }
 
package ex06.notification; public class EmailNotifier implements Notifier { public void send(){ System.out.println("이메일 알림"); } }
 
package ex06.notification; public class SmsNotifier implements Notifier { public void send(){ System.out.println("sms 알림"); } }
 
그리고 인터페이스를 구현한 클래스를 만든다.
 
package ex06.notification; public class SmsNotifier implements Notifier{ private Notifier notifier; public SmsNotifier(Notifier notifier) { this.notifier = notifier; } public SmsNotifier() { } public void send(){ if(notifier!=null){ notifier.send(); // 기능 확장 } System.out.println("문자 알림"); } }
 
그리고 각 클래스를 변수로 받는다. 빈생성자를 만들어 값을 입력하지 않아도 사용할 수 있게 만든다.
send 메서드가 호출될 때 변수로 받은 값을 먼저 호출한다.
 
package ex06.notification; public class EmailNotifier implements Notifier{ private Notifier notifier; public EmailNotifier(Notifier notifier) { this.notifier = notifier; } public EmailNotifier() {} public void send(){ if(notifier!=null){ notifier.send(); // 집어넣은 객체를 선 실행 } System.out.println("이메일 알림"); } }
 
package ex06; import ex06.notification.BasicNotifier; import ex06.notification.EmailNotifier; import ex06.notification.Notifier; import ex06.notification.SmsNotifier; /** * 목표 : 기능 확장(데코레이터 패턴) -> 알림서비스 개발하기 * */ public class App { public static void main(String[] args) { Notifier no2 = new SmsNotifier(new EmailNotifier()); no2.send(); } }
 
notion image
 
Share article
RSSPowered by inblog