https://cyclic-basket-9b5.notion.site/1c1eeedb4edc4f388897822b04237ee9?pvs=4
๊ฐ์ฒด ์์ฑ์ ๊ณต์ฅ(Factory) ํด๋์ค๋ก ์บก์ํ ์ฒ๋ฆฌํด์ ๋์ ์์ฑํ๊ฒ ํ๋ ์์ฑ ๋์์ธ ํจํด์ด๋ค.
์ข ๋ ์์ธํ๊ฒ ์ค๋ช ํ์๋ฉด, ํด๋ผ์ด์ธํธ์์ ์ง์ new ์ฐ์ฐ์๋ฅผ ํตํด ์ ํ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ ๋์ ์ ํ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๊ณต์ฅ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ , ์ด๋ฅผ ์์ํ๋ ์๋ธ ๊ณต์ฅ ํด๋์ค์ ๋ฉ์๋์์ ์ฌ๋ฌ๊ฐ์ง ์ ํ ๊ฐ์ฒด ์์ฑ์ ๊ฐ๊ฐ ์ฑ ์์ง๋ ๊ฒ์ ๋งํ๋ค.
์ด ํจํด์ ์ด์ฉํ๋ ์ด์ ๋ ๊ฐ์ฒด๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ ์ ์๊ณ , ์ ์ง๋ณด์์ ์ฉ์ดํด์ง๊ธฐ ๋๋ฌธ์ด๋ค.
๐ก ๊ทธ๋ ๊ณต์ฅํํ ์์ฒญํ๋ฉด ์๋ธ ๊ณต์ฅ ํด๋์ค์ ๋ฉ์๋์์ ์ ํ์ ์์ฑํ๋ค๋ ๋ง์ ํํํ๊ฑด ์๊ฒ ๋๋ฐ, **๊ทธ๋ฅ ํ ๊ณต์ฅ์์ ๋ค ๋ง๋ค๋ฉด ์๋ผ? ์ ์๋ธ ๊ณต์ฅ์ด ํ์ํ๋ฐ?**๋ผ๋ ๋ฌผ์์ด ์๊ธธ ์ ์๋ค.์ด๋ฅผ ์ดํดํ๊ธฐ ์ํด์๋ ๋จผ์ Factory Method ํจํด๊ณผ Abstract Factory ํจํด์ ๋ฒ ์ด์ค์ธ Simple Factory๋ฅผ ์์์ผํ๋ค.
์ฐธ๊ณ ๋ก Simple Factory๋ ๋์์ธ โํจํดโ์ด๋ผ๊ณ ๋ ํ ์ ์๊ณ , OOP์์ ์์ฃผ ์ฐ์ด๋ ๊ด์ฉ๊ตฌ๋ผ๊ณ ๋ณด๋ฉด ๋๋ค.
Simple Factory๋ ๊ฐ์ฒด๋ฅผ ์์ฑํด๋ด๋ ๊ณต์ฅ์ ๋ฐ๋ก ๋๋ ๊ฒ์ด๋ค.
์ฆ, ๊ฐ์ฒด ์์ฑ ๋ถ๋ถ์ ์ ๋ดํ๋ ๊ณต์ฅ ํด๋์ค๊ฐ ๋ฐ๋ก ์๋ ๊ฒ์ด๋ค.
๊ฐ์ฒด๋ ์ฌ๋ฌ ๊ณณ์์ ์์ฑ๋ ์ ์๋ค. ํ์ง๋ง ํธ์ถํ๋ ์ชฝ์ด ๊ฐ์ฒด์ ์์ฑ์์ ์ง์ ์์กดํ๊ณ ์๋ค๋ฉด, ๋์ค์ ์์ ๋์ด์ผ ํ๋ ์ฝ๋๊ฐ ๋ง์ด ๋ฐ์ํ๋ค. (๋ฐ์์ ์์๋ฅผ ํตํด ํ์ธํด๋ณด์!)
๋ฐ๋ผ์ ์์ฑ์ ํธ์ถ(new)์ ๋ณ๋์ ํด๋์ค(Factory)์์ ๋ด๋นํ๊ณ ํด๋ผ์ด์ธํธ ์ฝ๋์์๋ ์ด Factory๋ฅผ ํตํด ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ ํ๋ค.
public interface Sopt {
void printLanguage();
}
public class Server implements Sopt {
@Override
public void printLanguage() {
System.out.println("Java");
}
}
public class Ios implements Sopt {
@Override
public void printLanguage() {
System.out.println("Swift");
}
}
Sopt๋ฅผ ํ๋ฒ ์์๋ก ๋ค์ด๋ณด์.
๊ณตํต ์ธํฐํ์ด์ค์ธย Sopt
ย ์ ์ ์ํ๊ณ ์ด๋ฅผ ๊ตฌํํ๋ย Server
,ย Ios
ย ํด๋์ค์ด๋ค.
์ด์ ํด๋ผ์ด์ธํธ ์ฝ๋์์ย Server
,ย Ios
ย ์ ์ฌ์ฉํ๊ธฐ ์ํด ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค.
public class Client {
public static void main(String[] args) {
Sopt server = new Server();
Sopt ios = new Ios();
server.printlanguage();
ios.printlanguage();
}
}
์ผ๋ฐ์ ์ธ ์ฌ์ฉ๋ฒ์ new๋ฅผ ์ฌ์ฉํด ๊ตฌํ ํด๋์ค๋ฅผ ์์ฑํ ํ ํธ์ถํ๋ ๊ฒ์ด๋ค. ํ์ง๋ง ์ด๋ ๊ฒ ๋๋ฉด ํด๋ผ์ด์ธํธ์ ํด๋์ค๋ค ์ฌ์ด์ ๋ค์๊ณผ ๊ฐ์ ์์กด๊ด๊ณ๊ฐ ์๊ธด๋ค.
์ด๋ ๊ฒ ๊ตฌํ ํด๋์ค(Server, Ios)๋ฅผ ์์กดํ๊ณ ์์ผ๋ฉด, ํด๋น ํด๋์ค์ ์์ฑ์ or ์ ์ฒ๋ฆฌ ์ฝ๋๊ฐ ๋ณ๊ฒฝ๋์์ ๋ ์ฌ์ฉํ๋ ๋ชจ๋ ํด๋ผ์ด์ธํธ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํด์ผ ํ๋ค.
๊ทธ๋ฌ๋ฉด ์ด์ ๊ฐ์ฒด์ ์์ฑ๋ง์ ๋ด๋นํ๋ ๋ณ๋์ Factory ํด๋์ค๋ฅผ ๋ง๋ค์ด๋ณด์!
public interface Sopt {
enum Part { // ์ถ๊ฐ
SERVER, IOS
}
void printLanguage();
}
public class Server implements Sopt {
@Override
public void printLanguage() {
System.out.println("Java");
}
}
public class Ios implements Sopt {
@Override
public void printLanguage() {
System.out.println("Swift");
}
}
์ฐ์ Sopt ์ธํฐํ์ด์ค์ enum ํ์ ์ผ๋ก ์ ์ธ์ ํด์ค๋ค.
public class SoptFactory {
public Sopt createSopt(Sopt.Part soptPart) {
switch (soptPart) {
case SERVER:
return new Server();
case IOS:
return new Ios();
default:
throw new IllegalArgumentException("Sopt์ ์๋ ํํธ์
๋๋ค");
}
}
}
SoptFactory๋ผ๋ ํฉํ ๋ฆฌ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ Sopt.Part์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฐํํด์ค๋ค.
public class Client {
public static void main(String[] args) {
SoptFactory soptFactory = new SoptFactory();
Sopt server = soptFactory.createSopt(Sopt.Part.SERVER);
Sopt ios = soptFactory.createSopt(Sopt.Part.IOS);
server.printLanguage();
ios.printLanguage();
}
}
soptFactory๋ฅผ ์ ์ธํ๊ณ ์์ฑ ๋ฉ์๋์ธ create๋ง ํธ์ถํ๋ฉด ์ค์ ๊ตฌํ ํด๋์ค์ธ Server, Ios์ ์์กดํ์ง ์์ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๊ฒ ๋๋ค.
์ด์ ์์กด๊ด๊ณ๋ ๋ค์๊ณผ ๊ฐ์ด ๋ฐ๋๊ฒ ๋๋ค.
ํด๋ผ์ด์ธํธ์์ ๊ตฌํํด๋์ค(Server, Ios)์ ์ง์ ์์กดํ์ง ์๊ธฐ ๋๋ฌธ์, ํด๋์ค ์ด๋ฆ์ด ๋ณ๊ฒฝ๋๊ฑฐ๋, ์์ฑ์๊ฐ ๋ณ๊ฒฝ๋์ด๋ SoptFactory ๋ด๋ถ๋ง ์์ ํ๋ฉด ๋๋ค!!
์ฐ์ ์๋ฒฝํ๋ฐ ์์ผ๋ก Simple Factory๋ก ๊ตฌํํด์ผ์ง~
๋! ๐ โโ๏ธ
์์ฝ์ง๋ง, Simple Factory๋ ๊ฐ์ฒด ์์ฑ์ ์ญํ ์ ํฉํ ๋ฆฌ์ ๋ด๋น์์ผ ํ์ฅ์ด ์ฉ์ดํ๋ค๋ ์ฅ์ ์ด ์์ง๋ง, ๋ณ๊ฒฝ์ ๋ซํ ์์ด์ผ ํ๋ค๋ OCP ์์น์ ์๋ฐฐ๋๋ค.
๋ง์ฝ ์๋ก์ด Sopt ์ธํฐํ์ด์ค์ ๊ตฌํ ํด๋์ค๋ก Android๊ฐ ์ถ๊ฐ๋์๋ค๊ณ ๊ฐ์ ํด๋ณด์.
public class Client {
public static void main(String[] args) {
SoptFactory soptFactory = new SoptFactory();
Sopt server = soptFactory.createSopt(Sopt.Part.SERVER);
Sopt ios = soptFactory.createSopt(Sopt.Part.IOS);
Sopt android = soptFactory.createSopt(Sopt.Part.ANDROID); // ์ถ๊ฐ
server.printLanguage();
ios.printLanguage();
android.printLanguage(); // ์ถ๊ฐ
}
}
๋ฌธ์ ์์ด๋ณด์ด๋๋ฐ ์ด๋์ OCP๋ฅผ ์๋ฐํ๋ค๋๊ฑฐ์ง?
public interface Sopt {
enum Part {
SERVER, IOS, ANDROID // ์ถ๊ธฐ
}
void printLanguage();
}
public class Server implements Sopt {
@Override
public void printLanguage() {
System.out.println("Java");
}
}
public class Ios implements Sopt {
@Override
public void printLanguage() {
System.out.println("Swift");
}
}
public class Android implements Sopt { // ์ถ๊ฐ
@Override
public void printLanguage() {
System.out.println("Kotlin");
}
}
Android ํด๋์ค๋ฅผ ์๋กญ๊ฒ ์ถ๊ฐํ์ง๋ง,
๊ธฐ์กด Sopt.Part
์ด๊ฑฐํ์ ์๋ก์ด ํ์
์ ์ถ๊ฐํด์ผ ํ๋ค ๐ญ
public class SoptFactory {
public Sopt createSopt(Sopt.Part soptPart) {
switch (soptPart) {
case SERVER:
return new Server();
case IOS:
return new Ios();
case ANDROID:
return new Android();
default:
throw new IllegalArgumentException("Sopt์ ์๋ ํํธ์
๋๋ค");
}
}
}
**SoptFactory
**์ create
๋ฉ์๋์ ์๋ก์ด **case
**๋ฅผ ์ถ๊ฐํด์ผ ํ๋ค ๐ญ
์ด๋ ๋ฏ OCP๋ฅผ ์๋ฐํ๊ฒ ๋๋ค.
๊ทธ๋ฌ๋ฉด OCP๋ฅผ ์๋ฐํ์ง ์๊ณ ํฉํ ๋ฆฌ ํจํด์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์์๊น?
ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ด๋ ์ถ์ ํฉํ ๋ฆฌ ํจํด์ ํ์ฉํ๋ค๋ฉด ๊ธฐ์กด ํด๋์ค์ ์ํฅ์ ์ฃผ์ง ์๊ณ ํ์ฅ์ด ๊ฐ๋ฅํ๋ค ๐ ๋ณธ๋ก ์ผ๋ก ๋ค์ด๊ฐ๋ณด์ ๐จ๐ปโ๐ป
๋ค์ ํ ๋ฒ ๋ฆฌ๋ง์ธ๋ ํ์๋ฉด, Factory Method ํจํด์ด๋ ํด๋ผ์ด์ธํธ์์ ์ง์ new ์ฐ์ฐ์๋ฅผ ํตํด ์ ํ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ ๋์ ์ ํ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๊ณต์ฅ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ (์ฌ๊ธฐ๊น์ง Simple Factory ์ ๋์ผ), ์ด๋ฅผ ์์ํ๋ ์๋ธ ๊ณต์ฅ ํด๋์ค์ ๋ฉ์๋์์ ์ฌ๋ฌ๊ฐ์ง ์ ํ ๊ฐ์ฒด ์์ฑ์ ๊ฐ๊ฐ ์ฑ ์์ง๋ ๊ฒ์ ๋งํ๋ค.(์ด ๋ถ๋ถ์ด ์ฐจ์ด์ !)

- Creator : ์ต์์ ๊ณต์ฅ ํด๋์ค๋ก์, ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ์ถ์ํํ์ฌ ์๋ธ ํด๋์ค๋ก ํ์ฌ๊ธ ๊ตฌํํ๋๋ก ํจ.
- ๊ฐ์ฒด ์์ฑ ์ฒ๋ฆฌ ๋ฉ์๋(someOperation) : ๊ฐ์ฒด ์์ฑ์ ๊ดํ ์ ์ฒ๋ฆฌ, ํ ์ฒ๋ฆฌ๋ฅผ ํ ํ๋ฆฟํํ ๋ฉ์๋
- ํฉํ ๋ฆฌ ๋ฉ์๋(createProduct) : ์๋ธ ๊ณต์ฅ ํด๋์ค์์ ์ฌ์ ์ํ ๊ฐ์ฒด ์์ฑ ์ถ์ ๋ฉ์๋
- ConcreteCreator : ๊ฐ ์๋ธ ๊ณต์ฅ ํด๋์ค๋ค์ ์ด์ ๋ง๋ ์ ํ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋๋ก ์์ฑ ์ถ์ ๋ฉ์๋๋ฅผ ์ฌ์ ์ํ๋ค. ์ฆ, ์ ํ ๊ฐ์ฒด ํ๋๋น ๊ทธ์ ๊ฑธ๋ง๋ ์์ฐ ๊ณต์ฅ ๊ฐ์ฒด๊ฐ ์์น๋๋ค.
- Product : ์ ํ ๊ตฌํ์ฒด๋ฅผ ์ถ์ํํ ๊ฒ
- ConcreateProduct : ์ ํ ๊ตฌํ์ฒด
์๊น ๋ณด์๋ Simple Factory์ ์์์ ๋์ ํด ๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
- Product๋ Sopt
- ConcreateProduct๋ Server, Ios
- Creator๋ SoptFactory(์๋ธ ํด๋์ค๋ก ํ์ฌ๊ธ ๊ตฌํํ๋๋ก ํ์ง ์์์ ๋)
์ด์ ํ ์ผ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ์ถ์ํํ์ฌ ์๋ธ ํด๋์ค๋ก ํ์ฌ๊ธ ๊ตฌํํ๋ ๊ฒ์ด๋ค.
public interface Sopt {
void printLanguage();
}
public class Server implements Sopt {
@Override
public void printLanguage() {
System.out.println("Java");
}
}
public class Ios implements Sopt {
@Override
public void printLanguage() {
System.out.println("Swift");
}
}
์ธํฐํ์ด์ค Product(Sopt)์ ๊ตฌํ ํด๋์ค ConcreateProduct(Server, Ios)์ด๋ค. Simple Factory์ ๋์ผํ๋ค.
// ๊ณต์ฅ ๊ฐ์ฒด ์ถ์ํ (์ถ์ ํด๋์ค)
public abstract class SoptFactory {
// ๊ฐ์ฒด ์์ฑ ์ฒ๋ฆฌ ๋ฉ์๋(final๋ก ์ค๋ฒ๋ผ์ด๋ฉ ๋ฐฉ์ง)
public final Sopt createOperation() {
Sopt sopt = createSopt(); // ์๋ธ ํด๋์ค์์ ๊ตฌ์ฒดํํ ํฉํ ๋ฆฌ ๋ฉ์๋ ์คํ
sopt.setup(); // .. ์ด๋ฐ์ ๊ฐ์ฒด ์์ฑ์ ๊ฐ๋ฏธํ ๋ก์ง ์คํ
return sopt; // ์์ฑ๋ ๊ฐ์ฒด ๋ฐํ
}
// ํฉํ ๋ฆฌ ๋ฉ์๋ : ๊ตฌ์ฒด์ ์ธ ๊ฐ์ฒด ์์ฑ ์ข
๋ฅ๋ ๊ฐ ์๋ธ ํด๋์ค์ ์์
// protected ์ด๊ธฐ ๋๋ฌธ์ ๊ฐ์ ํจํค์ง ๋ด์ ํด๋์ค๊ฑฐ๋, ํด๋น ํด๋์ค๋ฅผ ์์๋ฐ์ ์์ํด๋์ค์์๋ง ์ ๊ทผ ๊ฐ๋ฅ
protected abstract Sopt createSopt();
}
// ์๋ฒ๋ฅผ ์์ฑํ๋ ๊ตฌ์ฒด ํฉํ ๋ฆฌ
public class ServerFactory extends SoptFactory {
@Override
public Sopt createSopt() {
return new Server();
}
}
// iOS๋ฅผ ์์ฑํ๋ ๊ตฌ์ฒด ํฉํ ๋ฆฌ
public class IosFactory extends SoptFactory {
@Override
public Sopt createSopt() {
return new Ios();
}
}
์ฐ์ ๋๋ฌด ์ด๋ ค์ด๊ฑธโฆ?
์ฐจ๊ทผ์ฐจ๊ทผ ์ด๋ค ์ ์ด ๋ณํ๋์ง ์ดํด๋ณด์!
SoptFactory
๊ฐ ์ถ์ ํด๋์ค๋ก ๋ณํ๋์๋ค.- ๊ฐ์ฒด ์์ฑ ์ฒ๋ฆฌ ๋ฉ์๋(
createOperation
**)**๊ฐ ์๊ฒผ๋ค. - ํฉํ ๋ฆฌ ๋ฉ์๋์ธ
createSopt
๊ฐ ์ถ์ ๋ฉ์๋๋ก ๊ทธ๋ฆฌ๊ณprotected
๋ก ์ ๊ทผ์ ์ด์๊ฐ ๋ฐ๋์๋ค.
์ฒซ๋ฒ์งธ, SoptFactory๊ฐ ์ถ์ ํด๋์ค๋ก ๋ณํ๋ ์ด์ ๋
๊ตฌ์ฒด์ ์ธ ๊ฐ์ฒด ์์ฑ ๋ก์ง์ ์ฑ ์์ ์๋ธ ํด๋์ค๋ก ๋๊ธฐ๊ธฐ ์ํด์ ์ ๋๋ค. ์ด๋ฌํ ์ ๊ทผ ๋ฐฉ์์ ๊ฐ์ฒด ์์ฑ์ ๊ตฌ์ฒด์ ์ธ ์ธ๋ถ์ฌํญ์ ์จ๊ธฐ๋ฉด์๋ ํ์์ ๋ฐ๋ผ ํ์ฅํ ์ ์๋ ์ ์ฐ์ฑ์ ์ ๊ณตํ๋ค.
๋๋ฒ์งธ, ๊ฐ์ฒด ์์ฑ ์ฒ๋ฆฌ ๋ฉ์๋(createOperation)๊ฐ ์๊ธด ์ด์ ๋
๊ฐ์ฒด ์์ฑ ๊ณผ์ ์์ ๊ณตํต๋ ์ฌํญ(ex. ๊ฐ์ฒด ์์ฑ ํ ์ค์ , ์์ sopt.setup())์ ์ฒ๋ฆฌํ๊ณ , ์ค์ ๊ฐ์ฒด ์์ฑ์ createSopt ์ถ์ ๋ฉ์๋(ํฉํ ๋ฆฌ ๋ฉ์๋)์ ์์ํ๊ธฐ ์ํด์์ด๋ค.
์ธ๋ฒ์งธ, ํฉํ ๋ฆฌ ๋ฉ์๋์ธ createSopt๊ฐ ์ถ์ ๋ฉ์๋์ด์ protected๋ก ์ ๊ทผ์ ์ด์๊ฐ ์ค์ ๋ ์ด์ ๋
์ถ์ ๋ฉ์๋๋ก ์ ์ธํจ์ผ๋ก์จ ๋ชจ๋ ์๋ธ ํด๋์ค๊ฐ ์ด ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ๊ตฌํํ๋๋ก ์๊ตฌํ ์ ์๊ณ ,
protected๋ก ์ค์ ํจ์ผ๋ก์ ์๋ธ ํด๋์ค์ ๊ฐ์ ํจํค์ง์ ํด๋์ค์ ์ํด์๋ง ์ ๊ทผ๋ ์ ์์ผ๋ฉฐ, API ์ฌ์ฉ์๋ createSopt์ ์กด์ฌ ์์ฒด๋ฅผ ์๋ ค์ฃผ์ง ์๊ณ createOperation ๊ณต๊ฐ ๋ฉ์๋๋ฅผ ํตํด ๊ฐ์ฒด๋ฅผ ์์ฒญํ๊ฒ ๋๋ค๋ ์ฅ์ ์ด ์๋ค.
public class Client {
public static void main(String[] args) {
// ๊ฐ ํ์
์ ํฉํ ๋ฆฌ ์ธ์คํด์ค ์์ฑ
SoptFactory serverFactory = new ServerFactory();
SoptFactory iosFactory = new IosFactory();
// ์๋ฒ ๊ฐ์ฒด ์์ฑ ๋ฐ ์ฌ์ฉ
Sopt server = serverFactory.createOperation();
server.printLanguage();
// iOS ๊ฐ์ฒด ์์ฑ ๋ฐ ์ฌ์ฉ
Sopt ios = iosFactory.createOperation();
ios.printLanguage();
}
}
ํด๋ผ์ด์ธํธ ์ฝ๋์ด๋ค.
ํด๋ผ์ด์ธํธ ์ฝ๋์์ Server์ Ios ํด๋์ค์ ๋ํ ์์กด์ฑ ์์ด ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
์์กด์ฑ ์ฃผ์ ์ ์ฌ์ฉํด์ ์ธ๋ถ์์ Factory ํด๋์ค๋ฅผ ๋ฐ์์จ๋ค๋ฉด ServerFactory(), IosFactory() ์ ๋ํ ์์กด์ฑ๋ ์ ๊ฑฐ๊ฐ ๊ฐ๋ฅํ๋ค.
ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ ์ฅ์ ์ ํ์ฅํ ๋ ๊ธฐ์กด ์ฝ๋์ ๋ณ๊ฒฝ์ด ์์ด๋ ๋๋ค๋ ์ ์ด๋ผ๊ณ ์์ ๋งํ๋ค.
์ด๋ฒ์๋ Android ํํธ๊ฐ ์ถ๊ฐ๋๋ค๊ณ ๊ฐ์ ํ์
Simple Factory์์๋ SoptFactory์ create ๋ฉ์๋๋ฅผ ์์ ํด์ผ ํ๋ค.
๊น๋นกํ๊ณ switch-case๋ฅผ ๋ฃ์ง ์์๋ค๋ฉด, ์ฝ๋์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ณ Enum์ ์ค์ ํด ๋์๋ IllegalArgumentException๋ก ์ด๋์ ๋ ๋ฐฉ์ดํ ์๋ ์์ง๋ง, ์์ ์๋ ์ด๋ ค์๋ค๋ ๋จ์ ์ ๋ณํ์ง ์๋๋ค.
ํ์ง๋ง ์ฐ๋ฆฌ๋ Factory Method ํจํด์ ์ ์ฉํ๊ธฐ ๋๋ฌธ์, ๊ธฐ์กด SoptFactory์ ์์ ์์ด ์๋ก์ด ์ฝ๋๋ฅผ ์ถ๊ฐ๋ง ํ๋ฉด ๋๋ค!!
public interface Sopt {
void printLanguage();
}
public class Server implements Sopt {
@Override
public void printLanguage() {
System.out.println("Java");
}
}
public class Ios implements Sopt {
@Override
public void printLanguage() {
System.out.println("Swift");
}
}
public class Android implements Sopt { // ์ถ๊ฐ
@Override
public void printLanguage() {
System.out.println("Kotlin");
}
}
Sopt ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ Android ํด๋์ค ์ถ๊ฐ
public abstract class SoptFactory {
public final Sopt createOperation() {
Sopt sopt = createSopt(); // ์๋ธ ํด๋์ค์์ ๊ตฌ์ฒดํํ ํฉํ ๋ฆฌ ๋ฉ์๋ ์คํ
sopt.setup(); // .. ์ด๋ฐ์ ๊ฐ์ฒด ์์ฑ์ ๊ฐ๋ฏธํ ๋ก์ง ์คํ
return sopt; // ์์ฑ๋ ๊ฐ์ฒด ๋ฐํ
}
protected abstract Sopt createSopt();
}
public class ServerFactory extends SoptFactory {
@Override
public Sopt createSopt() {
return new Server();
}
}
public class IosFactory extends SoptFactory {
@Override
public Sopt createSopt() {
return new Ios();
}
}
public class AndroidFactory extends SoptFactory { // ์ถ๊ฐ
@Override
protected Sopt createSopt() {
return new Android();
}
}
Android ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ AndroidFactory ์ ์
public class Client {
public static void main(String[] args) {
// ๊ฐ ํ์
์ ํฉํ ๋ฆฌ ์ธ์คํด์ค ์์ฑ
SoptFactory serverFactory = new ServerFactory();
SoptFactory iosFactory = new IosFactory();
SoptFactory androidFactory = new AndroidFactory();
// ์๋ฒ ๊ฐ์ฒด ์์ฑ ๋ฐ ์ฌ์ฉ
Sopt server = serverFactory.createOperation();
server.printLanguage();
// iOS ๊ฐ์ฒด ์์ฑ ๋ฐ ์ฌ์ฉ
Sopt ios = iosFactory.createOperation();
ios.printLanguage();
// Andorid ๊ฐ์ฒด ์์ฑ ๋ฐ ์ฌ์ฉ
Sopt android = androidFactory.createOperation();
android.printLanguage();
}
}
์ด๋ ๊ฒ ์ต์์ ๊ณต์ฅ ํด๋์ค์ ์ฝ๋ ์์ ์์ด ์๋ก์ด ๊ฐ์ฒด ํ์ ์ ์ถ๊ฐํ๊ธฐ ์ํด ์๋ก์ด ํฉํ ๋ฆฌ ํด๋์ค๋ง ์ถ๊ฐํ๋ฉด ๋๊ณ ๊ธฐ์กด ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ ํ์๊ฐ ์๋ค. ์ด๋ OCP๋ฅผ ์ ์ค์ํ๋ค๊ณ ๋ณผ ์ ์๋ค.
์ด๋ ๊ฒ ์์กด๊ด๊ณ๋ฅผ ๊ฐ๊ฒ ๋๋ค.
- ์ฅ์ : Factory Method ํจํด์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ ์ง๊ธ๊น์ง ๋ณธ ๊ฒ์ฒ๋ผย ์์ ์ ๋ซํ์๊ณ ํ์ฅ์๋ ์ด๋ ค์๋ OCP ์์น์ ์งํฌ ์ ์๋ค๋ ์ ์ด๋ค.
- ๋จ์ : ๊ฐ๋จํ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ๋๋ณด๋ค ๋ง์ ํด๋์ค๋ฅผ ์ ์ํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ฝ๋๋์ด ์ฆ๊ฐํ๋ค.