mardi 8 janvier 2019

Decorator with abstraction, dont get decorated value properly

Im trying to do Decorator Design Pattern and it is what i got:

My base class is an abstract Worker class:

public abstract class Worker
{
    public float Salary { get; set; }
    public abstract float CountSalary();
}

Worker is a base class for Driver:

public class Driver : Worker
{
    public float Salary { get; set; }
    public override float CountSalary() => Salary = 3000;
    //for testing i just hard coded '3000' value
}

My Decorator is an abstract Bonus class. It decorating worker's salary with bonuses.

public abstract class Bonus : Worker
{
    public Bonus(Worker worker) => this.worker = worker;
    public override float CountSalary() => worker.Salary;
    protected Worker worker { get; private set; }
}

public class AmountBonus : Bonus
{
    public AmountBonus(Worker worker) : base(worker: worker){ }
    public override float CountSalary() => base.worker.Salary + 200;
}

I make a call of a Decorator in this way in my code:

Worker w = new AmountBonus(new Driver());

And instead of 3200, new Salary = 200. Could you tell me, when i make a mistake and i dont get predicted Salary = 3200? Why when i make call like this:

Worker w = new AmountBonus(new AmountBonus(new Driver()));

My Salary dont stack to 3400 value?

Aucun commentaire:

Enregistrer un commentaire