使用 C# 编码(涉及类、接口、委托等关键知识点),实现对周黑鸭工厂的产品生产统一管理

本文介绍了如何使用C#定义接口IProductionFactory,并通过工厂类如WuhanFactory、NanjingFactory和ChangshaFactory来实现不同功能。展示了如何通过委托和委托链进行生产和输出。
摘要由CSDN通过智能技术生成

定义接口

interface IProductionFactory
{
    string productNeck(string command);
    string productWings(string command);
}


实现地方工厂类

class WuhanFactory : IProductionFactory
{
    //武汉工厂
    public string productNeck(string command)
    {
        return "武汉鸭脖";
    }

    public string productWings(string command)
    {
        return "武汉鸭翅";
    }
}

class NanjingFactory : IProductionFactory
{
    //南京工厂
    public string productNeck(string command)
    {
        return "南京鸭脖";
    }

    public string productWings(string command)
    {
        throw new NotImplementedException();
    }   
}

class ChangshaFactory : IProductionFactory
{
    //长沙工厂
    public string productNeck(string command)
    {
        throw new NotImplementedException();
    }

    public string productWings(string command)
    {
        return "长沙鸭翅";
    }
}

主函数

class Program
{
    public static void Main(string[] args)
    {
        //实例化工厂
        IProductionFactory wuhan = new WuhanFactory();
        IProductionFactory nanjing = new NanjingFactory();
        IProductionFactory changsha = new ChangshaFactory();
        //定义生产委托
        Func<string,string> product = new(wuhan.productNeck);
        product += wuhan.productWings;
        product += nanjing.productNeck;
        product += changsha.productWings;
        //定义产出集
        List<string> res = [];
        //进行生产
        foreach (Func<string, string> s in product.GetInvocationList())
        {
            res.Add(s("produce"));
        }
        //打印产出
        foreach(var s in res)
        {
            Console.WriteLine(s);
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值