๐ŸŒป JAVA/๋””์ž์ธ ํŒจํ„ด

Factory Pattern (feat. ์ถ”์ƒ ํŒฉํ† ๋ฆฌ, ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ)

iseunghan 2023. 6. 5. 23:08
๋ฐ˜์‘ํ˜•

1. ๊ฐ„๋‹จํ•œ ํŒฉํ† ๋ฆฌ ํŒจํ„ด

๋จผ์ € ๊ฐ„๋‹จํ•œ ํŒฉํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ณ  ์ด๋ฅผ ์ ์ง„์ ์œผ๋กœ ํŒจํ„ด์„ ์ ์šฉํ•˜์—ฌ ์—…๊ทธ๋ ˆ์ด๋“œ ํ•˜๋Š” ์‹์œผ๋กœ ์ง„ํ–‰ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ์•„๋ž˜์— ๋…ธํŠธ๋ถ์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ type์— ๋”ฐ๋ผ์„œ ๋…ธํŠธ๋ถ์„ ์ƒ์„ฑํ•˜๊ณ  ์ค€๋น„, ์„ค์ •, ํฌ์žฅํ•ด์„œ ์ตœ์ข…์ ์œผ๋กœ ๋…ธํŠธ๋ถ์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

public NoteBook creatNoteBook(String type) {
    NoteBook noteBook;

    if(type.equals("Macbook")) {
        noteBook =  new MackBook();
    } else if (type.equals("LG Gram")) {
        noteBook =  new LGGram();
    } else if (type.equals("GalaxyBook")) {
        noteBook =  new GalaxyBook();
    } else if (... ...) {
        ...
    }

    noteBook.prepare();
    noteBook.make();
    noteBook.boxing();

    return noteBook;
}

1.1 ๋ณ€ํ•˜๋Š” ๊ฒƒ๊ณผ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ

๋จผ์ € ๋ณ€ํ•˜๋Š” ๊ฒƒ๊ณผ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1.1.1 ๋ณ€ํ•˜๋Š” ๊ฒƒ

๋…ธํŠธ๋ถ์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋Š” Type์ด ๋งŽ์•„์ง€๋ฉด ๊ทธ๋งŒํผ if-else๋ฌธ๋„ ๊ธธ์–ด์งˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

if(type.equals("Macbook")) {
    noteBook =  new MackBook();
} else if (type.equals("LG Gram")) {
    noteBook =  new LGGram();
} else if (type.equals("GalaxyBook")) {
    noteBook =  new GalaxyBook();
} else if (... ...) {
    ...
}

1.1.2 ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ

๋…ธํŠธ๋ถ ์ข…๋ฅ˜์— ์ƒ๊ด€์—†์ด ์ค€๋น„ํ•˜๊ณ , ์„ค์ •ํ•˜๊ณ , ํฌ์žฅํ•˜๋Š” ๊ฒƒ์€ ๋ณ€ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

noteBook.prepare();
noteBook.make();
noteBook.boxing();

1.2 ๋‘˜์„ ๋ถ„๋ฆฌํ•ด๋ณด์ž

1.2.1 NoteBook์„ ์ƒ์‚ฐํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํŒฉํ† ๋ฆฌ

๋…ธํŠธ๋ถ์„ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ด์   Factory๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ๋งก๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

public class SimpleNoteBookFactory {
    public NoteBook createNoteBook(String type) {
        NoteBook noteBook;

        if(type.equals("Macbook")) {
            noteBook =  new MackBook();
        } else if (type.equals("LG Gram")) {
            noteBook =  new LGGram();
        } else if (type.equals("GalaxyBook")) {
            noteBook =  new GalaxyBook();
        } else if (... ...) {
            ...
        }
        return noteBook;
    }
}

1.2.2 ๋…ธํŠธ๋ถ์„ ํŒ๋งคํ•˜๋Š” ์Šคํ† ์–ด

Store์—์„œ๋Š” Factory์—์„œ ์ƒ์„ฑ๋œ ๋…ธํŠธ๋ถ์„ ๋ฐ›์•„์„œ ์ค€๋น„ํ•˜๊ณ , ์„ค์ •ํ•˜๊ณ , ํฌ์žฅํ•˜๋Š” ์ผ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

public class NoteBookStore {
    SimpleNoteBookFactory noteBookFactory;

    public NoteBookStore(SimpleNoteBookFactory noteBookFactory) {
        this.noteBookFactory = noteBookFactory;
    }

    public NoteBook orderNoteBook(String type) {
        NoteBook noteBook = noteBookFactory.createNoteBook(type);

        noteBook.prepare();
        noteBook.make();
        noteBook.boxing();

        return noteBook;
    }
}

์ •๋ฆฌ

๊ฐ„๋‹จํ•œ ํŒฉํ† ๋ฆฌ(๋‹จ์ˆœ ํŒฉํ† ๋ฆฌ)๋Š” ๋””์ž์ธ ํŒจํ„ด์ด ์•„๋‹™๋‹ˆ๋‹ค. ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋˜๋Š” ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์„ ๋„์ž…ํ•˜๋Š” ์ค‘๊ฐ„ ๋‹จ๊ณ„๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’ก ์žฅ์ 

  • ๋ณ€ํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌ
  • ๋‹จ์ผ ์ฑ…์ž„ ์›์น™

๐Ÿ’ก ๋‹จ์ 

  • ํ•˜๋‚˜์˜ ํŒฉํ† ๋ฆฌ์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑ
  • ํด๋ž˜์Šค๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค๋ฉด if-else๋ฌธ ๊ธธ์ด๋„ ํ•จ๊ป˜ ๋Š˜์–ด๋‚จ

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

2.1 ์ œ์กฐ์‚ฌ ๋งˆ๋‹ค ์Šคํ† ์–ด ์ƒ์„ฑ

๊ฐ ๋…ธํŠธ๋ถ ์ œ์กฐ์‚ฌ๋งˆ๋‹ค ์Šคํ† ์–ด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ•˜๋‚˜์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ ์ƒˆ๋กœ์šด ์ œ์กฐ์‚ฌ๊ฐ€ ๋“ฑ์žฅํ•ด๋„ ์œ ์—ฐํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public abstract class NoteBookStore {
	public NoteBook class orderNoteBook(String type) {
		NoteBook noteBook = createNoteBook(type);

		noteBook.prepare();
    noteBook.make();
    noteBook.boxing();

		return noteBook;
	}

	public abstract NoteBook createNoteBook();
}

์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋ฉด, ๊ฐ ์ œ์กฐ์‚ฌ์—์„œ๋Š” noteBook์„ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๊ฐ ์ œ์กฐ์‚ฌ์˜ ๋…ธํŠธ๋ถ ์ƒ์„ฑ ์ฝ”๋“œ๋Š” ๊ฐ ์ œ์กฐ์‚ฌ์Šคํ† ์–ด์— ์บก์Šํ™”๊ฐ€ ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋“ฑ์žฅ public abstract NoteBook createNoteBook(String type);

ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ์„ ์ฒ˜๋ฆฌํ•˜๋ฉฐ, ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ž‘์—…์„ ์„œ๋ธŒ ํด๋ž˜์Šค์— ์บก์Šํ™”์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ˆ˜ํผํด๋ž˜์Šค์— ์žˆ๋Š” ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ์™€ ์„œ๋ธŒํด๋ž˜์Šค์— ์žˆ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ์ฝ”๋“œ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2.1.1 AppleStore ์ƒ์„ฑ

public class MacBookStore extends NoteBookStore {
	public NoteBook createNoteBook(String type) {
	    return new MackBook(...); // ๋งค๊ฐœ๋ณ€์ˆ˜ ์ƒ๋žต
	}
}

2.2 NoteBook ํด๋ž˜์Šค ์ƒ์„ฑ

public abstract class NoteBook {

    String name;
    String cpu;
    String ram;
    String storage;
    String display;
    String os;

    public void prepare() {
        System.out.println("Preparing.. " + "name=" + name + ", cpu=" + cpu + ", ram=" + ram + ", storage=" + storage + ", display=" + display + ", os=" + os);
    }

    public void make() {
        System.out.println("Make NoteBook...");
    }

    public void boxing() {
        System.out.println("Boxing NoteBook...");
    }
}

2.2.1 NoteBook ์„œ๋ธŒ ํด๋ž˜์Šค ์˜ˆ์‹œ

MacBook์ด๋ผ๋ฉด ์•„๋ž˜ ์ฒ˜๋Ÿผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public class MacBook extends NoteBook {
    public MacBook() {
        this.name = "MacBook";
        this.cpu = "M2";
        this.ram = "16GB";
        this.storage = "SSD 500GB";
        this.display = "Letina Display";
        this.os = "macOS";
    }
}

2.3 ์‹ค์ œ ๋…ธํŠธ๋ถ์„ ์ฃผ๋ฌธํ•ด๋ณด์ž

public class Main {
	public static void main(String[] agrs) {
		NoteBookStore store = new MacBookStore();
		NoteBook noteBook = store.orderNoteBook();
		System.out.println("My Order NoteBook is " + noteBook.getName());
	}
}

์ •๋ฆฌ

ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ๊ฐ์ฒด ์ƒ์„ฑ์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ , ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“œ๋Š” ์ผ์„ ์„œ๋ธŒํด๋ž˜์Šค์—๊ฒŒ ์œ„์ž„ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก ์žฅ์ 

  • ๊ฐ์ฒด ์ƒ์„ฑ์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ์ œ๊ณต (๊ฐœ๋ฐฉ/ํ์‡„ ์›์น™)
  • ์„œ๋ธŒ ํด๋ž˜์Šค์—๊ฒŒ ๊ฐ์ฒด ์ƒ์„ฑ์„ ์œ„์ž„ (๋‹จ์ผ ์ฑ…์ž„ ์›์น™)
  • ์ƒ์„ฑํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถค

๐Ÿ’ก ๋‹จ์ 

  • ํŒจํ„ด์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ธŒ ํด๋ž˜์Šค๋“ค์ด ๋งŽ์•„์ ธ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ์Œ

3. ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด(Abstract Fatory Pattern)

3.1 AbstractFactory

๊ฐ ์ œ์กฐ์‚ฌ์˜ ๋ถ€ํ’ˆ๋“ค์„ ์ƒ์„ฑํ•˜๋Š” ๊ณต์žฅ์„ ๋งŒ๋“ค๊ธฐ ์ „์—, ์ถ”์ƒ ํŒฉํ† ๋ฆฌ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ณต์žฅ์—์„œ ์–ด๋–ป๊ฒŒ ๋ถ€ํ’ˆ์„ ์ƒ์‚ฐํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ํ–‰๋™๋“ค์ด ์ •ํ•ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ ์ œ์กฐ์‚ฌ ๊ณต์žฅ๋“ค์€ AbstractFactory๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ๊ฐ ์ œํ’ˆ์— ๋งž๋Š” ๋ถ€ํ’ˆ๋“ค์„ ์ƒ์‚ฐํ•ด๋‚ด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

factory/AbstractFactory

public interface NoteBookSemiconductorFactory {
    Cpu createCpu();
    Ram createRam();
    Storage createStorage();
    Display createDisplay();
    Os createOs();
}

factory/ConcreteFactory1

public class MacBookSemiconductorFactory implements NoteBookSemiconductorFactory {
    @Override
    public Cpu createCpu() {
        return new AppleCpu("M1");
    }

    @Override
    public Ram createRam() {
        return new AppleRam("8GB");
    }

    @Override
    public Storage createStorage() {
        return new AppleStorage("HDD 250GB");
    }

    @Override
    public Display createDisplay() {
        return new AppleDisplay("Letina Display");
    }

    @Override
    public Os createOs() {
        return new MacOS("macOS");
    }
}

๋งฅ๋ถ์€ AppleCpu, AppleRam, … MacBook์— ๋งž๋Š” ๋ถ€ํ’ˆ๋“ค์„ ์ƒ์‚ฐํ•ด๋ƒ…๋‹ˆ๋‹ค.

factory/ConcreteFactory2

public class GalaxyBookSemiconductorFactory implements NoteBookSemiconductorFactory {
    @Override
    public Cpu createCpu() {
        return new SamsungCpu("i-7700k");
    }

    @Override
    public Ram createRam() {
        return new SamsungRam("16GB");
    }

    @Override
    public Storage createStorage() {
        return new SamsungStorage("HDD 250GB");
    }

    @Override
    public Display createDisplay() {
        return new SamsungDisplay("LCD Display");
    }

    @Override
    public Os createOs() {
        return new WindowsOS("windowsOS");
    }
}

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ GalaxyBook ์ œํ’ˆ์— ๋งž๋Š” ๋ถ€ํ’ˆ๋“ค์„ ์ƒ์‚ฐํ•ด๋‚ด๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

3.2 AbstractProduct

๊ฐ ๊ณต์žฅ์—์„œ ๋ถ€ํ’ˆ์„ ๋งŒ๋“ค ๋•Œ, ์–ด์จŒ๋“  AppleCpu๋‚˜ SamsungCpu๋‚˜ ๊ฒฐ๊ตญ์—๋Š” CPU์˜ ์ข…๋ฅ˜์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ๋ถ€ํ’ˆ๋งˆ๋‹ค ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜์—ฌ ๊ฐ ์ œ์กฐ์‚ฌ์—์„œ๋Š” ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ํ™•์žฅ์ด ๊ฐ€๋Šฅํ•œ ๊ตฌ์กฐ๋กœ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

product/AbstractProduct

public interface Cpu {
}

product/concreteProduct1

public class AppleCpu implements Cpu {
    private String name;

		// getter, setter, ...
}

product/concreteProduct2

public class SamsungCpu implements Cpu {
    private String name;

		// getter, setter, ...
}

… ๋‚˜๋จธ์ง€๋“ค์€ ์ƒ๋žต

3.3 NoteBook

๊ธฐ์กด ๋…ธํŠธ๋ถ์— ๋ถ€ํ’ˆ๋“ค์„ String์—์„œ interface ํƒ€์ž…์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. prepare()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์„œ๋ธŒ ํด๋ž˜์Šค์—์„œ ์–ด๋–ค ๋ถ€ํ’ˆ๋“ค์„ ์จ์•ผํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์œ„์ž„ํ•ฉ๋‹ˆ๋‹ค. ์„ค์ •ํ•˜๊ณ  ํฌ์žฅํ•˜๋Š” ์ผ์€ ์–ด๋Š ๋…ธํŠธ๋ถ์ด๋‚˜ ๋™์ผํ•˜๋‹ˆ ํ•„์š”์— ๋”ฐ๋ผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

notebook/AbstractNoteBook

public abstract class NoteBook {

    String name;
    Cpu cpu;
    Ram ram;
    Storage storage;
    Display display;
    Os os;

    public abstract void prepare();

    public void setting() {
        System.out.println("Setting NoteBook...");
    }

    public void box() {
        System.out.println("Boxing NoteBook...");
    }
}

notebook/MacBook

๊ฐ ๊ตฌ์ƒ ํด๋ž˜์Šค์—๋Š” ๋ถ€ํ’ˆ๊ณต์žฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ•ด๋‹น ๋…ธํŠธ๋ถ์—๋Š” ์–ด๋–ค ๋ถ€ํ’ˆ์„ ์จ์•ผํ•˜๋Š”์ง€ ์ผ์ผ์ด ์•Œํ•„์š”๊ฐ€ ์—†์–ด์กŒ์Šต๋‹ˆ๋‹ค.

public class MacBook extends NoteBook {

    NoteBookSemiconductorFactory semiconductorFactory;

    public MacBook(NoteBookSemiconductorFactory semiconductorFactory) {
        this.semiconductorFactory = semiconductorFactory;
    }

    @Override
    public void prepare() {
        this.name = "MacBook";
        this.cpu = semiconductorFactory.createCpu();
        this.ram = semiconductorFactory.createRam();
        this.storage = semiconductorFactory.createStorage();
        this.display = semiconductorFactory.createDisplay();
        this.os = semiconductorFactory.createOs();
    }

    @Override
    public String toString() {
        return "MacBook{" +
                "name='" + name + '\\\\'' +
                ", cpu=" + cpu +
                ", ram=" + ram +
                ", storage=" + storage +
                ", display=" + display +
                ", os=" + os +
                '}';
    }
}

notebook/GalaxyBook

public class GalaxyBook extends NoteBook {

    NoteBookSemiconductorFactory semiconductorFactory;

    public GalaxyBook(NoteBookSemiconductorFactory semiconductorFactory) {
        this.semiconductorFactory = semiconductorFactory;
    }

    @Override
    public void prepare() {
        this.name = "GalaxyBook";
        this.cpu = semiconductorFactory.createCpu();
        this.ram = semiconductorFactory.createRam();
        this.storage = semiconductorFactory.createStorage();
        this.display = semiconductorFactory.createDisplay();
        this.os = semiconductorFactory.createOs();
    }

    @Override
    public String toString() {
        return "GalaxyBook{" +
                "name='" + name + '\\\\'' +
                ", cpu=" + cpu +
                ", ram=" + ram +
                ", storage=" + storage +
                ", display=" + display +
                ", os=" + os +
                '}';
    }
}

3.4 Client

ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. ์ถ”์ƒ ํด๋ž˜์Šค๋กœ createNoteBook()์ด๋ผ๋Š” ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋Š” ์„œ๋ธŒ ํด๋ž˜์Šค์—๊ฒŒ ๊ฐ์ฒด ์ƒ์„ฑ ๋ถ€๋ถ„์„ ์œ„์ž„ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

public abstract class NoteBookStore {

    public NoteBook orderNoteBook() {
        NoteBook noteBook = createNoteBook();

        noteBook.prepare();
        noteBook.make();
        noteBook.boxing();

        return noteBook;
    }

    protected abstract NoteBook createNoteBook();
}

client/ConcreteClient1

์Šคํ† ์–ด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ „๋‹ฌ๋ฐ›์€ ๋ถ€ํ’ˆ๊ณต์žฅ์„ ๊ฐ€์ง€๊ณ  ๋…ธํŠธ๋ถ์„ ์ƒ์„ฑํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. createNoteBook()์—์„œ๋Š” ์–ด๋–ค ๋ถ€ํ’ˆ์„ ๊ฐ€์ง€๊ณ  ์ƒ์„ฑํ–ˆ๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ์•Œ์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

public class MacBookStore extends NoteBookStore {

    NoteBookSemiconductorFactory factory;

    public MacBookStore(NoteBookSemiconductorFactory factory) {
        this.factory = factory;
    }

    @Override
    protected NoteBook createNoteBook() {
        return new MacBook(factory);
    }
}

client/ConcreteClient2

public class GalaxyBookStore extends NoteBookStore {

    NoteBookSemiconductorFactory factory;

    public GalaxyBookStore(NoteBookSemiconductorFactory factory) {
        this.factory = factory;
    }

    @Override
    protected NoteBook createNoteBook() {
        return new GalaxyBook(factory);
    }
}

3.5 Main

์‹ค์ œ ๋…ธํŠธ๋ถ์„ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

public class Main {
    public static void main(String[] args) {
        MacBookSemiconductorFactory macBookSemiconductorFactory = new MacBookSemiconductorFactory();
        GalaxyBookSemiconductorFactory galaxyBookSemiconductorFactory = new GalaxyBookSemiconductorFactory();

        MacBookStore macBookStore = new MacBookStore(macBookSemiconductorFactory);
        GalaxyBookStore galaxyBookStore = new GalaxyBookStore(galaxyBookSemiconductorFactory);

        NoteBook macBook = macBookStore.orderNoteBook();
        System.out.println(macBook);

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

        NoteBook galaxyBook = galaxyBookStore.orderNoteBook();
        System.out.println(galaxyBook);
    }
}

์‹คํ–‰๊ฒฐ๊ณผ

Make NoteBook...
Boxing NoteBook...
MacBook{name='MacBook', cpu=AppleCpu{name='M1'}, ram=AppleRam{name='8GB'}, storage=AppleStorage{name='HDD 250GB'}, display=AppleDisplay{name='Letina Display'}, os=MacOS{name='macOS'}}
---------------------
Make NoteBook...
Boxing NoteBook...
GalaxyBook{name='GalaxyBook', cpu=SamsungCpu{name='i-7700k'}, ram=SamsungRam{name='16GB'}, storage=SamsungStorage{name='HDD 250GB'}, display=SamsungDisplay{name='LCD Display'}, os=WindowsOS{name='windowsOS'}}

์ •๋ฆฌ

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

๐Ÿ’ก ์žฅ์ 

  • ๊ตฌ์ƒ ์ œํ’ˆ๋“ค๊ณผ ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ ์‚ฌ์ด์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถค
  • ํŒฉํ† ๋ฆฌ์—์„œ ์ƒ์„ฑ๋˜๋Š” ์ œํ’ˆ๋“ค์˜ ์ƒํ˜ธํ˜ธํ™˜์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์Œ
  • ๋‹จ์ผ ์ฑ…์ž„ ์›์น™
  • ๊ฐœ๋ฐฉ/ํ์‡„ ์›์น™

๐Ÿ’ก ๋‹จ์ 

  • ํŒจํ„ด์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด ์ธํ„ฐํŽ˜์ด์Šค๋“ค์ด ๋งŽ์ด ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ์Œ
  • ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ๋ชจ๋“  ํŒฉํ† ๋ฆฌ์— ๋Œ€ํ•œ ์ฝ”๋“œ ์ˆ˜์ •์ด ํ•„์š”ํ•˜๋‹ค.

REFERENCES

 

ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ํŒจํ„ด

/ ๋””์ž์ธ ํŒจํ„ด๋“ค / ์ƒ์„ฑ ํŒจํ„ด ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ํŒจํ„ด ๋‹ค์Œ ์ด๋ฆ„์œผ๋กœ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค: ๊ฐ€์ƒ ์ƒ์„ฑ์ž, Factory Method ์˜๋„ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ๊ฐ์ฒด๋“ค์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜์ง€

refactoring.guru

 

 

์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด

/ ๋””์ž์ธ ํŒจํ„ด๋“ค / ์ƒ์„ฑ ํŒจํ„ด ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด ๋‹ค์Œ ์ด๋ฆ„์œผ๋กœ๋„ ๋ถˆ๋ฆฝ๋‹ˆ๋‹ค: Abstract Factory ์˜๋„ ์ถ”์ƒ ํŒฉํ† ๋ฆฌ๋Š” ๊ด€๋ จ ๊ฐ์ฒด๋“ค์˜ ๊ตฌ์ƒ ํด๋ž˜์Šค๋“ค์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ ๋„ ๊ด€๋ จ ๊ฐ์ฒด๋“ค์˜ ๋ชจ์Œ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ

refactoring.guru

 

 

๐Ÿ’  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ(Factory Method) ํŒจํ„ด - ์™„๋ฒฝ ๋งˆ์Šคํ„ฐํ•˜๊ธฐ

Factory Method Pattern ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ๊ฐ์ฒด ์ƒ์„ฑ์„ ๊ณต์žฅ(Factory) ํด๋ž˜์Šค๋กœ ์บก์Šํ™” ์ฒ˜๋ฆฌํ•˜์—ฌ ๋Œ€์‹  ์ƒ์„ฑํ•˜๊ฒŒ ํ•˜๋Š” ์ƒ์„ฑ ๋””์ž์ธ ํŒจํ„ด์ด๋‹ค. ์ฆ‰, ํด๋ผ์ด์–ธํŠธ์—์„œ ์ง์ ‘ new ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ์ œํ’ˆ ๊ฐ์ฒด๋ฅผ

inpa.tistory.com

 

๐Ÿ’  ์ถ”์ƒ ํŒฉํ† ๋ฆฌ(Abstract Factory) ํŒจํ„ด - ์™„๋ฒฝ ๋งˆ์Šคํ„ฐํ•˜๊ธฐ

Abstract Factory Pattern ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์€ ์—ฐ๊ด€์„ฑ์ด ์žˆ๋Š” ๊ฐ์ฒด ๊ตฐ์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์„ ๊ฒฝ์šฐ ์ด๋“ค์„ ๋ฌถ์–ด ์ถ”์ƒํ™”ํ•˜๊ณ , ์–ด๋–ค ๊ตฌ์ฒด์ ์ธ ์ƒํ™ฉ์ด ์ฃผ์–ด์ง€๋ฉด ํŒฉํ† ๋ฆฌ ๊ฐ์ฒด์—์„œ ์ง‘ํ•ฉ์œผ๋กœ ๋ฌถ์€ ๊ฐ์ฒด ๊ตฐ์„ ๊ตฌํ˜„ํ™”

inpa.tistory.com

 

๋ฐ˜์‘ํ˜•