首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >装饰器模式-如何跟踪装饰器?

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

Stack Overflow用户
提问于 2016-08-10 03:06:28
回答 1查看 99关注 0票数 1

我想在一个有序的集合中跟踪所有的装饰器,我该怎么做呢?

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

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2016-08-10 04:01:36

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38858584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档