Replies: 1 comment
-
팩토리 메서드 패턴:
abstract class Cake {
abstract void bake();
}
class ChocolateCake extends Cake {
void bake() {
System.out.println("Baking a chocolate cake!");
}
}
class StrawberryCake extends Cake {
void bake() {
System.out.println("Baking a strawberry cake!");
}
}
abstract class CakeFactory {
abstract Cake createCake();
void orderCake() {
Cake cake = createCake();
cake.bake();
}
}
class ChocolateCakeFactory extends CakeFactory {
Cake createCake() {
return new ChocolateCake();
}
}
class StrawberryCakeFactory extends CakeFactory {
Cake createCake() {
return new StrawberryCake();
}
}
// 사용 예시
CakeFactory factory = new ChocolateCakeFactory();
factory.orderCake(); // "Baking a chocolate cake!" 출력 추상 팩토리 패턴:
interface Chair {
void sit();
}
class ModernChair implements Chair {
public void sit() {
System.out.println("Sitting on a modern chair.");
}
}
class ClassicChair implements Chair {
public void sit() {
System.out.println("Sitting on a classic chair.");
}
}
interface Table {
void dine();
}
class ModernTable implements Table {
public void dine() {
System.out.println("Dining on a modern table.");
}
}
class ClassicTable implements Table {
public void dine() {
System.out.println("Dining on a classic table.");
}
}
interface FurnitureFactory {
Chair createChair();
Table createTable();
}
class ModernFurnitureFactory implements FurnitureFactory {
public Chair createChair() {
return new ModernChair();
}
public Table createTable() {
return new ModernTable();
}
}
class ClassicFurnitureFactory implements FurnitureFactory {
public Chair createChair() {
return new ClassicChair();
}
public Table createTable() {
return new ClassicTable();
}
}
// 사용 예시
FurnitureFactory factory = new ModernFurnitureFactory();
Chair chair = factory.createChair();
Table table = factory.createTable();
chair.sit(); // "Sitting on a modern chair." 출력
table.dine(); // "Dining on a modern table." 출력 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
.
Beta Was this translation helpful? Give feedback.
All reactions