23种设计模式-03-工厂方法模式
程序员文章站
2024-01-05 17:32:34
...
public abstract class Food
{
public abstract void Make();
}
public class Chicken : Food
{
public override void Make()
{
Console.WriteLine("鸡肉做好了!");
}
}
public class Patato : Food
{
public override void Make()
{
Console.WriteLine("土豆做好了!");
}
}
public class TomatoWithEggs : Food
{
public override void Make()
{
Console.WriteLine("西红柿炒蛋做好了!");
}
}
public abstract class FactoryCreator
{
public abstract Food CreateFactory();
}
public class ChickenFactoryCreator : FactoryCreator
{
public override Food CreateFactory()
{
return new Chicken();
}
}
public class PatatoFactoryCreator : FactoryCreator
{
public override Food CreateFactory()
{
return new Patato();
}
}
public class TomatoWithEggsFactoryCreator : FactoryCreator
{
public override Food CreateFactory()
{
return new TomatoWithEggs();
}
}
/// <summary>
/// 工厂方法模式:
/// 在简单工厂模式中,我们需要吃什么菜,由最开始的自己做,到后面的将如何做推迟到简单工厂中
/// 也就是饭馆做什么菜,还是需要听我们的,我们说做啥菜,饭馆给我们做啥菜,如果要加新菜,需要饭馆去加,有点麻烦
/// 在工厂方法模式中,我们点的菜由哪个饭馆做,可以由我们自己定义,
/// 比如A饭馆有土豆和西红柿炒蛋,我要吃西红柿炒蛋或者土豆就让A做
/// 但是我要吃鸡肉,只有C饭馆有,此时我就可以让C去给我做
/// 这就是所谓的具体哪个饭馆做,由我来定义,
/// 每个饭馆都是一个工厂,具体需要哪个工厂,由我来定义
/// </summary>
class Program
{
static void Main(string[] args)
{
TestFactoryMethod();
Console.WriteLine("End");
Console.ReadKey();
}
private static void TestFactoryMethod()
{
//初始化工厂方法
var patatoFactory = new PatatoFactoryCreator();
var tomatoWithEggsFactory = new TomatoWithEggsFactoryCreator();
var chickenFactory = new ChickenFactoryCreator();
//通过工厂方法去创建工厂
var food1 = patatoFactory.CreateFactory();
food1.Make();
var food2 = tomatoWithEggsFactory.CreateFactory();
food2.Make();
var food3 = chickenFactory.CreateFactory();
food3.Make();
}
}