Your SlideShare is downloading. ×
Design Pattern with Burger
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Design Pattern with Burger

475

Published on

PT.BUZOO INDONESIA is No1 Japanese offshore development company in Indonesia. …

PT.BUZOO INDONESIA is No1 Japanese offshore development company in Indonesia.
We are professional of web solution and smartphone apps. We can support Japanese, English and Indonesia.
We are hiring now at http://buzoo.co.id/

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
475
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
8
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. By Irwan Fathurrahman
  • 2. Order Burger prepare bake presentation filling
  • 3. 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; }
  • 4. But our store just have one kind of Burger! We have to create 4 menu!
  • 5. 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; }
  • 6. But! What about we add new burger and remove old Burger?
  • 7. 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; }
  • 8. 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; } } } }
  • 9. Now, we can sell our Burgers!
  • 10. Oh Look! Our store expands! Now we have 2 store, but have different menu
  • 11. Back to old solution? Because our both store have different menu, we can’t have same factory
  • 12. 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; } } } }
  • 13. Solution? What about we make every store decide their own menu?
  • 14. 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); } } }
  • 15. But we have other problem! Customer want other style of vegetable and beef too.
  • 16. 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(){}; }
  • 17. 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(); } }
  • 18. 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; } } }
  • 19. 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. 
  • 20. Review  All factory pattern promote loose coupling by reducing the depedency on concrete class. Concrete class is class which will be an object concrete.
  • 21. 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.
  • 22. 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.
  • 23. Design Principle  Code for an interface, not code for and implementation.
  • 24. Design Principle  Depend upon abstraction, don’t upon concrete class.

×