-
Notifications
You must be signed in to change notification settings - Fork 3
feat : decorator pattern #2
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
Open
sidsharma2002
wants to merge
1
commit into
master
Choose a base branch
from
decorator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file added
BIN
+835 Bytes
out/production/DesignPatterns/oops/decorator/coffeeShopImpl1/Beverage.class
Binary file not shown.
Binary file added
BIN
+585 Bytes
...production/DesignPatterns/oops/decorator/coffeeShopImpl1/coffeeShopMenu/ChocoCoffee.class
Binary file not shown.
Binary file added
BIN
+581 Bytes
out/production/DesignPatterns/oops/decorator/coffeeShopImpl1/coffeeShopMenu/ColdCoffee.class
Binary file not shown.
Binary file added
BIN
+576 Bytes
out/production/DesignPatterns/oops/decorator/coffeeShopImpl1/coffeeShopMenu/HotCoffee.class
Binary file not shown.
Binary file added
BIN
+807 Bytes
...esignPatterns/oops/decorator/coffeeShopImpl1/coffeeShopMenu/HotCoffeeWithAddedCream.class
Binary file not shown.
Binary file added
BIN
+830 Bytes
...signPatterns/oops/decorator/coffeeShopImpl1/coffeeShopMenu/HotCoffeeWithWhippedMilk.class
Binary file not shown.
Binary file added
BIN
+2.49 KB
out/production/DesignPatterns/oops/decorator/coffeeShopImpl2/Beverage.class
Binary file not shown.
Binary file added
BIN
+1.1 KB
out/production/DesignPatterns/oops/decorator/coffeeShopImpl2/CoffeeShop.class
Binary file not shown.
Binary file added
BIN
+584 Bytes
...production/DesignPatterns/oops/decorator/coffeeShopImpl2/coffeeShopMenu/ChocoCoffee.class
Binary file not shown.
Binary file added
BIN
+505 Bytes
...roduction/DesignPatterns/oops/decorator/coffeeShopImpl2/coffeeShopMenu/CostOfAddons.class
Binary file not shown.
13 changes: 13 additions & 0 deletions
13
out/production/DesignPatterns/oops/decorator/storyNotes/Note1
This file contains hidden or 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,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 removed
BIN
-412 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl1/DuckOwner.class
Binary file not shown.
Binary file removed
BIN
-530 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl1/RubberDuck.class
Binary file not shown.
Binary file removed
BIN
-157 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl2/behaviours/FlyBehaviour.class
Binary file not shown.
Binary file removed
BIN
-163 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl2/behaviours/QuackBehaviour.class
Binary file not shown.
Binary file removed
BIN
-1.25 KB
out/production/DesignPatterns/oops/duckFamilyImpl2/behaviours/ducks/CityDuck.class
Binary file not shown.
Binary file removed
BIN
-667 Bytes
...ion/DesignPatterns/oops/duckFamilyImpl2/behaviours/ducks/DefaultFlyingBehaviourImpl.class
Binary file not shown.
Binary file removed
BIN
-670 Bytes
...tion/DesignPatterns/oops/duckFamilyImpl2/behaviours/ducks/DefaultQuackBehaviourImpl.class
Binary file not shown.
Binary file removed
BIN
-1.13 KB
out/production/DesignPatterns/oops/duckFamilyImpl2/behaviours/ducks/DuckOwner.class
Binary file not shown.
Binary file removed
BIN
-828 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl2/behaviours/ducks/RubberDuck.class
Binary file not shown.
Binary file removed
BIN
-597 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl3/CanNotFlyBehaviour.class
Binary file not shown.
Binary file removed
BIN
-982 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl3/CityDuck.class
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-711 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl3/DuckOwner.class
Binary file not shown.
Binary file removed
BIN
-936 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl3/RubberDuck.class
Binary file not shown.
Binary file removed
BIN
-594 Bytes
out/production/DesignPatterns/oops/duckFamilyImpl3/SqueakBehaviour.class
Binary file not shown.
Binary file renamed
BIN
+794 Bytes
...nPatterns/oops/duckFamilyImpl1/Duck.class → ...egy/duckFamily/duckFamilyImpl1/Duck.class
Binary file not shown.
Binary file added
BIN
+452 Bytes
out/production/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl1/DuckOwner.class
Binary file not shown.
Binary file added
BIN
+590 Bytes
out/production/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl1/RubberDuck.class
Binary file not shown.
Binary file added
BIN
+177 Bytes
...ion/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl2/behaviours/FlyBehaviour.class
Binary file not shown.
Binary file added
BIN
+183 Bytes
...n/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl2/behaviours/QuackBehaviour.class
Binary file not shown.
Binary file added
BIN
+1.46 KB
...n/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl2/behaviours/ducks/CityDuck.class
Binary file not shown.
Binary file added
BIN
+727 Bytes
...ops/strategy/duckFamily/duckFamilyImpl2/behaviours/ducks/DefaultFlyingBehaviourImpl.class
Binary file not shown.
Binary file added
BIN
+730 Bytes
...oops/strategy/duckFamily/duckFamilyImpl2/behaviours/ducks/DefaultQuackBehaviourImpl.class
Binary file not shown.
Binary file renamed
BIN
+636 Bytes
...ckFamilyImpl2/behaviours/ducks/Duck.class → ...ckFamilyImpl2/behaviours/ducks/Duck.class
Binary file not shown.
Binary file added
BIN
+1.33 KB
.../DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl2/behaviours/ducks/DuckOwner.class
Binary file not shown.
Binary file added
BIN
+968 Bytes
...DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl2/behaviours/ducks/RubberDuck.class
Binary file not shown.
Binary file added
BIN
+657 Bytes
...oduction/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl3/CanNotFlyBehaviour.class
Binary file not shown.
Binary file added
BIN
+1.17 KB
out/production/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl3/CityDuck.class
Binary file not shown.
Binary file added
BIN
+1.49 KB
out/production/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl3/Duck.class
Binary file not shown.
Binary file added
BIN
+823 Bytes
out/production/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl3/DuckOwner.class
Binary file not shown.
Binary file added
BIN
+1.13 KB
out/production/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl3/RubberDuck.class
Binary file not shown.
Binary file added
BIN
+654 Bytes
out/production/DesignPatterns/oops/strategy/duckFamily/duckFamilyImpl3/SqueakBehaviour.class
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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 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
10
src/oops/decorator/coffeeShopImpl1/coffeeShopMenu/ChocoCoffee.java
This file contains hidden or 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,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
11
src/oops/decorator/coffeeShopImpl1/coffeeShopMenu/ColdCoffee.java
This file contains hidden or 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,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
12
src/oops/decorator/coffeeShopImpl1/coffeeShopMenu/HotCoffee.java
This file contains hidden or 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,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!!"); | ||
| } | ||
|
|
||
| } |
14 changes: 14 additions & 0 deletions
14
src/oops/decorator/coffeeShopImpl1/coffeeShopMenu/HotCoffeeWithAddedCream.java
This file contains hidden or 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,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!!"); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/oops/decorator/coffeeShopImpl1/coffeeShopMenu/HotCoffeeWithWhippedMilk.java
This file contains hidden or 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,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!!"); | ||
| } | ||
| } |
This file contains hidden or 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,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; | ||
| } | ||
| } |
This file contains hidden or 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 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
11
src/oops/decorator/coffeeShopImpl2/coffeeShopMenu/ChocoCoffee.java
This file contains hidden or 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,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!!"); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/oops/decorator/coffeeShopImpl2/coffeeShopMenu/CostOfAddons.java
This file contains hidden or 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,8 @@ | ||
| package oops.decorator.coffeeShopImpl2.coffeeShopMenu; | ||
|
|
||
| public interface CostOfAddons { | ||
| Integer mocha = 10; | ||
| Integer choco = 20; | ||
| Integer cream = 30; | ||
| Integer whippedMilk = 30; | ||
| } |
This file contains hidden or 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,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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.