SlideShare a Scribd company logo
1 of 25
By Irwan Fathurrahman
Order Burger

prepare

bake

presentation

filling
Public Burger orderBurger(){
Burger burger = new Burger();
burger.prepare();
burger.bake();
burger.filling();
burger.presentation();

Our store can take
and give the order.
This is the simple way
for our function to
take order based of
strategy in previous
slide

return burger;

}
But our store just have
one kind of Burger!
We have to create 4 menu!
Public Burger orderBurger(String type){
Burger burger;
if(type == “cheese”){
burger = new CheeseBurger();
} else if(type == “double cheese”){
burger = new DoubleCheeseBurger();
} else if(type == “beef”){
burger = new BeefBurger();
} else if(type == “doublebeef”){
burger = new DoubleBeefBurger();
}

That it!
Our store can take 4
kind of Burger.
- Cheese Burger
- Double Cheese
Burger
- Beef Burger
- Double Beef Burger

burger.prepare();
burger.bake();
burger.filling();
burger.presentation();
return burger;
}
But! What about we
add new burger and
remove old Burger?
Public Burger orderBurger(String type){
Burger burger;
if(type == “cheese”){
burger = new CheeseBurger();
} else if(type == “double cheese”){
burger = new DoubleCheeseBurger();
} else if(type == “beef”){
burger = new BeefBurger();
} else if(type == “doublebeef”){
burger = new DoubleBeefBurger();
} else if(type == “chesse&beef”){
burger = new CheeseBeefBurger();
}

It mess our order
burger method and
make order burger
closed modification

burger.prepare();
burger.bake();
burger.filling();
burger.presentation();
return burger;
}
SOLUTION! -> simple factory
Make simple factory that can
produce one kind burger.

Public class BurgerStore{
BurgerFactory factory;
Public BurgerStore(BurgerFactory factory){
this.factory = factory;
}
Public Burger orderBurger(String type){
Burger burger = factory.createBurger(type);
burger.prepare();
burger.bake();
burger.filling();
burger.presentation();

Public class BurgerFactory{
return burger;
Public Burger createBurger(String type){
Burger burger;
if(type == “cheese”){
burger = new CheeseBurger();
} else if(type == “beef”){
burger = new BeefBurger();
} else if(type == “doublebeef”){
burger = new DoubleBeefBurger();
} else if(type == “chesse&beef”){
burger = new CheeseBeefBurger();
}
return burger;

}
}

}

}
Now, we can sell our Burgers!
Oh Look! Our store expands!
Now we have 2 store, but have different menu
Back to old solution?
Because our both store have different menu, we can’t
have same factory
Add 2 factory
- It can be a solution. Each store can choose their own factory.
- But if other store share one factory and suddenly add their own menu, it
can be a problem later.

Public class BurgerFactoryStore1{

Public class BurgerFactoryStore2{

Public Burger createBurger(String type){
Burger burger;

Public Burger createBurger(String type){
Burger burger;

if(type == “cheese”){
burger = new CheeseBurger();
} else if(type == “beef”){
burger = new BeefBurger();
} else if(type == “doublebeef”){
burger = new DoubleBeefBurger();
} else if(type == “chesse&beef”){
burger = new CheeseBeefBurger();
}

if(type == “cheese”){
burger = new CheeseBurger();
} else if(type == “doublecheese”){
burger = new DoubleCheeseBurger();
} else if(type == “beef”){
burger = new BeefBurger();
} else if(type == “doublebeef”){
burger = new DoubleBeefBurger();
}
return burger;

return burger;

}
}

}

}
Solution?
What about we make every store decide their own
menu?
SOLUTION! -> factory method

Public class Store1 extends BurgerStore{
Public Burger createBurger(String type){
Burger burger;

We just make interface for our store, but
they can decide their own menu.

Public abstract class BurgerStore{
BurgerFactory factory;

if(type == “cheese”){
burger = new CheeseBurger();
} else if(type == “beef”){
burger = new BeefBurger();
}
return burger;
}
}

Public BurgerStore(BurgerFactory factory){
this.factory = factory;
}
Public Burger orderBurger(String type){
Burger burger = createBurger(type);

Public class Store2 extends BurgerStore{
Public Burger createBurger(String type){
Burger burger;

burger.prepare();
burger.bake();
burger.filling();
burger.presentation();

if(type == “beef”){
burger = new BeefBurger();
} else if(type == “doublebeef”){
burger = new DoubleBeefBurger();
}
return burger;

return burger;
}
Public abstract Burger createBurger
(String type);
}

}
}
But we have other problem!
Customer want other style of vegetable and beef too.
Look our Burger

One of our kind of Burger

Public abstract class Burger{
Beef factory;
Vegetable vegetable;
Public abstract void prepare();
Public void bake(){
// some method
}
Public void filling(){
// some method
}

Public class ChesseBburger extends Burger{
Public void ChesseBburger(// need ingredient){

Public void presentation(){
// some method
}

}
}
Public void prepare(){};
}
This our ingredient creator
Public interface IngredientFactory {

Need ingredient factory that create
beef and vegetable

Public class IndonesianBurgerIngredientFactory {

Public Beef createBeef();
Public Vegetable createVegetable();
}

Public class JapaneseBurgerIngredientFactory {

Public Beef createBeef(){
return new SapiLadaHitam();
}
Public Vegetable createVegetable(){
return new Kangkung();
}
}

Public Beef createBeef(){
return new FreshFish();
}
Public Vegetable createVegetable(){
return new Nori();
}
}
Solution! -> Abstract Factory Method
Now we can take ingredient style
customer want without affecting
our method in burger.

Public class ChesseBburger extends Burger{
IngredientFactory ingredient;
Public void ChesseBburger(
IngredientFactory ingredient){
this.Ingredient = ingredient;
}
Public void prepare(){
ingredient.createBeef();
ingredient.createVegetable();
};

Public class Store1 extends BurgerStore{
Public Burger createBurger(String type,
IngredientFactory ingredient){
Burger burger;
if(type == “cheese”){
burger = new CheeseBurger(ingredient);
} else if(type == “beef”){
burger = new BeefBurger(ingredient);
}
return burger;
}
}

}
Review
Simple factory is a simple way to decouple our
client from concrete class.
 Factory Method relies on inheritance : object
creation is delegated to subclasses which
implement the factory method to create object.
 Abstract factory Method relies on object
composition : ocject creation is implemented in
method exposed in factory interface.

Review


All factory pattern promote loose coupling by
reducing the depedency on concrete class.

Concrete class is class which will be
an object concrete.
Design Pattern




Is a description or template for how to solve a
problem that can be used in many different
situations.
Design patterns are optimized, reusable solutions
to the programming problems that we encounter
every day.
Design Principle



Take the parts that vary and encapsulate them.
So, we can can alter or extend that vary later
without affecting those that don’t.
Design Principle


Code for an interface, not code for and
implementation.
Design Principle


Depend upon abstraction, don’t upon concrete
class.

More Related Content

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
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 ChatGPTExpeed 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 EngineeringsPixeldarts
 
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 HealthThinkNow
 
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.pdfmarketingartwork
 
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 2024Neil 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 2024Albert 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 InsightsKurio // 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 2024Search 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 summarySpeakerHub
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit 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 managementMindGenius
 
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
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
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...
 

Design Pattern with Burger

  • 2.
  • 4. Public Burger orderBurger(){ Burger burger = new Burger(); burger.prepare(); burger.bake(); burger.filling(); burger.presentation(); Our store can take and give the order. This is the simple way for our function to take order based of strategy in previous slide return burger; }
  • 5. But our store just have one kind of Burger! We have to create 4 menu!
  • 6. Public Burger orderBurger(String type){ Burger burger; if(type == “cheese”){ burger = new CheeseBurger(); } else if(type == “double cheese”){ burger = new DoubleCheeseBurger(); } else if(type == “beef”){ burger = new BeefBurger(); } else if(type == “doublebeef”){ burger = new DoubleBeefBurger(); } That it! Our store can take 4 kind of Burger. - Cheese Burger - Double Cheese Burger - Beef Burger - Double Beef Burger burger.prepare(); burger.bake(); burger.filling(); burger.presentation(); return burger; }
  • 7. But! What about we add new burger and remove old Burger?
  • 8. Public Burger orderBurger(String type){ Burger burger; if(type == “cheese”){ burger = new CheeseBurger(); } else if(type == “double cheese”){ burger = new DoubleCheeseBurger(); } else if(type == “beef”){ burger = new BeefBurger(); } else if(type == “doublebeef”){ burger = new DoubleBeefBurger(); } else if(type == “chesse&beef”){ burger = new CheeseBeefBurger(); } It mess our order burger method and make order burger closed modification burger.prepare(); burger.bake(); burger.filling(); burger.presentation(); return burger; }
  • 9. SOLUTION! -> simple factory Make simple factory that can produce one kind burger. Public class BurgerStore{ BurgerFactory factory; Public BurgerStore(BurgerFactory factory){ this.factory = factory; } Public Burger orderBurger(String type){ Burger burger = factory.createBurger(type); burger.prepare(); burger.bake(); burger.filling(); burger.presentation(); Public class BurgerFactory{ return burger; Public Burger createBurger(String type){ Burger burger; if(type == “cheese”){ burger = new CheeseBurger(); } else if(type == “beef”){ burger = new BeefBurger(); } else if(type == “doublebeef”){ burger = new DoubleBeefBurger(); } else if(type == “chesse&beef”){ burger = new CheeseBeefBurger(); } return burger; } } } }
  • 10. Now, we can sell our Burgers!
  • 11. Oh Look! Our store expands! Now we have 2 store, but have different menu
  • 12. Back to old solution? Because our both store have different menu, we can’t have same factory
  • 13. Add 2 factory - It can be a solution. Each store can choose their own factory. - But if other store share one factory and suddenly add their own menu, it can be a problem later. Public class BurgerFactoryStore1{ Public class BurgerFactoryStore2{ Public Burger createBurger(String type){ Burger burger; Public Burger createBurger(String type){ Burger burger; if(type == “cheese”){ burger = new CheeseBurger(); } else if(type == “beef”){ burger = new BeefBurger(); } else if(type == “doublebeef”){ burger = new DoubleBeefBurger(); } else if(type == “chesse&beef”){ burger = new CheeseBeefBurger(); } if(type == “cheese”){ burger = new CheeseBurger(); } else if(type == “doublecheese”){ burger = new DoubleCheeseBurger(); } else if(type == “beef”){ burger = new BeefBurger(); } else if(type == “doublebeef”){ burger = new DoubleBeefBurger(); } return burger; return burger; } } } }
  • 14. Solution? What about we make every store decide their own menu?
  • 15. SOLUTION! -> factory method Public class Store1 extends BurgerStore{ Public Burger createBurger(String type){ Burger burger; We just make interface for our store, but they can decide their own menu. Public abstract class BurgerStore{ BurgerFactory factory; if(type == “cheese”){ burger = new CheeseBurger(); } else if(type == “beef”){ burger = new BeefBurger(); } return burger; } } Public BurgerStore(BurgerFactory factory){ this.factory = factory; } Public Burger orderBurger(String type){ Burger burger = createBurger(type); Public class Store2 extends BurgerStore{ Public Burger createBurger(String type){ Burger burger; burger.prepare(); burger.bake(); burger.filling(); burger.presentation(); if(type == “beef”){ burger = new BeefBurger(); } else if(type == “doublebeef”){ burger = new DoubleBeefBurger(); } return burger; return burger; } Public abstract Burger createBurger (String type); } } }
  • 16. But we have other problem! Customer want other style of vegetable and beef too.
  • 17. Look our Burger One of our kind of Burger Public abstract class Burger{ Beef factory; Vegetable vegetable; Public abstract void prepare(); Public void bake(){ // some method } Public void filling(){ // some method } Public class ChesseBburger extends Burger{ Public void ChesseBburger(// need ingredient){ Public void presentation(){ // some method } } } Public void prepare(){}; }
  • 18. This our ingredient creator Public interface IngredientFactory { Need ingredient factory that create beef and vegetable Public class IndonesianBurgerIngredientFactory { Public Beef createBeef(); Public Vegetable createVegetable(); } Public class JapaneseBurgerIngredientFactory { Public Beef createBeef(){ return new SapiLadaHitam(); } Public Vegetable createVegetable(){ return new Kangkung(); } } Public Beef createBeef(){ return new FreshFish(); } Public Vegetable createVegetable(){ return new Nori(); } }
  • 19. Solution! -> Abstract Factory Method Now we can take ingredient style customer want without affecting our method in burger. Public class ChesseBburger extends Burger{ IngredientFactory ingredient; Public void ChesseBburger( IngredientFactory ingredient){ this.Ingredient = ingredient; } Public void prepare(){ ingredient.createBeef(); ingredient.createVegetable(); }; Public class Store1 extends BurgerStore{ Public Burger createBurger(String type, IngredientFactory ingredient){ Burger burger; if(type == “cheese”){ burger = new CheeseBurger(ingredient); } else if(type == “beef”){ burger = new BeefBurger(ingredient); } return burger; } } }
  • 20. Review Simple factory is a simple way to decouple our client from concrete class.  Factory Method relies on inheritance : object creation is delegated to subclasses which implement the factory method to create object.  Abstract factory Method relies on object composition : ocject creation is implemented in method exposed in factory interface. 
  • 21. Review  All factory pattern promote loose coupling by reducing the depedency on concrete class. Concrete class is class which will be an object concrete.
  • 22. Design Pattern   Is a description or template for how to solve a problem that can be used in many different situations. Design patterns are optimized, reusable solutions to the programming problems that we encounter every day.
  • 23. Design Principle   Take the parts that vary and encapsulate them. So, we can can alter or extend that vary later without affecting those that don’t.
  • 24. Design Principle  Code for an interface, not code for and implementation.
  • 25. Design Principle  Depend upon abstraction, don’t upon concrete class.