C#分拣系统用设计模式实现

文章介绍了一个基于设计模式的分拣系统实现,包括工厂模式用于创建物品和分拣站,观察者模式处理物品到达的通知,以及策略模式定义分拣和区域划分规则。具体实现中,物品类、分拣站接口、分拣规则接口和区域划分规则接口被定义,然后通过具体的分拣站类和规则类进行实现。分拣系统类负责协调各个分拣站的工作。
摘要由CSDN通过智能技术生成

一个分拣系统通常包含多个分拣站,每个分拣站会收到一些物品并根据一定的规则将物品分拣到不同的区域。为了实现这样的分拣系统,我们可以使用以下设计模式:

工厂模式:用于创建不同类型的物品和分拣站。
观察者模式:用于在物品到达时通知所有分拣站进行处理。
策略模式:用于定义分拣规则和区域划分规则。
下面是一个简单的实现:

// 物品类
public class Item
{
    public string Name { get; set; }
    public int Weight { get; set; }
}

// 分拣站接口
public interface ISortingStation
{
    void SortItem(Item item);
}

// 分拣规则接口
public interface ISortingRule
{
    string GetDestination(Item item);
}

// 区域划分规则接口
public interface IZonePartitionRule
{
    string GetZone(Item item);
}

// 物品工厂类
public static class ItemFactory
{
    public static Item CreateItem(string name, int weight)
    {
        return new Item { Name = name, Weight = weight };
    }
}

// 分拣站基类
public abstract class SortingStationBase : ISortingStation
{
    public abstract void SortItem(Item item);
}

// 分拣站A
public class SortingStationA : SortingStationBase
{
    private readonly ISortingRule _sortingRule;
    private readonly IZonePartitionRule _zonePartitionRule;

    public SortingStationA(ISortingRule sortingRule, IZonePartitionRule zonePartitionRule)
    {
        _sortingRule = sortingRule;
        _zonePartitionRule = zonePartitionRule;
    }

    public override void SortItem(Item item)
    {
        string destination = _sortingRule.GetDestination(item);
        string zone = _zonePartitionRule.GetZone(item);

        // 将物品分拣到指定区域
        Console.WriteLine($"Item {item.Name} with weight {item.Weight} sorted to {destination} in zone {zone}");
    }
}

// 分拣规则A
public class SortingRuleA : ISortingRule
{
    public string GetDestination(Item item)
    {
        // 根据物品重量判断分拣到哪个区域
        if (item.Weight <= 10)
            return "Area A";
        else if (item.Weight <= 20)
            return "Area B";
        else
            return "Area C";
    }
}

// 区域划分规则A
public class ZonePartitionRuleA : IZonePartitionRule
{
    public string GetZone(Item item)
    {
        // 根据物品名称判断属于哪个区域
        if (item.Name.StartsWith("A"))
            return "Zone 1";
        else if (item.Name.StartsWith("B"))
            return "Zone 2";
        else
            return "Zone 3";
    }
}

// 分拣系统类
public class SortingSystem
{
    private readonly List<ISortingStation> _sortingStations;

    public SortingSystem()
    {
        _sortingStations = new List<ISortingStation>();
    }

    public void AddSortingStation(ISortingStation sortingStation)
    {    _sortingStations.Add(sortingStation);
    }

public void RemoveSortingStation(ISortingStation sortingStation)
{
    _sortingStations.Remove(sortingStation);
}

public void SortItem(Item item)
{
    // 通知所有分拣站进行处理
    foreach (var sortingStation in _sortingStations)
    {
        sortingStation.SortItem(item);
    }
}
}

// 示例用法
var sortingSystem = new SortingSystem();
var sortingRuleA = new SortingRuleA();
var zonePartitionRuleA = new ZonePartitionRuleA();
var sortingStationA1 = new SortingStationA(sortingRuleA, zonePartitionRuleA);
var sortingStationA2 = new SortingStationA(sortingRuleA, zonePartitionRuleA);

sortingSystem.AddSortingStation(sortingStationA1);
sortingSystem.AddSortingStation(sortingStationA2);

var item1 = ItemFactory.CreateItem("Apple", 8);
var item2 = ItemFactory.CreateItem("Banana", 15);
var item3 = ItemFactory.CreateItem("Coke", 25);

sortingSystem.SortItem(item1);
sortingSystem.SortItem(item2);
sortingSystem.SortItem(item3);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值