vendredi 17 avril 2020

Getting a specific value while using Decorator Pattern

I am implementing Decorator's pattern on phone call Tariff objects.

The point is to have different tariffs where all are active but the one that is applies depends on the time when the call is made. There are basics tariffs (flat amount per time) and special tariffs (such as a whole day cheaper or specific times during the day when the cost per time is cheaper).

What I don't know is how to properly get the different tariffs costs that are stored in my decorated object, as when I try to get the cost, It only returns the last decorator's cost.

For example: - I initialize the first Tariff on X value, the getter returns me X cost. - On the same variable of the first Tariff, I initialize the second value with Y cost, and the getter returns me Y cost, but I can't get X cost. - And so on, third tariff, Z cost, getter returns Z cost.

My implementation on the Pattern is like this:

Aucun commentaire:

Enregistrer un commentaire