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

本文关键字:跟踪 模式 何跟踪 | 更新日期: 2023-09-27 18:01:38

我想跟踪一个排序集合中所有的decorator,我该怎么做呢?

我的想法是有一个集合,我只是在构造函数的包装过程中添加装饰器,但是我目前的实现一直返回一个集合,其中有一个项目。有人能帮我修一下吗?

Service是一个组件;劳动和设备是装饰。

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

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

尝试将SortedSet更改为static。不要在新的装饰器中创建新的。你的任何装饰器都有自己的SortedSet,所以它们不会共享你的基本实例。