【问题标题】:Decorator pattern — how do I track decorators? [duplicate]装饰器模式——我如何跟踪装饰器? [复制]
【发布时间】:2016-08-09 19:06:27
【问题描述】:

我想跟踪排序集中的所有装饰器,我该怎么做?

我的想法是有一个集合,我只是在构造函数中的包装过程中添加装饰器,但是我当前的实现不断返回一个包含单个项目的集合。有人可以帮我解决这个问题吗?

服务是一个组件;劳动力和设备是装饰者。

public abstract class Service : IComparable<Service>
{
    public abstract decimal JobCost { get; }
    public virtual SortedSet<Service> Description { get; } = new SortedSet<Service>();
    public void AddToStack() => Description.Add(this);

    public int CompareTo(Service other)
    {
        // if both are labor services, then 
        if ((this is Labor) && (other is Labor))
        {
            return (this as Labor).Time.CheckIn.CompareTo((other as Labor).Time.CheckIn);
        }

        if (other is Equipment)
        {
            return -1;
        }

        return -2;
    }
}

public abstract class ServiceDecorator : Service
{
    public abstract override SortedSet<Service> Description { get; }
}


public class Labor : ServiceDecorator
{
    private Service service;
    private LaborRatesDual laborRates;
    private LaborTime laborTime;

    public Labor(Service service, LaborTime laborTime, LaborRatesDual laborRates)
    {
        this.service = service;
        this.laborTime = laborTime;
        this.laborRates = laborRates;

        this.service.AddToStack();
    }

    public int WorkOrderNo { get; set; }
    public string PO { get; set; }
    public override SortedSet<Service> Description => new SortedSet<Service>(service.Description);

    public int TechCount { get; set; } = 1;
    public LaborTime Time
    {
        get { return laborTime; }
        set { laborTime = value; }
    }
    public LaborRatesDual Rates
    {
        get { return laborRates; }
        set { laborRates = value; }
    }

    // labor time to complete the service (labor time x tech count)
    public TimeSpan Duration => TimeSpan.FromTicks(Time.Duration.Ticks * TechCount);

    // get the cost for this particular labor service
    public decimal Cost
    {
    ...
    }

    // returns complete job cost, including the wrapped object
    public override decimal JobCost => service.JobCost + Cost;

    // helps identify is the service should be billed at continuous rate, or if the time
    // should be billable using the dual-rate system
    public bool IsContinuation { get; set; } = false;

}


public class Equipment : ServiceDecorator
{
    Service service;
    EquipmentTime time;
    EquipmentRate rate;

    public Equipment(Service service, EquipmentTime equipmentTime, EquipmentRate equipmentRate)
    {
        this.service = service;
        this.time = equipmentTime;
        this.rate = equipmentRate;

        this.service.AddToStack();
    }

    public EquipmentTime Time
    {
        get { return time; }
        set { time = value; }
    }
    public int UnitCount { get; set; } = 1;
    public decimal Rate
    {
        get { return rate.Rate; }
        set { rate.Rate = value; }
    }
    public bool UseCalendarDayCount { get; set; } = false;

    // units x days
    public int FullCount
    {
     ...
    }

    public override SortedSet<Service> Description => new SortedSet<Service>(service.Description);

    public decimal Cost => FullCount * Rate;

    public override decimal JobCost => service.JobCost + Cost;
}

【问题讨论】:

    标签: c# decorator


    【解决方案1】:

    尝试将 SortedSet 更改为静态。并且不要在新的装饰器中创建新的。您的任何装饰器都有自己的 SortedSet,因此他们不会共享您的基本实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-02
      • 2016-08-01
      • 2023-01-22
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      相关资源
      最近更新 更多