# [λ””μžμΈ νŒ¨ν„΄] 생성 νŒ¨ν„΄ - νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄
Study Repository

[λ””μžμΈ νŒ¨ν„΄] 생성 νŒ¨ν„΄ - νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄

by rlaehddnd0422

νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄μ΄λž€?

  • "객체 생성"을 λ³„λ„μ˜ 클래슀둜 λΆ„λ¦¬ν•˜μ—¬ μƒμ„±ν•˜κ²Œ ν•˜λŠ” λ””μžμΈ νŒ¨ν„΄μ„ νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄ 이라고 ν•©λ‹ˆλ‹€.
  • ν΄λΌμ΄μ–ΈνŠΈμ—μ„œ 직접 new ν‚€μ›Œλ“œλ‘œ 객체λ₯Ό μƒμ„±ν•˜λŠ” 것이 μ•„λ‹Œ 객체듀을 도맑아 μƒμ„±ν•˜λŠ” 클래슀λ₯Ό λ§Œλ“€κ³ , 이 클래슀λ₯Ό 톡해 객체λ₯Ό μƒμ„±ν•˜λŠ” νŒ¨ν„΄μœΌλ‘œ, ν•΄λ‹Ή ν΄λž˜μŠ€λŠ” "객체 생성"μ΄λΌλŠ” 단 ν•˜λ‚˜μ˜ μ±…μž„λ§Œ μ§€λŠ” 것.

 

νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄μ˜ μž₯점

  1. 객체 생성을 λ³„λ„μ˜ 클래슀둜 λΆ„λ¦¬ν•˜μ˜€μœΌλ‹ˆ, μ±…μž„μ΄ λΆ„λ¦¬λ˜κ³  객체지ν–₯적이라고 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€. (단일 μ±…μž„ 원칙 , SRP μ€€μˆ˜)
  2. 객체 생성에 ν•„μš”ν•œ 과정을 ν…œν”Œλ¦Ώμ²˜λŸΌ 미리 ꡬ성해놓고, 객체 생성 과정을 λ‹€μ–‘ν•˜κ²Œ μ²˜λ¦¬ν•˜μ—¬ 객체λ₯Ό μœ μ—°ν•˜κ²Œ μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  3. μˆ˜μ •μ— λ‹«ν˜€μžˆκ³  ν™•μž₯μ—λŠ” μ—΄λ €μžˆλŠ” OCP 원칙을 지킬 수 μžˆλ‹€.
  4. μΊ‘μŠν™”, 좔상화λ₯Ό 톡해 객체의 ꡬ체적인 νƒ€μž…μ„ 감좜 수 있음.
  5. μΆ”κ°€μ μœΌλ‘œ νŒ©ν† λ¦¬ λ©”μ†Œλ“œλ₯Ό 톡해 객체의 생성 ν›„ 객체듀이 κ³΅ν†΅μœΌλ‘œ ν•  일을 μˆ˜ν–‰ν•˜λ„λ‘ 지정할 수 있음.

 

νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄μ˜ 단점

  1. κ°„λ‹¨ν•œ κΈ°λŠ₯을 μ‚¬μš©ν•  λ•Œλ³΄λ‹€ λ§Žμ€ 클래슀λ₯Ό μ •μ˜ν•΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— μ½”λ“œλŸ‰μ΄ μ¦κ°€ν•©λ‹ˆλ‹€.

 

νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄ μ˜ˆμ‹œ 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  
    }  
}

 

 

<참고 자료>

 

νŒ©ν† λ¦¬ νŒ¨ν„΄ - νŒ©ν† λ¦¬ λ©”μ„œλ“œ

ν—€λ“œ 퍼슀트 λ””μžμΈ νŒ¨ν„΄μ„ 읽고 μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€.λŠμŠ¨ν•œ 결합을 μ΄μš©ν•˜λŠ” 객체지ν–₯ λ””μžμΈ, 객체의 μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“œλŠ” μž‘μ—…μ΄ 항상 κ³΅κ°œλ˜μ–΄ μžˆμ–΄μ•Ό ν•˜λŠ” 것은 μ•„λ‹ˆλ©°, 였히렀 κ²°ν•©κ³Ό κ΄€λ ¨λœ

velog.io

 

Factory νŒ¨ν„΄ (2/3) - Factory Method (νŒ©ν† λ¦¬ λ©”μ„œλ“œ) νŒ¨ν„΄

1. Overview Factory νŒ¨ν„΄μ€ 객체 생성과 κ΄€λ ¨λœ λ””μžμΈ νŒ¨ν„΄μž…λ‹ˆλ‹€. 1νŽΈμ—μ„œ 봀던 Simple Factory λŠ” 객체 생성 역할을 λ‹΄λ‹Ήν•˜λ©΄μ„œ 각 ν΄λΌμ΄μ–ΈνŠΈμ—μ„œ κ΅¬ν˜„ ν΄λž˜μŠ€μ— 직접 μ˜μ‘΄ν•˜μ§€ μ•Šλ„λ‘ λΆ„λ¦¬ν–ˆμŠ΅λ‹ˆλ‹€.

bcp0109.tistory.com

 

λΈ”λ‘œκ·Έμ˜ 정보

Study Repository

rlaehddnd0422

ν™œλ™ν•˜κΈ°