假设我们需要实现一个生产看板系统,用于显示生产线上的各个工作站的状态和统计信息。我们可以使用观察者模式和工厂模式来实现这个系统。

首先,我们需要定义一个工作站状态枚举WorkstationStatus,用于表示工作站的状态。具体实现如下:

public enum WorkstationStatus
{
    Running,
    Stopped,
    Error
}

接着,我们需要定义一个工作站类Workstation,用于表示生产线上的一个工作站。具体实现如下:

public class Workstation
{
    public int Id { get; set; }
    public string Name { get; set; }
    public WorkstationStatus Status { get; set; }
}

在这个工作站类中,我们定义了工作站的ID、名称和状态等属性。

然后,我们需要定义一个生产线类ProductionLine,用于管理所有的工作站。具体实现如下:

public class ProductionLine
{
    private readonly List<Workstation> _workstations = new List<Workstation>();

    public IEnumerable<Workstation> GetWorkstations()
    {
        return _workstations;
    }

    public void AddWorkstation(Workstation workstation)
    {
        _workstations.Add(workstation);
    }

    public void RemoveWorkstation(Workstation workstation)
    {
        _workstations.Remove(workstation);
    }
}

在这个生产线类中,我们使用了一个列表来存储所有的工作站,并实现了获取工作站、添加工作站和移除工作站等方法。

接下来,我们需要定义一个工作站状态监控器接口IWorkstationMonitor,用于监控工作站的状态。具体实现如下:

public interface IWorkstationMonitor
{
    void Update(Workstation workstation);
}

在这个工作站状态监控器接口中,我们定义了一个Update方法,用于在工作站状态发生变化时更新工作站状态。

然后,我们需要定义一个工作站状态显示器类WorkstationStatusDisplay,用于显示工作站的状态。具体实现如下:

public class WorkstationStatusDisplay : IWorkstationMonitor
{
    public void Update(Workstation workstation)
    {
        Console.WriteLine($"Workstation {workstation.Name}: {workstation.Status}");
    }
}

在这个工作站状态显示器类中,我们实现了IWorkstationMonitor接口中定义的Update方法,并使用控制台输出工作站的名称和状态。

接着,我们需要定义一个工作站统计信息显示器类WorkstationStatisticsDisplay,用于显示工作站的统计信息。具体实现如下:

public class WorkstationStatisticsDisplay : IWorkstationMonitor
{
    public void Update(Workstation workstation)
    {
        Console.WriteLine($"Workstation {workstation.Name} statistics:");
        // TODO: display workstation statistics
    }
}

在这个工作站统计信息显示器类中,我们同样实现了IWorkstationMonitor接口中定义的Update方法,并使用控制台输出工作站的名称和统计信息。

接下来,我们需要定义一个工厂类WorkstationMonitorFactory,用于创建工作站状态监控器对象。具体实现如下:

public static class WorkstationMonitorFactory
{
    public static IWorkstationMonitor CreateMonitor(string monitorType)
    {
        switch (monitorType)
        {
            case "status":
                return new WorkstationStatusDisplay();
            case "statistics":
                return new WorkstationStatisticsDisplay();
            default:
                throw new ArgumentException($"Invalid monitor type: {monitorType}");
        }
    }
}

在这个工厂类中,我们定义了一个静态方法CreateMonitor,用于根据传入的监控器类型参数创建相应的监控器对象。目前,我们支持两种监控器类型,即工作站状态显示器和工作站统计信息显示器。

最后,我们需要在主函数中创建生产线和工作站,并为每个工作站创建相应的工作站状态监控器对象。具体实现如下:

static void Main(string[] args)
{
    // create production line
    var productionLine = new ProductionLine();

    // create workstations
    var workstation1 = new Workstation { Id = 1, Name = "Workstation 1", Status = WorkstationStatus.Running };
    var workstation2 = new Workstation { Id = 2, Name = "Workstation 2", Status = WorkstationStatus.Stopped };
    var workstation3 = new Workstation { Id = 3, Name = "Workstation 3", Status = WorkstationStatus.Error };

    // add workstations to production line
    productionLine.AddWorkstation(workstation1);
    productionLine.AddWorkstation(workstation2);
    productionLine.AddWorkstation(workstation3);

    // create monitors for each workstation
    var statusMonitor1 = WorkstationMonitorFactory.CreateMonitor("status");
    var statisticsMonitor1 = WorkstationMonitorFactory.CreateMonitor("statistics");
    var statusMonitor2 = WorkstationMonitorFactory.CreateMonitor("status");
    var statisticsMonitor2 = WorkstationMonitorFactory.CreateMonitor("statistics");
    var statusMonitor3 = WorkstationMonitorFactory.CreateMonitor("status");
    var statisticsMonitor3 = WorkstationMonitorFactory.CreateMonitor("statistics");

    // register monitors with workstations
    workstation1.RegisterMonitor(statusMonitor1);
    workstation1.RegisterMonitor(statisticsMonitor1);
    workstation2.RegisterMonitor(statusMonitor2);
    workstation2.RegisterMonitor(statisticsMonitor2);
    workstation3.RegisterMonitor(statusMonitor3);
    workstation3.RegisterMonitor(statisticsMonitor3);

    // simulate workstation status changes
    workstation1.Status = WorkstationStatus.Stopped;
    workstation2.Status = WorkstationStatus.Running;
    workstation3.Status = WorkstationStatus.Running;
    
 // unregister monitors from workstations
    workstation1.UnregisterMonitor(statusMonitor1);
    workstation1.UnregisterMonitor(statisticsMonitor1);
    workstation2.UnregisterMonitor(statusMonitor2);
    workstation2.UnregisterMonitor(statisticsMonitor2);
    workstation3.UnregisterMonitor(statusMonitor3);
    workstation3.UnregisterMonitor(statisticsMonitor3);

    // remove workstations from production line
    productionLine.RemoveWorkstation(workstation1);
    productionLine.RemoveWorkstation(workstation2);
    productionLine.RemoveWorkstation(workstation3);
}

在这个主函数中,我们首先创建了一个生产线和三个工作站,并将工作站添加到生产线中。然后,我们为每个工作站创建了一个状态监控器和一个统计信息监控器,并将它们注册到相应的工作站中。

接着,我们模拟了一些工作站状态的改变,并从工作站中注销了监控器对象。最后,我们将工作站从生产线中移除。

完整的代码实现如下:



using System;
using System.Collections.Generic;

// the observable object interface
public interface IWorkstation
{
    int Id { get; set; }
    string Name { get; set; }
    WorkstationStatus Status { get; set; }

    void RegisterMonitor(IWorkstationMonitor monitor);
    void UnregisterMonitor(IWorkstationMonitor monitor);
    void NotifyMonitors();
}

// the observable object implementation
public class Workstation : IWorkstation
{
    private readonly List<IWorkstationMonitor> _monitors = new List<IWorkstationMonitor>();

    public int Id { get; set; }
    public string Name { get; set; }
    public WorkstationStatus Status { get; set; }

    public void RegisterMonitor(IWorkstationMonitor monitor)
    {
        _monitors.Add(monitor);
    }

    public void UnregisterMonitor(IWorkstationMonitor monitor)
    {
        _monitors.Remove(monitor);
    }

    public void NotifyMonitors()
    {
        foreach (var monitor in _monitors)
        {
            monitor.Update(this);
        }
    }
}

// the observer interface
public interface IWorkstationMonitor
{
    void Update(IWorkstation workstation);
}

// the observer implementation for displaying workstation status
public class WorkstationStatusDisplay : IWorkstationMonitor
{
    public void Update(IWorkstation workstation)
    {
        Console.WriteLine($"Workstation '{workstation.Name}' is {workstation.Status}");
    }
}

// the observer implementation for displaying workstation statistics
public class WorkstationStatisticsDisplay : IWorkstationMonitor
{
    public void Update(IWorkstation workstation)
    {
        Console.WriteLine($"Workstation '{workstation.Name}' statistics: TODO");
    }
}

// the subject interface
public interface IProductionLine
{
    void AddWorkstation(IWorkstation workstation);
    void RemoveWorkstation(IWorkstation workstation);
}

// the subject implementation
public class ProductionLine : IProductionLine
{
    private readonly List<IWorkstation> _workstations = new List<IWorkstation>();

    public void AddWorkstation(IWorkstation workstation)
    {
        _workstations.Add(workstation);
    }

    public void RemoveWorkstation(IWorkstation workstation)
    {
        _workstations.Remove(workstation);
    }
}

// the factory class for creating workstation monitors
public static class WorkstationMonitorFactory
{
    public static IWorkstationMonitor CreateMonitor(string monitorType)
    {
        switch (monitorType)
        {
            case "status":
                return new WorkstationStatusDisplay();
            case "statistics":
                return new WorkstationStatisticsDisplay();
            default:
                throw new ArgumentException($"Invalid monitor type: {monitorType}");
        }
    }
}

// the possible values for a workstation's status
public enum WorkstationStatus
{
    Running,
    Stopped,
    Error
}

class Program
{
    static void Main(string[] args)
    {
        // create production line
        var productionLine = new ProductionLine();

        // create workstations
        var workstation1 = new Workstation { Id = 1, Name = "Workstation 1", Status = WorkstationStatus.Running };
        var workstation2 = new Workstation { Id = 2, Name = "Workstation 2", Status = WorkstationStatus.Stopped };
        var workstation3 = new Workstation { Id = 3, Name = "Workstation 3", Status = WorkstationStatus.Error };
            // register monitors with workstations
    workstation1.RegisterMonitor(statusMonitor1);
    workstation1.RegisterMonitor(statisticsMonitor1);
    workstation2.RegisterMonitor(statusMonitor2);
    workstation2.RegisterMonitor(statisticsMonitor2);
    workstation3.RegisterMonitor(statusMonitor3);
    workstation3.RegisterMonitor(statisticsMonitor3);

    // change workstation status and notify monitors
    workstation1.Status = WorkstationStatus.Stopped;
    workstation1.NotifyMonitors();
    workstation2.Status = WorkstationStatus.Error;
    workstation2.NotifyMonitors();

    // unregister monitors from workstations
    workstation1.UnregisterMonitor(statusMonitor1);
    workstation1.UnregisterMonitor(statisticsMonitor1);
    workstation2.UnregisterMonitor(statusMonitor2);
    workstation2.UnregisterMonitor(statisticsMonitor2);
    workstation3.UnregisterMonitor(statusMonitor3);
    workstation3.UnregisterMonitor(statisticsMonitor3);

    // remove workstation from production line
    productionLine.RemoveWorkstation(workstation3);
    }
}

在这个例子中,我们使用了观察者模式来实现生产看板功能。通过将工作站视为被观察对象,我们可以轻松地注册和注销观察器对象,以及在工作站状态发生变化时通知观察器对象。观察器对象可以是任何类型的对象,可以将它们视为对工作站状态的不同表示或者对工作站状态的不同处理方式。

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐