mardi 8 janvier 2019

Get reference to object from base class. Decorator design pattern

Im dealing with Decorator design pattern. What i got now:

My Base abstract class Worker:

public abstract class Worker
{
    public float UsdPerHour { get; set; }
    public int HoursPerDay { get; set; }
    public int DaysOfWork { get; set; }

    public abstract float CountSalary();
}

Its abstract, because its base for my Workers classes. i think its not necesery, what is inside that classes. If you want to, i will add it to my edit.

Next i start implementing my decorator. Each Worker have CountSalary(). I want to applying bonuses to their salary based on decorator.

We are starting on abstract Bonus class:

public abstract class Bonus : Worker
{
    public Bonus(Worker worker) => this.worker = worker;
    public override float CountSalary() => worker.CountSalary();

    private Worker worker { get; set; }
}

Bonus are base clas for AmountBonus class.

public class AmountBonus : Bonus
{
    public AmountBonus(Worker worker) : base(worker: worker){ }
    // is this 'base' call correct?

    public override float CountSalary()
    {
        throw new NotImplementedException();
    }

    private Worker worker;
}

My question is, how to get reference to object Worker worker in base class (Bonus) in concrete decorator class as AmountBonus?

Aucun commentaire:

Enregistrer un commentaire