GiantStepDEV

ν•΄λ‹Ή λ‚΄μš©μ€ μ•„λž˜ λΈ”λ‘œκ·Έ ν¬μŠ€νŒ…μ„ μ°Έμ‘°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

https://memostack.tistory.com/61

 

Design Pattern - Factory Method Pattern (νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄)

Factory Method Pattern νŒ©ν† λ¦¬ λ©”μ†Œλ“œ νŒ¨ν„΄μ€ '객체'와 '객체 생성 클래슀'λ₯Ό λ”°λ‘œ λΆ„λ¦¬ν•˜λŠ” λ””μžμΈ νŒ¨ν„΄μ΄λ‹€. 객체 생성 ν΄λž˜μŠ€κ°€ λ”°λ‘œ λΆ„λ¦¬λ˜μ–΄ μžˆμ–΄μ„œ, 객체 생성 변화에 μœ μ—°ν•˜κ²Œ λŒ€λΉ„ν•  수 μžˆλ‹€.

memostack.tistory.com

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

  • '객체'와 '객체 생성 클래슀'λ₯Ό λ”°λ‘œ λΆ„λ¦¬ν•˜λŠ” λ””μžμΈ νŒ¨ν„΄
  • 객체λ₯Ό μƒμ„±μžλ₯Ό μ‚¬μš©ν•˜μ—¬ new둜 ν˜ΈμΆœν•˜μ§€ μ•Šκ³ , κ°„μ ‘μ μœΌλ‘œ 객체 생성 ν›„ λ°˜ν™˜ν•΄μ£ΌλŠ” 방식이닀.

예제

μš”κ΅¬μ‚¬ν•­

- κ²Œμž„ μ•„μ΄ν…œμ„ λ§Œλ“œλŠ” μ•„μ΄ν…œ μƒμ„±μžλ₯Ό λ§Œλ“ λ‹€.

  1. μ•„μ΄ν…œμ˜ μ •λ³΄λŠ” DB에 μžˆλ‹€.
  2. μ•„μ΄ν…œ λΆˆλ²• 생성을 막기 μœ„ν•΄, μ•„μ΄ν…œ 생성 둜그λ₯Ό 남긴닀.

- μ•„μ΄ν…œμ„ 상속받아 μ—¬λŸ¬κ°œμ˜ μ„ΈλΆ€ μ•„μ΄ν…œ(HPν¬μ…˜, κ²€, 총 λ“±λ“±)을 κ΅¬ν˜„ν•œλ‹€.

ItemCreator(λΆ€λͺ¨ 클래슀)

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public abstract class ItemCreator {
    // DBμ—μ„œ μ•„μ΄ν…œ 정보 쑰회
    protected abstract Map<String,String> getItemInfo();
    protected abstract Item createItem(Map<String,String> itemInfo);
    private void log(String itemName) {
        System.out.println(new Date() + " - " + itemName + " - μ•„μ΄ν…œ μƒμ„±λμŠ΅λ‹ˆλ‹€.");
    }

    public Item create() {
        // ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ νŒ¨ν„΄ 이용
        // 1. DBμ—μ„œ μ•„μ΄ν…œ 정보λ₯Ό κ°€μ Έμ˜¨λ‹€.
        final Map<String,String> itemInfo = getItemInfo();
        // 2. μ•„μ΄ν…œ 생성
        final Item item = createItem(itemInfo);
        // 3. μ•„μ΄ν…œ 생성 둜그λ₯Ό 남긴닀.
        log(itemInfo.get("name"));

        return item;
    }
}
  • μ•„μ΄ν…œ 생성은 3λ‹¨κ³„λ‘œ ꡬ성
  1. DB 쑰회
  2. μ•„μ΄ν…œ 생성
  3. 생성 둜그 남김

HpPotionCreator(μžμ‹ 클래슀)

- hpν¬μ…˜μ„ μƒμ„±ν•˜λŠ” 객체 생성

import java.util.HashMap;
import java.util.Map;

public class HpPotionCreator extends ItemCreator {
    @Override
    protected Map<String, String> getItemInfo() {
        System.out.println("DBμ—μ„œ μ•„μ΄ν…œ 정보λ₯Ό κ°€μ Έμ˜¨λ‹€.");

        final Map<String,String> itemInfo = new HashMap<>();
        itemInfo.put("name", "체λ ₯ νšŒλ³΅μ•½");
        itemInfo.put("category", "potion");

        return itemInfo;
    }

    @Override
    protected Item createItem(Map<String, String> itemInfo) {
        System.out.println("HP λ¬Όμ•½ 생성!!");
        return new HpPotion(itemInfo);
    }
}
  • DBμ—μ„œ κ°€μ Έμ™”λ‹€κ³  κ°€μ •..

SwordCreator(μžμ‹ 클래슀)

- 'κ²€'을 μƒμ„±ν•˜λŠ” 객체 생성

import java.util.HashMap;
import java.util.Map;

public class SwordCreator extends ItemCreator{
    @Override
    protected Map<String, String> getItemInfo() {
        System.out.println("DBμ—μ„œ μ•„μ΄ν…œ 정보λ₯Ό κ°€μ Έμ˜¨λ‹€.");

        final Map<String,String> itemInfo = new HashMap<>();
        itemInfo.put("name", "μž₯κ²€");
        itemInfo.put("category", "weapon");

        return itemInfo;
    }

    @Override
    protected Item createItem(Map<String, String> itemInfo) {
        System.out.println("μž₯κ²€ μž₯μ°©!!");
        return new Sword(itemInfo);
    }
}

Item(μΈν„°νŽ˜μ΄μŠ€)

public interface Item {
    void use();
}

HpPotion(μ„ΈλΆ€ μ•„μ΄ν…œ κ΅¬ν˜„)

import java.util.Map;

public class HpPotion implements Item{
    private String name;
    private String category;

    public HpPotion(Map<String,String> itemInfo) {
        this.name = itemInfo.get("name");
        this.category = itemInfo.get("category");
    }

    public String getName() {
        return name;
    }

    public String getCategory() {
        return category;
    }

    @Override
    public void use() {
        System.out.println("'" + getName() + "' μ‚¬μš©ν•˜μ—¬ 체λ ₯ 회볡.");
    }
}

Sword(μ„ΈλΆ€ μ•„μ΄ν…œ κ΅¬ν˜„)

import java.util.Map;

public class Sword implements Item{
    private String name;
    private String category;

    public Sword(Map<String,String> itemInfo) {
        this.name = itemInfo.get("name");
        this.category = itemInfo.get("category");
    }

    public String getName() {
        return name;
    }

    public String getCategory() {
        return category;
    }

    @Override
    public void use() {
        System.out.println("'" + getName() + "' μž₯μ°©ν–ˆμŠ΅λ‹ˆλ‹€.");
    }
}

Main

public class Main {
    public static void main(String[] args) {
        final ItemCreator hpc = new HpPotionCreator();
        final HpPotion hpPotion = (HpPotion) hpc.create();
        hpPotion.use();
        System.out.println("================================");
        final ItemCreator k = new SwordCreator();
        final Sword sword = (Sword) k.create();
        sword.use();
    }
}
DBμ—μ„œ μ•„μ΄ν…œ 정보λ₯Ό κ°€μ Έμ˜¨λ‹€.
HP λ¬Όμ•½ 생성!!
Tue Jan 24 18:10:01 KST 2023 - 체λ ₯ νšŒλ³΅μ•½ - μ•„μ΄ν…œ μƒμ„±λμŠ΅λ‹ˆλ‹€.
'체λ ₯ νšŒλ³΅μ•½' μ‚¬μš©ν•˜μ—¬ 체λ ₯ 회볡.
================================
DBμ—μ„œ μ•„μ΄ν…œ 정보λ₯Ό κ°€μ Έμ˜¨λ‹€.
μž₯κ²€ μž₯μ°©!!
Tue Jan 24 18:10:01 KST 2023 - μž₯κ²€ - μ•„μ΄ν…œ μƒμ„±λμŠ΅λ‹ˆλ‹€.
'μž₯κ²€' μž₯μ°©ν–ˆμŠ΅λ‹ˆλ‹€.

ν•΄λ‹Ή λΈ”λ‘œκ·Έ ν¬μŠ€νŒ…μ„ 보며 κ±°μ§„ 이해가 κ°”λŠ”λ° ν•œ κ°€μ§€ 이해가 μ•ˆκ°€λŠ” 뢀뢄이 μžˆμ—ˆλ‹€.

Mainν΄λž˜μŠ€μ—μ„œ 객체λ₯Ό 생성할 λ•Œ

final HpPotion hpPotion = (HpPotion) hpc.create();

new도 μ•„λ‹ˆκ³ , λ©”μ†Œλ“œλ„ μ•„λ‹ˆκ³  (HpPotion)으둜 λͺ…μ‹œν•˜λŠ” μ΄μœ κ°€ 무엇인지 도톡 μ˜λ¬Έμ΄μ—ˆλ‹€.

λ‹Ήμž₯ λ¬Όμ–΄λ³Ό 곳이 λ§ˆλ•…μΉ˜κ°€ μ•Šμ•„ μž μ‹œ κ³ μ „μ€‘μ΄μ—ˆλŠ”λ° chatGPT에 물어보고 의문이 해결됐닀.


The error was resolved because you added a typecast operator "()" on both sides of the HpPotion class name. This tells the compiler to treat the object returned by the create() method as an instance of the HpPotion class.
The create() method in the ItemCreator class likely returns an object of type "Object" or a superclass of HpPotion, so it needs to be casted to HpPotion to be used as HpPotion.
When you wrote "final HpPotion hpPotion = HpPotion hpc.create();" without casting the returned object, the compiler was not able to determine the type of the object returned by the create() method and hence threw an error. By adding the typecast operator, you explicitly told the compiler that the object returned by the create() method is of type HpPotion, and it can be assigned to the hpPotion variable.
It's a way of telling the compiler that you know what you're doing and that you are sure the object returned is of the type you are casting it to.

λŒ€λž΅ μ΄ν•΄ν•œ λ°”λ‘œλŠ”...

()λŠ” castingν•œ 것이고 casting이 ν•„μš”ν•œ μ΄μœ λŠ” create() λ©”μ†Œλ“œμ— μ˜ν•΄ λ°˜ν™˜λœ 값을 HpPotion 클래슀의 μΈμŠ€ν„΄μŠ€λ‘œ μ²˜λ¦¬ν•˜λ„λ‘ μ»΄νŒŒμΌλŸ¬μ—κ²Œ λͺ…μ‹œν•˜κΈ° μœ„ν•΄μ„œμ΄λ‹€.

 

create() λ©”μ†Œλ“œλŠ” Item μœ ν˜•μ˜ item을 λ°˜ν™˜ν•œλ‹€.

ν—ˆλ‚˜ hpcκ°μ²΄λŠ” HpPotionCreatorμ—μ„œ μƒμ„±ν•œ 것이기 λ•Œλ¬Έμ— Item에 μ ‘κ·Όν•  μˆ˜κ°€ μ—†λ‹€. λ”°λΌμ„œ, μ»΄νŒŒμΌλŸ¬λŠ” λ°˜ν™˜λœ κ°’μ˜ μœ ν˜•μ„ κ²°μ •ν•  수 μ—†μ–΄ ν˜•λ³€ν™˜μ„ μ•ˆν•˜λ©΄ 였λ₯˜κ°€ λ°œμƒν•˜λŠ” 것이닀.

이에 ()을 μΆ”κ°€ν•¨μœΌλ‘œμ¨ create() λ©”μ†Œλ“œμ—μ„œ λ°˜ν™˜λœ κ°œμ²΄λŠ” HpPotion μœ ν˜•μ΄λ©°, hpPotion λ³€μˆ˜μ— ν• λ‹Ήν•  수 μžˆλ‹€κ³  μ»΄νŒŒμΌλŸ¬μ— λͺ…μ‹œν•œ 것이닀.

 

μ •λ¦¬ν•˜λ©΄ (HpPotion)은 κ·Έλƒ₯ μ»΄νŒŒμΌλŸ¬ν•œν…Œ 'create()의 λ°˜ν™˜νƒ€μž…μ€ HpPotion μœ ν˜•μ΄μ•Ό'라고 μ•Œλ €μ€€κ±°λ‹€!!

μ•„λ‹ˆλ©΄..μ•„λ‹ˆλΌλ©΄ λŒ“κΈ€λ‘œ λ§ν•΄μ£Όμ„Έμš” γ…œγ…œ

profile

GiantStepDEV

@kongmi

ν¬μŠ€νŒ…μ΄ μ’‹μ•˜λ‹€λ©΄ "μ’‹μ•„μš”β€οΈ" λ˜λŠ” "κ΅¬λ…πŸ‘πŸ»" ν•΄μ£Όμ„Έμš”!