Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions out/production/DesignPatterns/oops/decorator/storyNotes/Note1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

[coffeeShopImpl1] :

Beverage is an abstract parent class which has two main methods getDescription and getCost. All the child classes
inherit from that Beverage class and set its own attributes.

You can find some items in coffeeShopMenu like ChocoCoffee, ColdCoffee, HotCoffee and couple of hundreds.

The problem arises where suppose the prices of the coffees increase, also each item can have different configurations
like hot coffee with whipped milk or hot coffee with added cream, so there are hundreds of possible permutations.

One intuition would be to add all these properties in the Beverage Class itself and then just set these booleans on runtime.
This approach is represented in Impl2.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
22 changes: 22 additions & 0 deletions src/oops/decorator/coffeeShopImpl1/Beverage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package oops.decorator.coffeeShopImpl1;

abstract public class Beverage {
private String description;
private Integer cost;

public String getDescription() {
return description;
}

public Integer getCost() {
return cost;
}

public void setDescription(String description) {
this.description = description;
}

public void setCost(Integer cost) {
this.cost = cost;
}
}
10 changes: 10 additions & 0 deletions src/oops/decorator/coffeeShopImpl1/coffeeShopMenu/ChocoCoffee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package oops.decorator.coffeeShopImpl1.coffeeShopMenu;

import oops.decorator.coffeeShopImpl1.Beverage;

public class ChocoCoffee extends Beverage {
public ChocoCoffee() {
super.setCost(300);
super.setDescription("Choco Coffee!!");
}
}
11 changes: 11 additions & 0 deletions src/oops/decorator/coffeeShopImpl1/coffeeShopMenu/ColdCoffee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oops.decorator.coffeeShopImpl1.coffeeShopMenu;

import oops.decorator.coffeeShopImpl1.Beverage;

public class ColdCoffee extends Beverage {

public ColdCoffee() {
super.setCost(200);
super.setDescription("Cold Coffee!!");
}
}
12 changes: 12 additions & 0 deletions src/oops/decorator/coffeeShopImpl1/coffeeShopMenu/HotCoffee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package oops.decorator.coffeeShopImpl1.coffeeShopMenu;

import oops.decorator.coffeeShopImpl1.Beverage;

public class HotCoffee extends Beverage {

public HotCoffee() {
super.setCost(100);
super.setDescription("Hot Coffee!!");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package oops.decorator.coffeeShopImpl1.coffeeShopMenu;

import oops.decorator.coffeeShopImpl1.Beverage;

public class HotCoffeeWithAddedCream extends Beverage {

public HotCoffeeWithAddedCream() {
Integer hotCoffeeCost = super.getCost();
Integer addedCreamCost = 100;
super.setCost(hotCoffeeCost + addedCreamCost);

super.setDescription("Hot Coffee with added cream!!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package oops.decorator.coffeeShopImpl1.coffeeShopMenu;

import oops.decorator.coffeeShopImpl1.Beverage;

public class HotCoffeeWithWhippedMilk extends HotCoffee {
public HotCoffeeWithWhippedMilk() {
Integer hotCoffeeCost = super.getCost();
Integer costOfWhippedMilk = 50;
super.setCost(hotCoffeeCost + costOfWhippedMilk);

super.setDescription("Hot Coffee with whipped milk!!");
}
}
80 changes: 80 additions & 0 deletions src/oops/decorator/coffeeShopImpl2/Beverage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package oops.decorator.coffeeShopImpl2;

import oops.decorator.coffeeShopImpl2.coffeeShopMenu.CostOfAddons;

abstract public class Beverage {

private String description;
private Integer cost = 0;
private Integer costOfProperties = 0;

public void setDescription(String description) {
this.description = description;
}

public void setCost(Integer cost) {
this.cost = cost;
}

public String getDescription() {
return description;
}

public Integer getTotalCost() {
return cost + costOfProperties;
}

public Integer getCostOfProperties() {
return costOfProperties;
}

public void notifyPropertiesChanged() {
calculateCostOfProperties();
}

private void calculateCostOfProperties() {
costOfProperties = 0;

if (mocha) {
costOfProperties += CostOfAddons.mocha;
}

if (choco) {
costOfProperties += CostOfAddons.choco;
}

if (cream) {
costOfProperties += CostOfAddons.cream;
}

if (whippedMilk) {
costOfProperties += CostOfAddons.whippedMilk;
}

// ... and so on
}

////////// Beverage Properties //////////

private Boolean mocha = false;
private Boolean choco = false;
private Boolean cream = false;
private Boolean whippedMilk = false;
// and 100s of such properties..

public void setMocha(Boolean mocha) {
this.mocha = mocha;
}

public void setChoco(Boolean choco) {
this.choco = choco;
}

public void setCream(Boolean cream) {
this.cream = cream;
}

public void setWhippedMilk(Boolean whippedMilk) {
this.whippedMilk = whippedMilk;
}
}
24 changes: 24 additions & 0 deletions src/oops/decorator/coffeeShopImpl2/CoffeeShop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package oops.decorator.coffeeShopImpl2;

import oops.decorator.coffeeShopImpl2.coffeeShopMenu.ChocoCoffee;

public class CoffeeShop {
public static void main(String[] args) {
Beverage coffee = new ChocoCoffee();

System.out.println(coffee.getTotalCost());

// dynamically setting the addons

coffee.setMocha(true);
coffee.setChoco(true);
coffee.notifyPropertiesChanged();

System.out.println(coffee.getTotalCost());

coffee.setCream(true);
coffee.notifyPropertiesChanged();

System.out.println(coffee.getTotalCost());
}
}
11 changes: 11 additions & 0 deletions src/oops/decorator/coffeeShopImpl2/coffeeShopMenu/ChocoCoffee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oops.decorator.coffeeShopImpl2.coffeeShopMenu;

import oops.decorator.coffeeShopImpl2.Beverage;

public class ChocoCoffee extends Beverage {

public ChocoCoffee() {
super.setCost(100);
super.setDescription("Choco Coffee!!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package oops.decorator.coffeeShopImpl2.coffeeShopMenu;

public interface CostOfAddons {
Integer mocha = 10;
Integer choco = 20;
Integer cream = 30;
Integer whippedMilk = 30;
}
13 changes: 13 additions & 0 deletions src/oops/decorator/storyNotes/Note1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

[coffeeShopImpl1] :

Beverage is an abstract parent class which has two main methods getDescription and getCost. All the child classes
inherit from that Beverage class and set its own attributes.

You can find some items in coffeeShopMenu like ChocoCoffee, ColdCoffee, HotCoffee and couple of hundreds.

The problem arises where suppose the prices of the coffees increase, also each item can have different configurations
like hot coffee with whipped milk or hot coffee with added cream, so there are hundreds of possible permutations.

One intuition would be to add all these properties in the Beverage Class itself and then just set these booleans on runtime.
This approach is represented in Impl2.