[λμμΈ ν¨ν΄] μμ± ν¨ν΄ - ν©ν 리 λ©μλ ν¨ν΄
by rlaehddnd0422ν©ν 리 λ©μλ ν¨ν΄μ΄λ?
- "κ°μ²΄ μμ±"μ λ³λμ ν΄λμ€λ‘ λΆλ¦¬νμ¬ μμ±νκ² νλ λμμΈ ν¨ν΄μ ν©ν 리 λ©μλ ν¨ν΄ μ΄λΌκ³ ν©λλ€.
- ν΄λΌμ΄μΈνΈμμ μ§μ new ν€μλλ‘ κ°μ²΄λ₯Ό μμ±νλ κ²μ΄ μλ κ°μ²΄λ€μ λ맑μ μμ±νλ ν΄λμ€λ₯Ό λ§λ€κ³ , μ΄ ν΄λμ€λ₯Ό ν΅ν΄ κ°μ²΄λ₯Ό μμ±νλ ν¨ν΄μΌλ‘, ν΄λΉ ν΄λμ€λ "κ°μ²΄ μμ±"μ΄λΌλ λ¨ νλμ μ± μλ§ μ§λ κ².
ν©ν 리 λ©μλ ν¨ν΄μ μ₯μ
- κ°μ²΄ μμ±μ λ³λμ ν΄λμ€λ‘ λΆλ¦¬νμμΌλ, μ± μμ΄ λΆλ¦¬λκ³ κ°μ²΄μ§ν₯μ μ΄λΌκ³ λ³Ό μ μμ΅λλ€. (λ¨μΌ μ± μ μμΉ , SRP μ€μ)
- κ°μ²΄ μμ±μ νμν κ³Όμ μ ν νλ¦Ώμ²λΌ 미리 ꡬμ±ν΄λκ³ , κ°μ²΄ μμ± κ³Όμ μ λ€μνκ² μ²λ¦¬νμ¬ κ°μ²΄λ₯Ό μ μ°νκ² μ ν μ μμ΅λλ€.
- μμ μ λ«νμκ³ νμ₯μλ μ΄λ €μλ OCP μμΉμ μ§ν¬ μ μλ€.
- μΊ‘μν, μΆμνλ₯Ό ν΅ν΄ κ°μ²΄μ ꡬ체μ μΈ νμ μ κ°μΆ μ μμ.
- μΆκ°μ μΌλ‘ ν©ν 리 λ©μλλ₯Ό ν΅ν΄ κ°μ²΄μ μμ± ν κ°μ²΄λ€μ΄ 곡ν΅μΌλ‘ ν μΌμ μννλλ‘ μ§μ ν μ μμ.
ν©ν 리 λ©μλ ν¨ν΄μ λ¨μ
- κ°λ¨ν κΈ°λ₯μ μ¬μ©ν λλ³΄λ€ λ§μ ν΄λμ€λ₯Ό μ μν΄μΌ νκΈ° λλ¬Έμ μ½λλμ΄ μ¦κ°ν©λλ€.
ν©ν 리 λ©μλ ν¨ν΄ μμ 1
- Coffee : ν©ν 리 λ©μλκ° μμ±ν κ°μ²΄μ κ³΅ν΅ λΆλͺ¨ ν΄λμ€ λλ μΈν°νμ΄μ€.
- CoffeeFactory : ν©ν 리 λ©μλλ₯Ό ꡬννλ ν΄λμ€λ‘ κ°μ²΄ μμ±μ μ§μ€νλ ν΄λμ€.
- Latte, Espresso : ν©ν 리 λ©μλλ₯Ό ν΅ν΄ μμ±ν ν΄λμ€. μ΄λ»κ² μμ±νλμ§μ κ΄ν λ°©λ²μ CoffeeFactory ν΄λμ€μ ν©ν 리 λ©μλμΈ createCoffee(String name) μ μ μλ¨.
public class Coffee {
protected String name;
public String getName() {
return name;
}
}
public class Latte extends Coffee {
public Latte() {
name = "latte";
}
}
public class Espresso extends Coffee {
public Espresso() {
name = "Espresso";
}
}
abstract class CoffeeFactory {
public static Coffee createCoffee(String name) {
switch (name) {
case "Latte":
return new Latte();
case "Espresso":
return new Espresso();
default :
throw new IllegalArgumentException("INVALID COFFE NAME EXCEPTION");
}
}
}
- CoffeeFactory ν΄λμ€μμ Latte ν΄λμ€μ Espresso ν΄λμ€μ κ³΅ν΅ λΆλͺ¨ ν΄λμ€μΈ Coffee ν΄λμ€μ νμ μμ±νμ¬ λ¦¬ν΄νλλ‘ νλ λ€νμ±μ μ΄μ©νλ λ°©μμ ν΅ν΄ String nameμ κ°μ λ°λΌ μλ‘ λ€λ₯Έ ν΄λμ€λ₯Ό μμ±ν μ μμ΅λλ€.
public class Main {
public static void main(String[] args) {
Coffee coffee = CoffeeFactory.createCoffee("Latte");
System.out.println(coffee.getName()); // "latte"
System.out.println(coffee.getClass()); // Latte
}
}
ν©ν 리 λ©μλ ν¨ν΄ μμ 2
- ν©ν 리 λ©μλ ν¨ν΄μ μ¬μ©νλ©΄ OCPλ₯Ό μ μ§ν¬ μ μλ€κ³ νλλ°, μ΄μ κ΄λ ¨ν μμλ₯Ό νλ λ€μ΄λ³΄κ² μ΅λλ€.
λ€μκ³Ό κ°μ μν©μ κ°μ ν΄λ΄ μλ€.
μμ1κ³Ό λ€λ₯Έ μ
- κ°μ²΄ μμ±μ λ΄λΉνλ AnimalFactory ν΄λμ€μμ Dogμ Catμ μμ±νλ ν©ν 리 λ©μλλ₯Ό ꡬννμ§ μκ³ , AnimalFactory ν΄λμ€λ₯Ό μΆμ ν΄λμ€λ‘ λ³κ²½νκ³ , ν©ν 리 λ©μλλ₯Ό μΆμ λ©μλλ‘ λ³κ²½
- AnimalFactory ν΄λμ€λ₯Ό μμλ°λ DogFactoryλΌλ ν΄λμ€λ₯Ό μμ±νμ¬ Dog ν΄λμ€λ₯Ό μμ±νλ μν μ λ³λλ‘ λΆλ¦¬.
public interface Animal {
void sound();
}
public class Cat implements Animal {
@Override
public void sound() {
System.out.println("Meow!");
}
}
public class Dog implements Animal {
@Override
public void sound() {
System.out.println("BOW WOW");
}
}
public abstract class AnimalFactory {
public Animal newInstance() {
Animal animal = createAnimal();
animal.sound();
return animal;
}
protected abstract Animal createAnimal();
}
public class DogFactory extends AnimalFactory {
@Override
protected Animal createAnimal() {
return new Dog();
}
}
μ΄λ¬ν μν©μμ Cat ν΄λμ€λ₯Ό μμ±νλ ν©ν 리 λ©μλλ₯Ό ꡬννκ³ μΆλ€λ©΄, DogFactoryμ λ§μ°¬κ°μ§λ‘ κΈ°μ‘΄ μ½λμ λ³κ²½ μμ΄ CatFactoryλ₯Ό μΆκ°νκ³ AnimalFactory λ₯Ό μμλ°μ createAnimal() λ©μλλ₯Ό ꡬνν μ μμ΅λλ€. μ¦ κΈ°μ‘΄μ μ½λκ° λ³κ²½λμ§ μκ³ νμ₯λμ΄ OCPλ₯Ό μ§ν¬ μ μμ΅λλ€.
public class CatFactory extends AnimalFactory {
@Override
protected Animal createAnimal() {
return new Cat();
}
}
public class Main {
public static void main(String[] args) {
AnimalFactory af = new CatFactory();
AnimalFactory af2 = new DogFactory();
Animal cat = af.newInstance();
Animal dog = af2.newInstance();
cat.sound(); // MEOW !
dog.sound(); // BOW WOW
}
}
<μ°Έκ³ μλ£>
'π CS > Design-Pattern' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λμμΈ ν¨ν΄] μμ± ν¨ν΄ - μ±κΈν€ ν¨ν΄ (0) | 2024.05.31 |
---|
λΈλ‘κ·Έμ μ 보
Study Repository
rlaehddnd0422