-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/template pattern #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
Draft
surfing2003
wants to merge
4
commits into
main
Choose a base branch
from
Feature/template_pattern
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -129,3 +129,4 @@ dmypy.json | |
| .pyre/ | ||
|
|
||
| # Project specific | ||
| .idea/ | ||
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,184 @@ | ||
| """ | ||
| 카페인 음료 클래스 | ||
| Description: | ||
| 카페인 음료 생성 템플릿을 가지고 있는 클래스 | ||
| Author: | ||
| Name: Junhyeok Yang | ||
| Email: [email protected] | ||
| """ | ||
| from abc import ABC, abstractmethod | ||
| from typing import final | ||
|
|
||
|
|
||
| class CaffeineBeverage(ABC): | ||
| """ | ||
| Description: | ||
| 카페인 음료 생성의 기본이 되는 추상 클래스 | ||
| """ | ||
|
|
||
| @final | ||
| def prepare_recipe(self) -> None: | ||
| """ | ||
| 템플릿 역할을 하는 함수로 해당 클래스에 정의된 함수 외에 | ||
| 서브 클래스에서 선언될 함수의 순서를 가지고 있는 함수 | ||
Kimdongui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
Kimdongui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| self.boil_water() | ||
| self.brew() | ||
| self.pour_in_cup() | ||
| if self.customer_wants_condiments(): | ||
| self.add_condiments() | ||
|
|
||
| @classmethod | ||
| def boil_water(cls) -> None: | ||
| """ | ||
| 카페인 음료에 필요한 물을 끓이는 함수 | ||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
| """ | ||
| print("물을 끓인다.") | ||
|
|
||
| @classmethod | ||
| def pour_in_cup(cls) -> None: | ||
| """ | ||
| 카페인 음료를 컵에 따르는 함수 | ||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
| """ | ||
| print("음료를 컵에 따른다.") | ||
|
|
||
| @classmethod | ||
| def customer_wants_condiments(cls) -> bool: | ||
| """ | ||
| hook 역할 함수 | ||
| Args: | ||
| None | ||
| Returns: | ||
| (bool): True or False | ||
| """ | ||
| return True | ||
|
|
||
| @abstractmethod | ||
| def brew(self) -> None: | ||
| """ | ||
| 서브 클래스에 따라 바뀌는 함수 | ||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def add_condiments(self) -> None: | ||
| """ | ||
| 서브 클래스에 따라 바뀌는 함수 | ||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
| """ | ||
|
|
||
|
|
||
| class Tea(CaffeineBeverage): | ||
| """ | ||
| Description: | ||
| 추상 클래스를 바탕으로 생성한 서브 클래스 | ||
| """ | ||
|
|
||
| def brew(self) -> None: | ||
| """ | ||
| 홍차(서브클래스)에 맞는 함수 생성 | ||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
| """ | ||
| print("찻잎을 우려낸다.") | ||
|
|
||
| def add_condiments(self) -> None: | ||
| """ | ||
| 홍차(서브클래스)에 맞는 함수 생성 | ||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
| """ | ||
| print("레몬을 추가한다.") | ||
|
|
||
| @classmethod | ||
| def customer_wants_condiments(cls) -> bool: | ||
| """ | ||
| 사용자의 입력을 바탕으로 참 또는 거짓을 반환하는 함수 | ||
| Args: | ||
| None | ||
| Returns: | ||
| (bool): True or False | ||
| """ | ||
| answer = input("차에 레몬을 넣을까요? (y/n)") | ||
|
|
||
| if answer == "y": | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| class Coffee(CaffeineBeverage): | ||
| """ | ||
| Description: | ||
| 추상 클래스를 바탕으로 생성한 서브 클래스 | ||
| """ | ||
|
|
||
| def brew(self) -> None: | ||
| """ | ||
| 커피(서브 클래스)에 맞는 함수 생성 | ||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
| """ | ||
| print("커피를 우려낸다.") | ||
|
|
||
| def add_condiments(self) -> None: | ||
| """ | ||
| 커피(서브 클래스)에 맞는 함수 생성 | ||
| Args: | ||
| None | ||
| Returns: | ||
| None | ||
| """ | ||
| print("설탕과 우유를 추가한다.") | ||
|
|
||
| @classmethod | ||
| def customer_wants_condiments(cls) -> bool: | ||
| """ | ||
| 사용자의 입력을 바탕으로 참 또는 거짓을 반환하는 함수 | ||
| Args: | ||
| None | ||
| Returns: | ||
| (bool): True or False | ||
| """ | ||
| answer = input("커피에 우유와 설탕을 넣을까요? (y/n)") | ||
|
|
||
| if answer == "y": | ||
| return True | ||
|
|
||
| return False | ||
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,20 @@ | ||
| """ | ||
| 템플릿 패턴 테스트 | ||
| Description: | ||
| 템플릿 패턴 테스트를 위한 코드 | ||
| Author: | ||
| Name: Junhyeok Yang | ||
| Email: [email protected] | ||
| """ | ||
| from caffeinebeverage import Coffee, Tea | ||
|
|
||
| if __name__ == "__main__": | ||
| tea = Tea() | ||
| coffee = Coffee() | ||
|
|
||
| print("홍차 준비중...") | ||
| tea.prepare_recipe() | ||
|
|
||
| print("커피 준비중...") | ||
| coffee.prepare_recipe() | ||
Kimdongui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.