Created
April 2, 2021 04:52
-
-
Save ekal901/dc43d130c85e27979fc4f09745009cad to your computer and use it in GitHub Desktop.
decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package decorator; | |
| /** | |
| * 데코레이터 역할 (피자의 값이나 이름의 반환값에 변화를 주기 위해 추가된 데코레이터) | |
| */ | |
| public class CheeseTopping extends Topping{ | |
| Pizza pizza; | |
| public CheeseTopping(Pizza pizza) { | |
| this.pizza = pizza; | |
| } | |
| public String getName() { | |
| return pizza.getName() + ", 치츠 토핑 추가"; | |
| } | |
| @Override | |
| public double cost() { | |
| return 3000 + pizza.cost(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package decorator; | |
| public class CheezePizza extends Pizza { | |
| public CheezePizza() { | |
| name = "치즈피자"; | |
| } | |
| @Override | |
| public double cost() { | |
| return 12000; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package decorator; | |
| public class CombinationPizza extends Pizza{ | |
| public CombinationPizza() { | |
| name = "콤비네이션 피자"; | |
| } | |
| @Override | |
| public double cost() { | |
| return 16000; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package decorator; | |
| /** | |
| * Decorator란 ? | |
| * 메소드 호출의 return 값에 변화를 주기위해 중간 장식자를 추가 하는 패턴 | |
| */ | |
| public class Main { | |
| public static void main(String[] args) { | |
| Pizza combination = new CombinationPizza(); | |
| System.out.println(combination.getName()); | |
| System.out.println(combination.cost()); | |
| Pizza cheese = new CombinationPizza(); | |
| cheese = new CheeseTopping(cheese); | |
| cheese = new PepperoniTopping(cheese); | |
| System.out.println(cheese.getName()); | |
| System.out.println(cheese.cost()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package decorator; | |
| public class PepperoniTopping extends Topping { | |
| Pizza pizza; | |
| public PepperoniTopping(Pizza pizza) { | |
| this.pizza = pizza; | |
| } | |
| public String getName() { | |
| return pizza.getName() + ", 페퍼로니 토핑 추가"; | |
| } | |
| @Override | |
| public double cost() { | |
| return 3500 + pizza.cost(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package decorator; | |
| /** | |
| * 피자 추상 클래스 | |
| */ | |
| public abstract class Pizza { | |
| String name = "이름을 알 수 없는 피자"; | |
| public String getName() { | |
| return name; | |
| } | |
| public abstract double cost(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package decorator; | |
| /** | |
| * 토핑 추상 클래스 | |
| * Pizza를 상속 받는 이유? 피자의 이름과 가격에 영향을 주기 위한것인듯 | |
| * 그럼 바로 다른 토핑들이 피자를 상속 받을수 있지 않는가? 피자라는 클래스는 피자 아래에 다른 피자들이 존재하고 피자 아래에는 토핑이 존재, | |
| * 개념상, 토핑 밑에 여러 종류의 토핑이 있는게 말이 맞는것 같다. 그래서 구분한것 같다고 혼자 생각했다. | |
| */ | |
| public abstract class Topping extends Pizza { | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
intellij에서 오른쪽 버튼 클릭 -> diagrams -> show diagrams 클릭하여 클래스 다이어그램 참고
<다이어그램>
피자라는 개념을 사용해서 토핑(피자의 값이나 이름의 반환 값에 변화를 주기 위해 추가된 데코레이터)을 데코레이터 개념으로 사용