定义接口
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);
}
}
}