public abstract class Xiaolongkan
{
public abstract void MakeHotpot();
}
public class ShanghaiXiaolongkan : Xiaolongkan
{
public override void MakeHotpot()
{
Console.WriteLine("上海的小龙坎,很甜,不好吃");
}
}
public class ChongqingXiaolongkan : Xiaolongkan
{
public override void MakeHotpot()
{
Console.WriteLine("重庆的小龙坎,很辣,好吃刺激");
}
}
public abstract class Yuanlaosi
{
public abstract void MakeHotpot();
}
public class ShanghaiYuanlaosi : Yuanlaosi
{
public override void MakeHotpot()
{
Console.WriteLine("上海的袁老四,甜的,不好吃");
}
}
public class ChongqingYuanlaosi : Yuanlaosi
{
public override void MakeHotpot()
{
Console.WriteLine("重庆的袁老四,很辣,好吃刺激");
}
}
public abstract class AbstractFactory
{
public abstract Xiaolongkan CreateXiaolongkan();
public abstract Yuanlaosi CreateYuanlaosi();
}
public class ShanghaiFactory : AbstractFactory
{
public override Xiaolongkan CreateXiaolongkan()
{
return new ShanghaiXiaolongkan();
}
public override Yuanlaosi CreateYuanlaosi()
{
return new ShanghaiYuanlaosi();
}
}
public class ChongqingFactory : AbstractFactory
{
public override Xiaolongkan CreateXiaolongkan()
{
return new ChongqingXiaolongkan();
}
public override Yuanlaosi CreateYuanlaosi()
{
return new ChongqingYuanlaosi();
}
}
class Program
{
/// <summary>
/// 抽象工厂模式:
/// 学习了简单工厂和工厂方法之后,再来看抽象工厂,就非常简单,基本上无需写代码
/// 工厂方法是为了创建一个单一对象,而抽象工厂就是为了创建一系列的对象
/// 举例子我就全国不同城市的火锅吧,因为不同城市的口味是不同的
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//上海工厂
AbstractFactory shanghaiFactory = new ShanghaiFactory();
//上海小龙坎
Xiaolongkan shanghaiXiaolongkan = shanghaiFactory.CreateXiaolongkan();
shanghaiXiaolongkan.MakeHotpot();
//上海袁老四
Yuanlaosi shanghaiYuanlaosi = shanghaiFactory.CreateYuanlaosi();
shanghaiYuanlaosi.MakeHotpot();
//重庆工厂
AbstractFactory chongqingFactory = new ChongqingFactory();
//重庆小龙坎
Xiaolongkan chongqingXiaolongkan = chongqingFactory.CreateXiaolongkan();
chongqingXiaolongkan.MakeHotpot();
//重庆袁老四
Yuanlaosi chongqingYuanlaosi = chongqingFactory.CreateYuanlaosi();
chongqingYuanlaosi.MakeHotpot();
Console.WriteLine("End");
Console.ReadKey();
}
}