SlideShare a Scribd company logo
*
Design Pattern Study 4 번째
팩토리 패턴(Factory Pattern)
*index 1. Simple Factory
2. Factory Method Pattern
3. Abstract Factory Pattern
• 본 PPT는 Design Patterns Study를 위해 작성되었습니다.
• Study에 사용한 교재는 한빛미디어에서 나온
• [Head First Desgin Patterns]을 사용하였습니다.
• Written by 서울시립대 이희태 For study with
NHN NEXT2기 조현호
서 울 시 립 대 이 희 태
1. Simple Factory
라면 가게를 차려보자!
RamyunStore.orderRamyun()
RamyunStore.orderRamyun()
떡라면을 메뉴에서 제외
하고
소시지라면을 신메뉴로
넣으려면??
기존의 orderRamyun()
코드를 일일이 뜯어고
친다.
라면 만드는 부분을 캡슐화하면 어떨까?
Simple
RamyunFactory
라면 객체 생성 코드 부
분만 따로 빼서
주문(orderRamyun)과
제작(createRamyun)을
분리해보자!
SimpleRamyunFactory
SimpleFactory 구조
createRamyun()
SimpleRamyunFactory
orderRamyun()
RamyunStore
CheeseRamyun
prepare()
boil()
putIntoBowl()
Ramyun
SpicyRamyun
SausageRamyun
SquidRamyun
팩토리를 사용하는
client
Static메소드로 선
언하는 경우 많음
구체적인 라면 객체
를 생성하는 factory
팩토리에서 만들 상품
(추상클래스)
팩토리에서 생산하는
구체적인 상품들(구체적인 라면)
Has-A관계
Is-A관계
2. Factory Method Pattern
라면 프랜차이즈 회사
NS라면
Ottug라면
라면가게
거대 라면회사들이 직접 라면가게 프렌차이즈
사업을 하고 싶어합니다!
매운 맛이
일품
오 뚝
부드러운
면발!
Factory Method의 필요성
기존 RamyunStore의 프레임을 그대로 유지하면서 각 프랜차이
즈의 다양한 라면을 만드는 방법은 없을까요?
type이 cheese일때 상황에 따라
NScheese라면/ Ottugcheese라면
유연하게 변할 수 있어야…
orderRamyun()의 프레임인
create->prepare
->boil->putIntoBowl
의 과정은 그대로 유지해야
…
심하게 의존적인 라면가게
Factory Method
createRamyun에서 실제 생성되는
객체가 무엇인지 모르게 할 것.
추상메소드로 선언,
subclass에서 객체
생성
매개 변수를 통해 만들 객체
의 종류 선택
Concrete RamyunStore
< 라면이 만들어지는 과정 >
1. RamyunStore nsRamyunStore = new
NSRamyunStore();
2. nsRamyunStore.orderRamyun(“cheese”);
3. Ramyun ramyun =
createRamyun(“cheese”);
4. ramyun.prepare();
ramyun.boil();
ramyun.putIntoBowl();
라면 클래스
NSStyleCheeseRamyun VS OttugStyleCheeseRamyun
RamyunTestDrive
Console 출력
병렬 클래스 계층구조
String name;
String noodle;
String sauce;
ArrayList<String> toppings;
Ramyun
NSStyleCheeseRamyun
createRamyun()
NSRamyunStore
createRamyun() ←abstract
orderRamyun()
…
RamyunStore
NSStyleSausageRamyun
NSStyleSpicyRamyun
OttugStyleCheeseRamyun
OttugStyleSausageRamyun
OttugStyleSpicyRamyun
createRamyun()
OttugRamyunStore
병렬적 구조
Factory Method Pattern 정의
팩토리 메소드 패턴(Factory Method Pattern)
Def. 팩토리 메소드 패턴에서는 객체를 생성하기 위한 인터페이스를 정의하는
데, 어떤 클래스의 인스턴스를 만들지는 서브클래스에서 결정한다. 팩토리
메소드 패턴을 이용하면 클래스의 인스턴스를 만드는 일을 서브클래스에게
맡길 수 있다.
Factory Method Pattern UML
String name;
String noodle;
String sauce;
ArrayList<String> toppings;
void prepare()
void boil()
Void putIntoBowl()
Ramyun
NSStyle
CheeseRamyun
createRamyun() ←abstract
orderRamyun()
…
RamyunStore
OttugStyle
CheeseRamyun
//return new
OttugStyleRamyun()
createRamyun()
OttugRamyunStore
//return new
OttugStyleRamyun()
createRamyun()
NSRamyunStore
객체 의존성
RamyunStore
NSStyle
CheeseRamyun
NSStyle
SausageRamyun
NSStyle
SquidRamyun
NSStyle
SpicyRamyun
NSStyle
RicecakeRamyun
NSStyle
CheeseRamyun
NSStyle
SausageRamyun
NSStyle
SquidRamyun
NSStyle
SpicyRamyun
NSStyle
RicecakeRamyun
RamyunStore
Ramyun
디자인 원칙 6.
추상화된 것에 의존하도록 만들어라.
구상 클래스에 의존하도록 만들지 않도록 한다.
의존성 뒤집기(Dependency Inversion Principle)
>>> Guide Line
1. 어떤 변수에도 구상 클래스에 대한 레퍼런스를 저장하지 않는다.
2. 구상 클래스에서 파생된 클래스를 만들지 않는다.
3. 수퍼클래스에 이미 구현되어 있는 메소드를 Override하지 않는다.
않는다.
3. Abstract Factory Pattern
원재료군(families of Ingredients)을 처리하는 방법?
오 뚝
NS라면가게에서
파는 라면이라면
굵은 면발에 맵고 짠 스프에 굵은 대파를
사용한다.
Ottug라면가게에서
파는 라면이라면 가는 면발에 마일드한 스프에 싱싱한 쪽파를
사용한다.
원재료 공장
오 뚝 무조건
이것들이 들어가는데….
Ottug라면이라면
원재료를 이렇게
각각 생성할 필요
가 있을까?
추상 팩토리에 대한 idea
어떤 라면가게인지와는 상관없이, 라면요리에 원료가 되는 면, 스프, 채소,
달걀 등등을 한번에 처리해주는 공장이 있으면 좋을 텐데……
Ramyun
Store
CASE 1: ramyunStore == NS CASE 2: ramyunStore == Ottug
Create Ramyun!
소제목 스타일 편집
☞ Abstract Factory(추상 팩토리)
라면클래스에서 원재료 팩토리가 인터페이스와 맞기만 하면 어떤 팩토리를 사
용하던 상관 없다. SimpleFactory처럼 팩토리를 사용하되, Abstract 클래스로 만
들어서 “다형성”을 활용한다.
Ottug이든 NS이든 상관없이,
라면공장이라면 면,스프,채소는 원료를 통해 라면을 생산한다!
Source code – Ramyun 추상클래스 (1)
Source code – Ramyun 추상클래스 (2)
Source code – 라면 원료…
Source code – RamyunIngredientFactory 인터페이스
Source code – Ottug’s and NS’s RamyunIngredientFactory
Source code – Concrete Ramyun class. (SausageRamyun)
Source code – RamyunStore. (팩토리메소드 때와 변함 없음)
Source code - NSRamyunStore
Source code - OttugRamyunStore
Abstract Factory Pattern 정의
추상 팩토리 패턴(Abstract Factroy Pattern)
Def. 추상 팩토리 패턴에서는 인터페이스를 이용하여 서로 연관된, 또는
의존하는 객체를 구상 클래스를 지정하지 않고도 생성할 수 있다.
RamyunIngredientFactory UML
NSStyle
IngredientFactory
OttugStyle
IngredientFactory
public Noodle createNoodle();
public Sauce createSauce();
public Egg createEgg();
public Veggie createVeggie();
public Cheese createCheese();
…
RamyunIngredientFactory
interface class
Thick Noodle
Noodle
Sauce
Cheese
Thin Noodle
Mild OttugSauceHot NS Sauce
Processed Cheese Mozzarella Cheese
CreateRamyun(Strin
g item)
NSRamyunStore
CreateRamyun(Strin
g item)
OttugRamyunStore
Ramyun
Thank you!!
*

More Related Content

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Design pattern study 4 factory pattern _ by dragor0123