κ°μ²΄μ 'νλ'μ λν λμμΈ ν¨ν΄
1. Command
2. Strategy
3. Iterator
4. Observer
12.4.1 Command ν¨ν΄ - 'λͺ λ Ή'μ μΈμ€ν΄μ€λ‘ μ·¨κΈνμ¬ μ²λ¦¬ μ‘°ν©μ μ½κ² νλ€.
μ²λ¦¬ λ΄μ©μ΄ λΉμ·ν λͺ λ Ήμ ν¨ν΄μ λ°λΌ ꡬλΆνκ±°λ μ€ννλ μ²λ¦¬κ° νμν μ μλ€. μλ₯Ό λ€μ΄ κ³μ μ λ°λΌ λ°λλ ν μΈμ¨ μ μ©μ΄ μμ μ μλ€.
Command ν¨ν΄μ 'λͺ λ Ή' μ체λ₯Ό μΈμ€ν΄μ€λ‘ μ·¨κΈν΄ μ²λ¦¬μ μ‘°ν©μ μ©μ΄νκ² νλ ν¨ν΄μ΄λ€.
- Book.java
public class Book {
private double amount;
public Book(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
- ν μΈ μ μ±
public abstract class Command {
protected Book book;
public void setBook(Book book) {
this.book = book;
}
public abstract void execute();
}
public class DiscountCommand extends Command {
@Override
public void execute() {
double amount = book.getAmount();
book.setAmount(amount * 0.9);
}
}
public class SpecialDiscountCommand extends Command {
@Override
public void execute() {
double amount = book.getAmount();
book.setAmount(amount * 0.7);
}
}
κ°κ°μ ν μΈ μ μ± μ λ°λΌμ μμλ°μ λ©μλλ₯Ό ꡬννλλ‘ νλ€.
- SampleMain.java
public abstract class SampleMain {
public static void main(String[] args) {
Book comic = new Book(5000);
Book tecnicalBook = new Book(25000);
Command discountCommand = new DiscountCommand();
Command specialDiscountCommand = new SpecialDiscountCommand();
discountCommand.setBook(comic);
discountCommand.execute();
System.out.println("ν μΈ ν κΈμ‘μ " + comic.getAmount());
specialDiscountCommand.setBook(tecnicalBook);
specialDiscountCommand.execute();
System.out.println("ν μΈ ν κΈμ‘μ " + tecnicalBook.getAmount());
}
}
ν μΈ μ²λ¦¬λ₯Ό λνλ΄λ ν΄λμ€μ λμμ μΈμ€ν΄μ€λ₯Ό μ νκ³ (setBook(?)) executeλ₯Ό μ€νν¨μΌλ‘μ¨ ν΄λΉ ν΄λμ€μ ν μΈ μ μ± μ μ μ©μν€λλ‘ νλ€.
μλ‘μ΄ ν μΈ μ μ± μ΄ μκΈ΄λ€λ©΄ Commandλ₯Ό μμλ°λ ν΄λμ€λ₯Ό ν΅ν΄ μλ‘μ΄ μ μ± μ ꡬνν΄μ£ΌκΈ°λ§ νλ©΄λλ€.
Command ν¨ν΄μ κ³΅ν΅ μΈν°νμ΄μ€μ μ²λ¦¬μ νΈμΆμ νλ μμν¬ μͺ½μμ ꡬννκ³ νλ μμν¬ μ΄μ©μμκ² Command ν΄λμ€λ₯Ό μμν ꡬ체μ μΈ μ²λ¦¬ ν¨ν΄μ ꡬννλλ‘ νλ μ¬μ©λ²μ΄ λ§λ€.
12.4.2 Strategy ν¨ν΄ - μ λ΅μ κ°λ¨ν μ νν μ μλ ꡬ쑰λ₯Ό μ 곡
Command ν¨ν΄μμλ κ³μ κ³Ό μνμ λ΄μ©μ λ°λΌ μ¬λ¬ κ°μ ν μΈ ν¨ν΄μ λλμ΄ μ¬μ©νκ³ μ¬λ¬κ°μ ν μΈ ν¨ν΄μ μ μ©ν΄μΌν λ ν¨κ³Όμ μ΄μλ€.
μ΄λ₯Ό ꡬνν μλ¨μ΄ νλ λ μλ€. λ°λ‘ Strategy ν¨ν΄μ΄λ€. 'μ λ΅'μ κ°λ¨ν λ§νλ©΄ μ²λ¦¬ μκ³ λ¦¬μ¦μ΄λ€. Strategy ν¨ν΄μ μ²λ¦¬ μκ³ λ¦¬μ¦μ 쑰건μ λ°λΌ μ½κ² μ νν μ μλλ‘ λ§λ ν¨ν΄μ΄λ€.
- Book.java λ μ Command ν¨ν΄κ³Ό λμΌ
- Strategy.java
public interface Strategy {
void discount(Book book);
}
- Strategy μΈν°νμ΄μ€λ₯Ό ꡬνν μ λ΅ ν΄λμ€λ€
public class DiscountCommand implements Strategy {
@Override
public void execute() {
double amount = book.getAmount();
book.setAmount(amount * 0.9);
}
}
public class SpecialDiscountCommand implements Strategy {
@Override
public void execute() {
double amount = book.getAmount();
book.setAmount(amount * 0.7);
}
}
- Shop.java
public class Shop {
private Strategy strategy;
public Shop(Strategy strategy) {
this.strategy = strategy;
}
public Strategy getStrategy() {
return strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public void sell(Book book) {
this.strategy.discount(book);
}
}
μ λ΅ ν¨ν΄μμλ κ°κ°μ μ λ΅μ μ΄μ©νλ ν΄λμ€κ° νμνλ€. μ΄ μν μ Shop ν΄λμ€κ° νκ³ μλ€.
public class SampleMain {
public static void main(String[] args) {
Book comic = new Book(5000);
Book technicalBook = new Book(25000);
String discountStrategy = new DiscountStrategy();
String specialDiscountStrategy = new SpecialDiscountStrategy();
Shop shop = new Shop(discountStrategy);
shop.sell(comic);
System.out.println("ν μΈ ν κΈμ‘μ " + comic.getAmount() + "μ");
Shop shop = new Shop(specialDiscountStrategy);
shop.sell(technicalBook);
System.out.println("ν μΈ ν κΈμ‘μ " + technicalBook.getAmount() + "μ");
}
}
Command ν¨ν΄μ 'λͺ λ Ή' μ체λ₯Ό κ°μ²΄νν΄ μ²λ¦¬ λμ(Book)μ λ΄λΆμ 보κ΄νκ³ μμ§λ§ Strategy ν¨ν΄μ 'μκ³ λ¦¬μ¦' μ체λ₯Ό κ°μ²΄ννκ³ μλ€λ μ μ μ°¨μ΄κ° μλ€.
Strategy ν¨ν΄μ μ¬λ¬ μκ³ λ¦¬μ¦μ λ°κΏκ°λ©° μ¬μ©ν΄μΌνλ κ²½μ°μ μ₯μ μ΄ μλ€.
'πμ½μ μ± μ 리 > μλ° λ§μ€ν°λΆ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μλ° λ§μ€ν°λΆ] 12μ₯ : λμμΈν¨ν΄ μ¦κΈ°κΈ° - ꡬ쑰 (0) | 2021.10.02 |
---|---|
[μλ° λ§μ€ν°λΆ] 12μ₯ λμμΈν¨ν΄ μ¦κΈ°κΈ° - μμ± (0) | 2021.09.22 |
[μλ° λ§μ€ν°λΆ] 10μ₯ κ°μ²΄μ§ν₯ μ¦κΈ°κΈ° (0) | 2021.09.15 |
[μλ° λ§μ€ν°λΆ] 9μ₯ λ μ§ μ²λ¦¬ 곡λ΅νκΈ° (0) | 2021.09.14 |
[μλ° λ§μ€ν°λΆ] 6μ₯ μμΈ κ³΅λ΅νκΈ° (0) | 2021.09.12 |