1 / 31

Builder, mediator, and decorator

Builder, mediator, and decorator . Team 2 ( Eli.SE ). Builder. Builder Pattern. Multiple objects can be constructed with the same interface Similar to Factory Pattern Focuses on building items step-by-step. Class Diagram. Pizza. Multiple different types, same process Build dough

amora
Download Presentation

Builder, mediator, and decorator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Builder, mediator, and decorator Team 2 (Eli.SE)

  2. Builder

  3. Builder Pattern • Multiple objects can be constructed with the same interface • Similar to Factory Pattern • Focuses on building items step-by-step

  4. Class Diagram

  5. Pizza • Multiple different types, same process • Build dough • Build sauce • Build toppings

  6. PizzaBuilder abstract class PizzaBuilder { protected Pizza pizza // has 3 attributes: dough, sauce, toppings public Pizza getPizza() { return pizza; } public void createNewPizzaProduct() { pizza = new Pizza(); } abstract public void buildDough(); abstract public void buildSauce(); abstract public void buildToppings(); }

  7. HawaiianPizzaBuilder class HawaiianPizzaBuilder extends PizzaBuilder { public void buildDough() { pizza.setDough("cross"); } public void buildSauce() { pizza.setSauce("mild"); } public void buildToppings() { pizza.setToppings("ham+pineapple"); } }

  8. Cook class Cook { private PizzaBuilderpizzaBuilder; public void setPizzaBuilder(PizzaBuilderpb) { pizzaBuilder = pb; } public Pizza getPizza() { return pizzaBuilder.getPizza(); } public void constructPizza() { pizzaBuilder.createNewPizzaProduct(); pizzaBuilder.buildDough(); pizzaBuilder.buildSauce(); pizzaBuilder.buildToppings(); } }

  9. BuilderExample class BuilderExample { public static void main(String[] args) { Cook cook = Cook.new(); HawaiianPizzaBuilderhawaiianBuilder = new HawaiianPizzaBuilder(); SpicyPizzaBuilderspicyBuilder = new SpicyPizzaBuilder(); Pizza hawaiianPizza; Pizza spicyPizza; cook.setPizzaBuilder(hawaiianBuilder); cook.constructPizza(); hawaiianPizza = cook.getPizza(); cook.setPizzaBuilder(spicyBuilder); cook.constructPizza(); spicyPizza = cook.getPizza(); } }

  10. Why this over factory pattern? • Individual steps may not be used • Hold the pickles, extra mustard

  11. Sources • http://en.wikipedia.org/wiki/Builder_pattern • http://sourcemaking.com/design_patterns/builder

  12. Mediator Design Pattern

  13. Overview of Mediator • Defines an object that manages how a set of objects interact • Objects interact with the mediator object rather than with each other

  14. Real World Example

  15. Diagram

  16. Advantages • Reduces coupling in situations when many objects need to communicate • Minimizes “spaghetti code” by localizing the communication process to just one class

  17. Disadvantages • Easy for mediators to become complex in practice

  18. Decorator Pattern Raul Aragonez

  19. Intent • Attach additional responsibilities to an object dynamically. • Decorators provide a flexible alternative to subclassing for extending functionality. • Client-specified embellishment of a core object by recursively wrapping it.

  20. UML Diagram

  21. Participants • Component:defines the interface for objects that can have responsibilities added to them dynamically. • Concrete Component:is the object that can be decorated. It is defined so that zero or more responsibilities can be attached to it. • Decorator: maintains a reference to a Component object an interface that conforms to Component's interface. • Concrete Decorator:wraps around the Concrete Component and adds functionality to it.

  22. Pizza Example

  23. Class Explosion

  24. Instead…

  25. Advantages • Responsibilities can be added to one object without affecting the rest of the objects in the class. • Contents of the object are not affected and Responsibilities can be removed as easily as they are added. • Lots of Features = slow system performance = unnecessary code we need to support. We are paying for features that we don't need! • Therefore, Decorator lets us add features and responsibilities as we need them at run-time.

  26. Pizza example Renovation

  27. Lets try it!

  28. Disadvantages • Lots of little objects •  As a system gets larger it would become composed of many little objects that look the same, decorator objects. This makes it hard to learn the system and debug the code. • Maintenance of the Decorator code • when adding functionality to an object, there is no need to know the interface of the Decorator class. The Decorator must conform to the interface of the object. This means that there's always the need to maintain the interface to keep it synchronized with the object interface.

  29. Sources • http://www.giantflyingsaucer.com/blog/?p=1084 • http://sourcemaking.com/design_patterns/decorator • http://oreilly.com/catalog/hfdesignpat/chapter/ch03.pdf • http://pages.cpsc.ucalgary.ca/~jadalow/seng60904/decorator_paper.html#intent

More Related