0

我正在开发一个应用程序,它基本上会在用户需要对各种设备执行预防性维护时提示他们。每件设备(称之为工具)都有不同的重复模式。有些将基于时间,例如每 90 天,而另一些则基于其他因素,例如使用情况。例如,一个特定的工具在被检出 X 次后可能需要 PM。

所有工具都分配给一个人,因此当该用户登录应用程序时,我需要显示当时需要 PM 的工具列表。完成 PM 后,这些项目会从列表中删除,并且在其他情况下,例如日期更改或工具被放回婴儿床,工具将被添加。

我从将返回此列表的服务方法的设计开始,并正在寻找有关我用来解决列表中哪些项目的方法的一些帮助。对于输入,我将拥有用户的标识符和当前日期。从那里我需要查询工具列表并根据它们的重复模式确定上次执行 PM 的时间以及自上次 PM 以来发生的事件,该项目是否应该出现在列表中。

希望这是有道理的。我感谢任何关于我应该如何处理这个逻辑的想法和指导。

4

3 回答 3

2

为了结束,我想我会用我的努力的结果来更新帖子,尽管我对 Brandon 和 Florian 的答案表示赞赏,因为他们让我走上了正确的道路。

正如 Brandon 建议的那样,我最终得到了一个名为 IHaveRecurrence 的接口,定义如下:

public interface IHaveRecurrence
{
    DateTime? LastOccurrence { get; }
    RecurrenceType RecurrenceType { get; }
    Int32 RecurrenceValue { get; }
    Boolean IsDue();
}

我的 MaintainableTool(基)类现在实现了这个接口。IsDue 方法是通过委托给 Recurrence 类来实现的:

public Boolean IsDue()
{
    return Recurrence.IsDue(this);
}

Recurrence 是 Fl​​orian 建议的抽象基类。我有几个子类,例如 DailyRecurrence、WeeklyRecurrence 等。每个子类都对应于 RecurrenceType 枚举中的一个值,并实现适当的逻辑,以通过 IHaveRecurrence 接口根据 RecurrenceValue 和 LastOccurrence 属性确定 PM 是否到期。

Recurrence 使用内部 RecurrenceFactory 类来解析使用哪个子类:

internal sealed class RecurrenceFactory
{
    public Recurrence GetRecurrence(RecurrenceType type)
    {
        switch (type)
        {
            case Daily: return new DailyRecurrence;
            :
        }
    }
}

并且 Recurrence 实现为:

public abstract class Recurrence : IDisposable
{
    public static Boolean IsDue(IHaveRecurrence recurringObj)
    {
        using (var recurrence = RecurrenceFactory.GetRecurrence(recurringObj.RecurrenceType))
        {
            return recurrence.GetIsDue(recurringObj);
        }
    }

    protected abstract Boolean GetIsDue(IHaveRecurrence recurringObj);
}

然后,例如, DailyRecurrence 类实现为:

public sealed class DailyRecurrence : Recurrence
{
    protected override Boolean GetIsDue(IHaveRecurrence recurringObj)
    {
        if (recurringObj.LastOccurred.HasValue)
            return recurringObj.LastOccurred.AddDays(recurringObj.RecurrenceValue) <= DateTime.Now;

        return true;
    }
}

我喜欢这个模型,因为它具有高度可扩展性,封装了解决每个重复模式所需的逻辑并保持我的业务对象干净。

于 2011-04-11T15:49:22.707 回答
1

在我看来,每个工具都有自己的逻辑来确定是否需要 PM。所以我会用几个枚举定义一个 IEquipment 接口,如下所示:

public enum RecurrenceType
{
    //Values
}

public enum RecurrenceFrequency
{
    //Values
}

public interface IEquipment
{
    bool IsPMRequired();

    RecurrenceType RecurrenceType { get; }
    RecurrenceFrequency RecurrenceFrequency { get; }
    //You may want to choose something other than object, or eliminate this property
    object RecurrenceValue { get; set; } 
}

然后每类设备都会实现接口,可以实现判断是否需要PM的逻辑:

public class Tractor : IEquipment
{
    public bool IsPMRequired()
    {
        //Implement logic here specific to a Tractor
        throw new NotImplementedException();
    }

    public RecurrenceType RecurrenceType
    {
        get { throw new NotImplementedException(); }
    }

    public RecurrenceFrequency RecurrenceFrequency
    {
        get { throw new NotImplementedException(); }
    }

    public object RecurrenceValue
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}

那么您使用这些对象的逻辑将类似于:

List<IEquipment> equipment = new List<IEquipment>();
//Populate equipment
List<IEquipment> display = equipment.Where(e => e.IsPMRequired()).ToList();
//Display the equipment that needs PM

为 Equipment 提供一个基类可能也很有意义,您可以在其中放置通用方法/属性定义。

于 2011-04-01T17:15:24.857 回答
1

我会定义一个抽象类 Recurrence,如下所示:

abstract class Recurrence
{
      public abstract bool IsDue(DateTime dateAndTime);
}

现在使 Recurrence 成为 Equipment 的属性。

abstract class Equipment
{
      protected abstract Recurrence PMRecurrence
      {
            get;
      }

      public bool IsPMDue(DateTime dateAndTime)
      {
             return PMRecurrence.IsDue(dateAndTime);
      }
 }

然后,您可以定义适当的 Recurrence 子类,如 TimeRecurrence、UsageRecurrence,并将它们的实例分配给适当的 Equipment 子类,如 Hammer、Tractor 并根据需要实现它们。这应该提供灵活性和可维护性的良好平衡。

祝你好运,似乎是一项具有挑战性的任务!

于 2011-04-01T17:32:18.993 回答