Skip to content

Commit

Permalink
[Decorator] Added pizzas example and notes.
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Aug 12, 2015
1 parent 413f9d4 commit 4018120
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Composición de clases u objetos.

* [Composite](#composite)
* [Adapter](#adapter)
* [Decorator](#decorator)

### Patrones de Comportamiento (Behavioral Patterns)

Expand Down Expand Up @@ -177,3 +178,18 @@ Como cualquier adaptador en el mundo real este patrón se utiliza para ser una i

**Ejemplos:**
* [Books](https://github.com/LuisBurgos/design-patterns/tree/master/src/adapter/examples/books)

## <a name="decorator">Decorator</a> [&#8593;](#lista-de-patrones)

Extender la funcionalidad de los objetos se puede hacer de forma estática en nuestro código (tiempo de compilación) mediante el uso de la herencia, sin embargo, podría ser necesario extender la funcionalidad de un objeto de manera dinámica.

**Propósito:** Adjuntar responsabilidades adicionales a un objeto de forma **dinámica**. Los *decoradores* proporcionan una alternativa flexible para ampliar la funcionalidad.

**Aplicación:** Usamos el patrón [Decorator...](https://github.com/LuisBurgos/design-patterns/tree/master/src/decorator/pattern)
* Cuando necesitamos añadir o eliminar dinámicamente las responsabilidades a un objeto, sin afectar a otros objetos.
* Cuando queremos tener las ventajas de la *Herencia* pero ncesitemos añadir funcionalidad durante el tiempo de ejecución. Es más flexible que la *Herencia*,
* Simplificar el código agregando funcionalidades usando muchas clases diferentes.
* Evitar sobreescribir código viejo agregando, envés, código nuevo.

**Ejemplos:**
* [Pizzas](https://github.com/LuisBurgos/design-patterns/tree/master/src/decorator/examples/pizzas)
24 changes: 24 additions & 0 deletions src/decorator/examples/pizzas/Mozzarella.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package decorator.examples.pizzas;

/**
* Created by luisburgos on 11/08/15.
*/
public class Mozzarella extends ToppingDecorator {

public Mozzarella(Pizza temporalPizza) {
super(temporalPizza);
System.out.println("Adding Mozzarella");
}

@Override
public String getDescription() {
return temporalPizza.getDescription() + ", mozzarella";
}

@Override
public double getPrice() {
System.out.println("Price of mozarrella: " + .50);
return temporalPizza.getPrice() + .50;
}

}
9 changes: 9 additions & 0 deletions src/decorator/examples/pizzas/Pizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package decorator.examples.pizzas;

/**
* Created by luisburgos on 11/08/15.
*/
public interface Pizza {
public String getDescription();
public double getPrice();
}
16 changes: 16 additions & 0 deletions src/decorator/examples/pizzas/PizzaMaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package decorator.examples.pizzas;

/**
* Created by luisburgos on 11/08/15.
*/
public class PizzaMaker {

public static void main(String[] args) {

Pizza pizza = new Mozzarella(new TomatoSauce(new PlainPizza()));
System.out.println("Ingredients: " + pizza.getDescription());
System.out.println("Total Price: " + pizza.getPrice());

}

}
22 changes: 22 additions & 0 deletions src/decorator/examples/pizzas/PlainPizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package decorator.examples.pizzas;

/**
* Created by luisburgos on 11/08/15.
*/
public class PlainPizza implements Pizza {

public PlainPizza() {
System.out.println("Adding Thin Dough");
}

@Override
public String getDescription() {
return "Thin Dough";
}

@Override
public double getPrice() {
System.out.println("Price of thin dough: " + 4.00);
return 4.00;
}
}
25 changes: 25 additions & 0 deletions src/decorator/examples/pizzas/TomatoSauce.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package decorator.examples.pizzas;

/**
* Created by luisburgos on 11/08/15.
*/
public class TomatoSauce extends ToppingDecorator {

public TomatoSauce(Pizza temporalPizza) {
super(temporalPizza);
System.out.println("Adding Tomato Sauce");
}

@Override
public String getDescription() {
return temporalPizza.getDescription() + ", tomato sauce";
}

@Override
public double getPrice() {
System.out.println("Price of tomato sauce: " + 2.50);
return temporalPizza.getPrice() + 2.50;
}

}

23 changes: 23 additions & 0 deletions src/decorator/examples/pizzas/ToppingDecorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package decorator.examples.pizzas;

/**
* Created by luisburgos on 11/08/15.
*/
public abstract class ToppingDecorator implements Pizza {

protected Pizza temporalPizza;

public ToppingDecorator(Pizza temporalPizza) {
this.temporalPizza = temporalPizza;
}

@Override
public String getDescription() {
return temporalPizza.getDescription();
}

@Override
public double getPrice() {
return temporalPizza.getPrice();
}
}

0 comments on commit 4018120

Please sign in to comment.