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

Discuss about Decorator pattern in Little Kai example #1

Open
Cotel opened this issue Mar 4, 2017 · 6 comments
Open

Discuss about Decorator pattern in Little Kai example #1

Cotel opened this issue Mar 4, 2017 · 6 comments

Comments

@Cotel
Copy link
Member

Cotel commented Mar 4, 2017

What do you think? What would you change?

@Cotel
Copy link
Member Author

Cotel commented Mar 7, 2017

Updated master to use kotlin's keyword by in SauceDecorator. Do you like it?

@Cotel
Copy link
Member Author

Cotel commented Mar 7, 2017

Can Decorators be implemented through kotlin's extension functions too? The Decorator would define a new function for the decorated class.

In our case, IngredientDecorator would define calculateCost() for Noodles

@tonilopezmr
Copy link
Member

You could do an example

@Cotel
Copy link
Member Author

Cotel commented Mar 8, 2017

I've been trying and I don't think it is possible.

1st reason is extension functions scope is really strange. If I declare an extension function for Noodles inside IngredientDecorator it will never be reached from nowhere.

2nd reason. In my example i have removed calculateCost from Noodles and I declare it as an extension function in IngredientDecorator:

abstract class IngredientDecorator(protected val noodles: Noodles) : Noodles {
  override abstract protected val cost : Double

  fun Noodles.calculateCost() : Double = noodles.cost + cost // Both of this costs are referencing Noodles.costs
}

The problem is, when you declare an extension function, this inside that scope is the class which we are extending (in this case is Noodles). So, I cannot access to IngredientDecorator's cost.

3rd problem is that if we do calculateCost this way it won't increase if we add more ingredients because Noodles cost will be a constant value, it will not calculate recursively if it is another decorator.

@Cotel
Copy link
Member Author

Cotel commented Mar 15, 2017

Ok, I've been reading Decorator's entry in Wikipedia and we are doing this pattern badly.

As @srgtrujillo said, the Decorator pattern Adds behaviour to an object and gives the possibility to remove it too. It adds responsabilities to an object dynamically and in a transparent way. It can be used when extension via inheritance is not possible or when there is no good reason to use it.

So, what do you want to do? It is better to make another example? We keep this one even with that new implementation? It was a good idea but we are losing the point with the new changes. Keeping calculateCost() in decorated noodles has no sense in my opinion. You don't need to know how much the base noodles cost after you added things to them, you are interested in the final price.

We can think of another example for decorators within Little Kai example.

@tonilopezmr
Copy link
Member

I said that in class that isn't a good example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment