Skip to content

Instantly share code, notes, and snippets.

@hongsw
Created November 13, 2024 03:49
Show Gist options
  • Save hongsw/f9af800321d9212b27bee6083e194c58 to your computer and use it in GitHub Desktop.
Save hongsw/f9af800321d9212b27bee6083e194c58 to your computer and use it in GitHub Desktop.
추상화 클래스 예시 Recipe 클래스를 일반화하고, FreeRecipe와 PaidRecipe 클래스
class Recipe {
String title;
List<String> ingredients;
Recipe(this.title, this.ingredients);
// 공통으로 사용할 재료 목록 메서드
void getIngredientList() {
print("Ingredients for $title: ${ingredients.join(', ')}");
}
// 조리법은 각 레시피 타입에서 구체적으로 구현
void prepare() {
print("Preparing $title...");
}
}
class FreeRecipe extends Recipe {
FreeRecipe(String title, List<String> ingredients) : super(title, ingredients);
@override
void prepare() {
print("Preparing free recipe for $title...");
// 무료 레시피 전용 조리법
}
}
class PaidRecipe extends Recipe {
String videoUrl;
PaidRecipe(String title, List<String> ingredients, this.videoUrl)
: super(title, ingredients);
@override
void prepare() {
print("Preparing premium recipe for $title with exclusive steps...");
// 유료 레시피 전용 조리법
}
// 영상 재생 기능
void playVideo() {
print("Playing video for $title: $videoUrl");
}
// 재료와 조리법을 읽어주는 기능
void readAloud() {
print("Reading aloud ingredients and instructions for $title...");
}
}
void main() {
print("무료 레시피 객체 생성");
FreeRecipe freeRecipe =
FreeRecipe("Basic Salad", ["Lettuce", "Tomato", "Cucumber"]);
freeRecipe.getIngredientList();
freeRecipe.prepare();
print("\n");
print("유료 레시피 객체 생성");
PaidRecipe paidRecipe = PaidRecipe(
"Gourmet Pasta",
["Pasta", "Olive Oil", "Garlic", "Tomato Sauce", "Parmesan"],
"http://video.url/gourmet_pasta");
paidRecipe.getIngredientList();
paidRecipe.prepare();
paidRecipe.playVideo();
paidRecipe.readAloud();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment