-
Notifications
You must be signed in to change notification settings - Fork 1
Redesign of decorator pattern #12
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
6b956fa
45f69a1
5be0c75
b31714e
12ba9ba
9177395
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| package Decorator.littlekai.base | ||
|
|
||
| abstract class IngredientDecorator (private val noodles: Noodles) : Noodles by noodles { | ||
| protected abstract val COST : Double | ||
|
|
||
| fun calculateTotalCost(): Double = noodles.calculateCost() + COST | ||
| abstract class IngredientDecorator (private val noodles: Noodles): Noodles by noodles { | ||
| override fun calculateTotalCost(): Double = noodles.calculateTotalCost() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package Decorator.littlekai.base | ||
|
|
||
| interface Noodles { | ||
| fun calculateCost() : Double | ||
| interface Noodles { | ||
| fun calculateCost(): Double | ||
| fun calculateTotalCost(): Double | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| package Decorator.littlekai.base | ||
|
|
||
| abstract class SauceDecorator(private val noodles: Noodles) : Noodles by noodles { | ||
| abstract class SauceDecorator(private val noodles: Noodles): Noodles by noodles { | ||
| abstract val SPICINESS: Int | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,6 @@ import Decorator.littlekai.base.IngredientDecorator | |
| import Decorator.littlekai.base.Noodles | ||
|
|
||
|
|
||
| class Chicken(noodles: Noodles) : IngredientDecorator(noodles) { | ||
| override val COST: Double = 3.50 | ||
| class Chicken(noodles: Noodles, val COST: Double = 3.50): IngredientDecorator(noodles) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the keyword
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you are placing the declaration in the constructor. You are giving the opportunity to set another
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've done some tests and you're right. I have extracted the COST variable from the constructor to an immutable constant. |
||
| override fun calculateTotalCost(): Double = super.calculateTotalCost() + COST | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then
IngredientDecoratoris not adding any behaviour 😵There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think with the fact of implementing the decorator function in each concrete case, it's already adding behaviour. What do you think about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But If
Noodlesalready has a methodcalculateTotalCost()then, every child that implements it modifies its behaviour.Noodlesshouldn't have that method, it belongs toIngredientDecoratorand the concrete decorators which will add that behaviour to Noodles.