Top Banner
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE)
31

BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Dec 31, 2015

Download

Documents

Oscar Maxwell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

BUILDER, MEDIATOR, AND DECORATOR

Team 2 (Eli.SE)

Page 2: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

BUILDER

Page 3: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Builder Pattern

Multiple objects can be constructed with the same interface

Similar to Factory Pattern Focuses on building items step-by-step

Page 4: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Class Diagram

Page 5: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Pizza

Multiple different types, same process Build dough Build sauce Build toppings

Page 6: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

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();

}

Page 7: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

HawaiianPizzaBuilder

class HawaiianPizzaBuilder extends PizzaBuilder {

public void buildDough() {

pizza.setDough("cross");

}

public void buildSauce() {

pizza.setSauce("mild");

}

public void buildToppings() {

pizza.setToppings("ham+pineapple");

}

}

Page 8: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Cook

class Cook {

private PizzaBuilder pizzaBuilder;

public void setPizzaBuilder(PizzaBuilder pb) {

pizzaBuilder = pb;

}

public Pizza getPizza() {

return pizzaBuilder.getPizza();

}

public void constructPizza() {

pizzaBuilder.createNewPizzaProduct();

pizzaBuilder.buildDough();

pizzaBuilder.buildSauce();

pizzaBuilder.buildToppings();

}

}

Page 9: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

BuilderExample

class BuilderExample {

public static void main(String[] args) {

Cook cook = Cook.new();

HawaiianPizzaBuilder hawaiianBuilder = new HawaiianPizzaBuilder();

SpicyPizzaBuilder spicyBuilder = new SpicyPizzaBuilder();

Pizza hawaiianPizza;

Pizza spicyPizza;

cook.setPizzaBuilder(hawaiianBuilder);

cook.constructPizza();

hawaiianPizza = cook.getPizza();

cook.setPizzaBuilder(spicyBuilder);

cook.constructPizza();

spicyPizza = cook.getPizza();

}

}

Page 10: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Why this over factory pattern? Individual steps may not be used

Hold the pickles, extra mustard

Page 11: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Sources

http://en.wikipedia.org/wiki/Builder_pattern

http://sourcemaking.com/design_patterns/builder

Page 12: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

MEDIATOR DESIGN PATTERN

Page 13: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

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

Page 14: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Real World Example

Page 15: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Diagram

Page 16: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Advantages

Reduces coupling in situations when many objects need to communicate

Minimizes “spaghetti code” by localizing the communication process to just one class

Page 17: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Disadvantages

Easy for mediators to become complex in practice

Page 18: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
Page 19: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
Page 20: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

DECORATOR PATTERN

Raul Aragonez

Page 21: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

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.

Page 22: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

UML Diagram

Page 23: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

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.

Page 24: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Pizza Example

Page 25: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Class Explosion

Page 26: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Instead…

Page 27: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

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.

Page 28: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Pizza example Renovation

Page 29: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

Lets try it!

Page 30: BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).

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.