简体   繁体   English

具有抽象的装饰器,不能正确获得装饰值

[英]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:我的基类是一个abstract Worker类:

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

Worker is a base class for Driver : WorkerDriver的基类:

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.我的装饰器是一个abstract Bonus类。 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:我在我的代码中以这种方式调用了一个Decorator

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

And instead of 3200 , new Salary = 200 .而不是3200 , new Salary = 200 Could you tell me, when i make a mistake and i dont get predicted Salary = 3200 ?你能告诉我,当我犯了一个错误并且我没有得到预测的Salary = 3200吗? Why when i make call like this:为什么当我这样打电话时:

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

My Salary dont stack to 3400 value?我的Salary没有达到 3400 的价值?

I have made some modifications to your code :我对您的代码进行了一些修改:

 public abstract class Worker
{
    public abstract float Salary { get; } 
}

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

}

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

public class AmountBonus : Bonus
{
    public AmountBonus(Worker worker) : base(worker: worker) { }
    public override float Salary
    {
        get
        {
            return worker.Salary +200;
        }
    }

}


  static void Main(string[] args)
    {
        var driver = new Driver(3200);

        Console.WriteLine(driver.Salary);
        var driverSalWithBonus = new AmountBonus(driver);
        Console.WriteLine(driverSalWithBonus.Salary);
        Console.ReadLine();
    }

Then main problem with your code was CountSalary method was not called anywhere and its not required.然后您的代码的主要问题是 CountSalary 方法没有在任何地方被调用,也不是必需的。 The second problem was Salary should abstract so that it can be overwritten in the child classes.第二个问题是薪水应该抽象,以便它可以在子类中被覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM