ν΄λΉ λ΄μ©μ μλ λΈλ‘κ·Έ ν¬μ€ν μ μ°Έμ‘°νμμ΅λλ€.
https://memostack.tistory.com/61
Design Pattern - Factory Method Pattern (ν©ν 리 λ©μλ ν¨ν΄)
Factory Method Pattern ν©ν 리 λ©μλ ν¨ν΄μ 'κ°μ²΄'μ 'κ°μ²΄ μμ± ν΄λμ€'λ₯Ό λ°λ‘ λΆλ¦¬νλ λμμΈ ν¨ν΄μ΄λ€. κ°μ²΄ μμ± ν΄λμ€κ° λ°λ‘ λΆλ¦¬λμ΄ μμ΄μ, κ°μ²΄ μμ± λ³νμ μ μ°νκ² λλΉν μ μλ€.
memostack.tistory.com
ν©ν 리 λ©μλ ν¨ν΄?
- 'κ°μ²΄'μ 'κ°μ²΄ μμ± ν΄λμ€'λ₯Ό λ°λ‘ λΆλ¦¬νλ λμμΈ ν¨ν΄
- κ°μ²΄λ₯Ό μμ±μλ₯Ό μ¬μ©νμ¬ newλ‘ νΈμΆνμ§ μκ³ , κ°μ μ μΌλ‘ κ°μ²΄ μμ± ν λ°νν΄μ£Όλ λ°©μμ΄λ€.
μμ
μꡬμ¬ν
- κ²μ μμ΄ν μ λ§λλ μμ΄ν μμ±μλ₯Ό λ§λ λ€.
- μμ΄ν μ μ 보λ DBμ μλ€.
- μμ΄ν λΆλ² μμ±μ λ§κΈ° μν΄, μμ΄ν μμ± λ‘κ·Έλ₯Ό λ¨κΈ΄λ€.
- μμ΄ν μ μμλ°μ μ¬λ¬κ°μ μΈλΆ μμ΄ν (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λ¨κ³λ‘ ꡬμ±
- DB μ‘°ν
- μμ΄ν μμ±
- μμ± λ‘κ·Έ λ¨κΉ
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
μ νμ΄μΌ'λΌκ³ μλ €μ€κ±°λ€!!
μλλ©΄..μλλΌλ©΄ λκΈλ‘ λ§ν΄μ£ΌμΈμ γ γ