Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator design pattern #26

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Decorator design pattern - AFTER CODE with cake decorator to avoid cr…
…eating class for each option.

Benefits
1. Lean code - as its not required to create class for each option
2. Price can be calculated dynamically
3. Performance improvement
4. Loose coupling.
5. Easy to update - price of can be updated at one place without touching other classes.
  • Loading branch information
premaseem committed Feb 27, 2018
commit fb2bf25e207e8bff9de13c262d67baa6fa06299a
26 changes: 23 additions & 3 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ public static void main (String[] args) {
Cake cake = new VanillaCake();
System.out.println("Plain cake price without decorations - "+ cake.getCost());

// Decorated Object with addons
cake = new CandyDecoration(cake);
System.out.println("Cake price with Candy decoration - "+ cake.getCost());

// Decorated Object with addons
// Fun side, price of Cartoon decoration is more then cake itself :-)
cake = new CartoonDecoration(cake);
cake = new CartoonDecoration(cake);
System.out.println("Cake price with double Cartoon decoration - "+cake.getCost());


/* TAKE AWAY:
1. Lean code - as its not required to create class for each option
2. Price can be calculated dynamically
3. Performance improvement
4. Loose coupling.
5. Easy to update - price of can be updated at one place without touching other classes.

*/

/* OLD DISCARDED CODE
// with decoration
cake = new VanillaCakeWithCandyDecoration();
System.out.println("Cake price with Candy decoration - "+ cake.getCost());
Expand All @@ -25,10 +46,9 @@ public static void main (String[] args) {
// with multiple decorations
cake = new VanillaCakeWithCandyAndCartoonDecoration();
System.out.println("Cake price with Candy and Cartoon decoration - "+ cake.getCost());
*/


/*
Take Away from Bad Code:
/*Take Away from Bad Code:
1. For each combination/ option there is a static class.
2. With less options its ok but in real cake software time application will
blow out of proportion due to so many classes.
Expand Down
54 changes: 54 additions & 0 deletions pattern/src/com/premaseem/Decoration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

public abstract class Decoration extends Cake{
Cake cake;

@Override
String getIngredients () {
return "decoration using different addon items";
}
}

class CandyDecoration extends Decoration{

Cake baseCake;

public CandyDecoration (Cake cake) {
baseCake = cake;
}

@Override
double getCost () {
return baseCake.getCost() + .12;
}

@Override
String getIngredients () {
return "decoration using candies";
}
}

class CartoonDecoration extends Decoration{

Cake baseCake;

public CartoonDecoration (Cake cake) {
baseCake = cake;
}

@Override
double getCost () {
return baseCake.getCost() + 2.32;
}

@Override
String getIngredients () {
return "decoration using candies";
}
}