forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Decorator] Added pizzas example and notes.
- Loading branch information
1 parent
413f9d4
commit 4018120
Showing
7 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |