-
Notifications
You must be signed in to change notification settings - Fork 0
9주차 : 컴포지트 패턴 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
9주차 : 컴포지트 패턴 #10
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package composite; | ||
|
|
||
| import composite.menu.Menu; | ||
| import composite.menu.MenuComponent; | ||
| import composite.menu.MenuItem; | ||
|
|
||
| public class CompositePattern { | ||
| public static void main(String[] args) { | ||
| MenuComponent pancakeHouseMenu = new Menu("pancake house menu", "아침 메뉴"); | ||
| MenuComponent dinerMenu = new Menu("diner menu", "점심 메뉴"); | ||
| MenuComponent cafeMenu = new Menu("cafe menu", "저녁 메뉴"); | ||
| MenuComponent dessertMenu = new Menu("dessert menu", "디저트 메뉴"); | ||
| MenuComponent coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee"); | ||
|
|
||
| MenuComponent allMenu = new Menu("all menu", "전체 메뉴"); | ||
|
|
||
| allMenu.add(pancakeHouseMenu); | ||
| allMenu.add(dinerMenu); | ||
| allMenu.add(cafeMenu); | ||
| pancakeHouseMenu.add(new MenuItem( | ||
| "K&B's Pancake Breakfast", | ||
| "Pancakes with scrambled eggs and toast", | ||
| true, | ||
| 2.99)); | ||
|
|
||
|
|
||
| dinerMenu.add(new MenuItem( | ||
| "Vegetarian BLT", | ||
| "(Fakin') Bacon with lettuce & tomato on whole wheat", | ||
| true, | ||
| 2.99)); | ||
|
|
||
| allMenu.add(dessertMenu); | ||
|
|
||
| dessertMenu.add(new MenuItem( | ||
| "Apple Pie", | ||
| "Apple pie with a flakey crust, topped with vanilla icecream", | ||
| true, | ||
| 1.59)); | ||
|
|
||
| cafeMenu.add(new MenuItem( | ||
| "Veggie Burger and Air Fries", | ||
| "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", | ||
| true, | ||
| 3.99)); | ||
|
|
||
| cafeMenu.add(coffeeMenu); // cafe 메뉴 하위에 coffee 메뉴 | ||
| coffeeMenu.add(new MenuItem( | ||
| "Bagel", | ||
| "Flavors include sesame, poppyseed, cinnamon raisin, pumpkin", | ||
| false, | ||
| 0.69)); | ||
| coffeeMenu.add(new MenuItem( | ||
| "Biscotti", | ||
| "Three almond or hazelnut biscotti cookies", | ||
| true, | ||
| 0.89)); | ||
| Waitress waitress = new Waitress(allMenu); | ||
| waitress.printMenu(); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package composite; | ||
|
|
||
|
|
||
| import composite.menu.MenuComponent; | ||
|
|
||
| public class Waitress { | ||
| private final MenuComponent allMenus; // 트리의 최상위 노드 | ||
|
|
||
| public Waitress(MenuComponent allMenus) { | ||
| this.allMenus = allMenus; | ||
| } | ||
|
|
||
| public void printMenu() { | ||
| allMenus.print(); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package composite.menu; | ||
|
|
||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Menu extends MenuComponent { | ||
| private final List<MenuComponent> menuComponents = new ArrayList<>(); | ||
| private final String name; | ||
| private final String description; | ||
|
|
||
| public Menu(String name, String description) { | ||
| this.name = name; | ||
| this.description = description; | ||
| } | ||
|
|
||
| @Override | ||
| public void add(MenuComponent menuComponent) { | ||
| menuComponents.add(menuComponent); | ||
| } | ||
|
|
||
| @Override | ||
| public void remove(MenuComponent menuComponent) { | ||
| menuComponents.remove(menuComponent); | ||
| } | ||
|
|
||
| @Override | ||
| public MenuComponent getChild(int i) { | ||
| return menuComponents.get(i); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| @Override | ||
| public void print() { | ||
| System.out.print("\n" + getName()); | ||
| System.out.println(", " + getDescription()); | ||
| System.out.println("-------------------"); | ||
|
|
||
| for (MenuComponent menuComponent : menuComponents) { | ||
| menuComponent.print(); | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package composite.menu; | ||
|
|
||
| public abstract class MenuComponent { | ||
| public void add(MenuComponent menuComponent) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| public void remove(MenuComponent menuComponent) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| public MenuComponent getChild(int i) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| public String getName() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| public double getPrice() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| public boolean isVegetarian() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| public void print() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package composite.menu; | ||
|
|
||
| public class MenuItem extends MenuComponent { | ||
| private final String name; | ||
| private final String description; | ||
| private final boolean vegetarian; | ||
| private final double price; | ||
|
|
||
| public MenuItem(String name, String description, boolean vegetarian, double price) { | ||
| this.name = name; | ||
| this.description = description; | ||
| this.vegetarian = vegetarian; | ||
| this.price = price; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isVegetarian() { | ||
| return vegetarian; | ||
| } | ||
|
|
||
| @Override | ||
| public double getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| @Override | ||
| public void print() { | ||
| System.out.print(" " + getName()); | ||
| if (isVegetarian()) { | ||
| System.out.print("(v)"); | ||
| } | ||
| System.out.println(", " + getPrice()); | ||
| System.out.println(" ---" + getDescription()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
컴포지트 패턴은 어떤 원소가 복합객체이고 단일 객체인지 클라이언트에게 투명하게 보이게 하기 위해 SRP 디자인 원칙을 위배하였다.
만약 여러 역할을 서로 다른 인터페이스로 분리한다면, 복합/단일 객체가 각각 부적절한 메소드를 호출하는 상황이 일어나지 않게 된다. 하지만 이는 투명성을 떨어뜨리고, 복합/단일 객체를 분리해야하기 때문에 코드에서 분기처리문이 늘어나게 된다 (instanceof)