装饰器模式 - 如何跟踪装饰器?

时间:2016-08-09 19:06:28

标签: c# decorator

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

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

服务是一个组成部分;劳工和设备是装饰者。

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;
}

1 个答案:

答案 0 :(得分:1)

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

相关问题