๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

IT

ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด(Factory Method Pattern)

728x90

๐Ÿ“Œ ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์ด๋ž€ ?

  • ์ž๋ฐ” ๋””์ž์ธํŒจํ„ด ์ค‘ ํ•˜๋‚˜๋กœ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์„ ๋Œ€์‹  ์ˆ˜ํ–‰ํ•ด์ฃผ๋Š” ๊ณต์žฅ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ์ฒด์˜ ์ƒ์„ฑ์€ ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ๋Œ€์‹ ํ•œ๋‹ค.
  • ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ๊ฐ„์ ‘์ ์œผ๋กœ ์ƒ์„ฑํ•œ ํ›„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ˜•์‹

ํŠน์ง•)

  • Factory ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ
  • ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ข…๋ฅ˜์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • abstract, interface์„ ํ™œ์šฉํ•ด ๊ฐ์ฒด ์ƒ์„ฑ์„ ์„œ๋ธŒ ํด๋ž˜์Šค์— ์œ„์ž„ => ์„œ๋ธŒ ํด๋ž˜์Šค๋Š” ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ

ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ

Product :

  • ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ๊ฐ์ฒด์˜ ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜

ConcreteProduct :

  • Product ํด๋ž˜์Šค์— ์ •์˜๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‹ค์ œ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค

Creator :

  • Product ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•˜๋Š” ํด๋ž˜์Šค
  • ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ตฌํ˜„, ConcreteProduct ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • Product ๊ฐ์ฒด์˜ ์ƒ์„ฑ์„ ์œ„ํ•ด ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

ConcreteCreator :

  • ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ConcreteProduct์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜

๐Ÿ“Œ ์˜ˆ์ œ ( Creator๋ฅผ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์ •์˜, ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋Š” abstract๋กœ ์„ ์–ธ )

1.  Product

: ์‡ผํ•‘ํ•  ๋•Œ ๊ฒ€์ƒ‰, ๋‹ด๊ธฐ , ์ฃผ๋ฌธํ•˜๊ธฐ , ํ™•์ธํ•˜๋Š” ๊ฒƒ์€ ๊ณตํ†ต๊ณผ์ •์ด๊ธฐ์— ์ธํ„ฐํŽ˜์ด์Šค์— ์ž‘์„ฑ

//Shopping.java

public interface Shopping {
    public void find(); //๋ฌผ๊ฑด์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค
    public void cart(); //์นดํŠธ์— ๋‹ด๋Š”๋‹ค.
    public void order(); //์นดํŠธ์— ๋‹ด์€ ๋ฌผ๊ฑด์„ ์ฃผ๋ฌธํ•œ๋‹ค.
    public void check(); //์ฃผ๋ฌธ์ด ์ œ๋Œ€๋กœ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
}

2.  Creator

: ์œ„์˜ ํ–‰์œ„๋“ค์€ ์‡ผํ•‘๋ชฐ์—์„œ ์ „๋ถ€ ์ด๋ค„์ง„๋‹ค.

//ShoppingMall.java

public abstract class ShoppingMall {
    
    public Shopping shopping(String category) {
    	Shopping shopping = selectCategory(category);    //factory method ์‚ฌ์šฉ
    	shopping.find();
    	shopping.cart();
    	shopping.order();
    	shopping.check();
    	return shopping;
    }

    //factory method
    abstract Shopping selectCategory(String category);
}

3.ConcreteCreator  : Timon๊ณผ coupang์€ ์‡ผํ•‘๋ชฐ์ด๊ธฐ์— ShopingMall ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.

//Timon.java

public class Timon extends ShoppingMall{

  @Override
  Shopping selectCategory(String category) {

  System.out.println("-----------Timon-----------");

  if (category.equals("Food")) {
  	return new TimonFoodCategory();
  } else if (category.equals("Health")) {
  	return new TimonHealthCategory();
  }
  	return null;
  }
    
}
//Koupang.java

public class Koupang extends ShoppingMall{

  @Override
  Shopping selectCategory(String category) {
  
  System.out.println("-----------Koupang-----------");
  
  if (category.equals("Food")) {
  	return new KoupangFoodCategory();
  } else if (category.equals("Health")) {
  	return new KoupangHealthCategory();
  }
  	return null;
  }
    
}

4.ConcreateProduct  : Shopping ์ธํ„ฐํŽ˜์ด์Šค์— ์ž‘์„ฑํ•ด๋‘์—ˆ๋˜ ๊ณผ์ •์„ override๋ฅผ ํ†ตํ•ด ๊ตฌ์ฒด์ ์œผ๋กœ ์ž‘์„ฑ

//TimonFoodCategory.java

public class TimonFoodCategory implements Shopping{
	
  @Override
  public void find() {
  	System.out.println("๋‹ญ๊ฐ€์Šด์‚ด ๋ธŒ๋žœ๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.");
  }

  @Override
  public void cart() {
  	System.out.println("๋งˆ์Œ์— ๋“œ๋Š” ๋‹ญ๊ฐ€์Šด์‚ด์„ ์นดํŠธ์— ๋‹ด์Šต๋‹ˆ๋‹ค.");
  }

  @Override
  public void order() {
  	System.out.println("๋‹ญ๊ฐ€์Šด์‚ด์„ ์ฃผ๋ฌธํ•ฉ๋‹ˆ๋‹ค.");
  }

  @Override
  public void check() {
  	System.out.println("๋‹ญ๊ฐ€์Šด์‚ด ์ฃผ๋ฌธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.");
  }
  
}
//TimonHealthCategory.java

public class TimonHealthCategory implements Shopping{

  @Override
  public void find() {
  	System.out.println("๊ตฌ๋งคํ•˜๋ ค๋Š” ๋น„ํƒ€๋ฏผ์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.");
  }

  @Override
  public void cart() {
  	System.out.println("๋งˆ์Œ์— ๋“œ๋Š” ๋น„ํƒ€๋ฏผ์„ ์นดํŠธ์— ๋‹ด์Šต๋‹ˆ๋‹ค.");
  }

  @Override
  public void order() {
  	System.out.println("๋น„ํƒ€๋ฏผ์„ ์ฃผ๋ฌธํ•ฉ๋‹ˆ๋‹ค.");
  }

  @Override
  public void check() {
  	System.out.println("๋น„ํƒ€๋ฏผ ์ฃผ๋ฌธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.");
  }
//KoupangFoodCategory.java

public class KoupangFoodCategory implements Shopping{

  @Override
  public void find() {
  	System.out.println("๊ตฌ์šด ๋‹ฌ๊ฑ€์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.");
  }

  @Override
  public void cart() {
  	System.out.println("์ฐธ๋‚˜๋ฌด๋กœ ๊ตฌ์šด(...) ๋‹ฌ๊ฑ€์„ ์นดํŠธ์— ๋‹ด์Šต๋‹ˆ๋‹ค.");
  }

  @Override
  public void order() {
  	System.out.println("๋‹ฌ๊ฑ€์„ ์ฃผ๋ฌธํ•ฉ๋‹ˆ๋‹ค.");
  }

  @Override
  public void check() {
  	System.out.println("๋‹ฌ๊ฑ€ ์ฃผ๋ฌธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.");
  }
  
}
//KoupangHealthCategory.java

public class KoupangHealthCategory implements Shopping{

  @Override
  public void find() {
  	System.out.println("๊ตฌ๋งคํ•˜๋ ค๋Š” ํ”„๋กœํ‹ด์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.");
  }

  @Override
  public void cart() {
  	System.out.println("๋งˆ์Œ์— ๋“œ๋Š” ํ”„๋กœํ‹ด์„ ์นดํŠธ์— ๋‹ด์Šต๋‹ˆ๋‹ค.");
  }

  @Override
  public void order() {
  	System.out.println("ํ”„๋กœํ‹ด์„ ์ฃผ๋ฌธํ•ฉ๋‹ˆ๋‹ค.");
  }

  @Override
  public void check() {
  	System.out.println("ํ”„๋กœํ‹ด ์ฃผ๋ฌธ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.");
  }

}
  1. Main ํด๋ž˜์Šค
Main.java

public class Main {
  public static void main(String[] args) {

    //Timon ์‡ผํ•‘
    ShoppingMall timon = new Timon();

    Shopping vitamin = timon.shopping("Health"); //๊ฑด๊ฐ• ์นดํ…Œ๊ณ ๋ฆฌ
    Shopping chickenBreast = timon.shopping("Food"); // ์‹๋ฃŒํ’ˆ ์นดํ…Œ๊ณ ๋ฆฌ

    //Koupang ์‡ผํ•‘
    ShoppingMall koupang = new Koupang();

    Shopping protein = koupang.shopping("Health"); //๊ฑด๊ฐ• ์นดํ…Œ๊ณ ๋ฆฌ
    Shopping roastedEgg = koupang.shopping("Food"); //์‹๋ฃŒํ’ˆ ์นดํ…Œ๊ณ ๋ฆฌ
    
  }
}

์žฅ์  )

  • ์ƒ์„ฑ ํ•  ํด๋ž˜์Šค๋ฅผ ๋ฏธ๋ฆฌ ์•Œ์ง€ ๋ชปํ•ด๋„ ํŒฉํ† ๋ฆฌ ํด๋ž˜์Šค๊ฐ€ ๊ฐ์ฒด์˜ ์ƒ์„ฑ์„ ๋‹ด๋‹น
  • ๊ฐ์ฒด์˜ ์ž๋ฃŒํ˜•์ด ํ•˜์œ„ํด๋ž˜์Šค์— ์˜ํ•ด ๊ฒฐ์ •๋œ๋‹ค -> ํ™•์žฅ ์šฉ์ด์„ฑ
  • ๋™์ผํ•œ ํ˜•ํƒœ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐ€๋Šฅ
  • ํ™•์žฅ์„ฑ ์žˆ๋Š” ์ „์ฒด ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ ๊ฐ€๋Šฅ

๋‹จ์  )

  • ์žฌ์‚ฌ์šฉ์˜ ๋ฌธ์ œ = > ๊ฐ์ฒด๊ฐ€ ๋Š˜์–ด๋‚  ๋•Œ ๋งˆ๋‹ค ํ•˜์œ„ํด๋ž˜์Šค ์žฌ์ •์˜๋กœ ์ธํ•œ ๋ถˆํ•„์š”ํ•œ ๋งŽ์€ ํด๋ž˜์Šค ์ƒ์„ฑ ๊ฐ€๋Šฅ์„ฑ

๐Ÿ“Œ ์ •๋ฆฌ

ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ํด๋ž˜์Šค๊ฐ„์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

๊ฒฐํ•ฉ๋„๋Š” ํด๋ž˜์Šค์˜ ๋ณ€๊ฒฝํ•  ๋ถ€๋ถ„์ด ์ƒ๊ฒผ์„ ๋•Œ ๋‹ค๋ฅธ ํด๋ž˜์Šค์— ์–ผ๋งˆ๋‚˜ ์˜ํ–ฅ์„ ์ฃผ๋Š”๊ฐ€ ์ด๋‹ค.

ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์ง์ ‘ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์„œ๋ธŒ ํด๋ž˜์Šค์— ์œ„์ž„ํ•จ์œผ๋กœ์จ ํšจ์œจ์ ์ธ ์ฝ”๋“œ ์ œ์–ด๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ณ  ์˜์กด์„ฑ์„ ์ œ๊ฑฐํ•œ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ ๊ฒฐํ•ฉ๋„ ๋˜ํ•œ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๋‹ค.

๋ ˆํผ๋Ÿฐ์Šค