首页 > 代码库 > .net之抽象工厂模式
.net之抽象工厂模式
//抽象工厂
//抽象食物
namespace abstractFactory
{
public abstract class food
{
public abstract void Food();
}
}
//食物1
namespace abstractFactory
{
public class food1:food
{
public override void Food()
{
Console.Write("鱼翅燕窝");
}
}
}
//食物2
namespace abstractFactory
{
class food2:food
{
public override void Food()
{
Console.Write("白饭馒头");
}
}
}
//抽象饮料
namespace abstractFactory
{
public abstract class drink
{
public abstract void Drink();
}
}
//饮料1
namespace abstractFactory
{
class drink1:drink
{
public override void Drink()
{
Console.Write("桂花百合汤");
}
}
}
//饮料2
namespace abstractFactory
{
class drink2:drink
{
public override void Drink()
{
Console.Write("白开水");
}
}
}
//抽象工厂
namespace abstractFactory
{
/// <summary>
/// 抽象工厂
/// </summary>
public interface fanFactory
{
food createFood();
drink createDrink();
}
}
//工厂1
namespace abstractFactory
{
public class factory1:fanFactory
{
public food createFood()
{
return new food1();
}
public drink createDrink()
{
return new drink1();
}
}
}
//工厂2
namespace abstractFactory
{
class factory2:fanFactory
{
public food createFood()
{
return new food2();
}
public drink createDrink()
{
return new drink2();
}
}
}
//入口
static void Main(string[] args)
{
fanFactory ff1 = new factory1();
fanFactory ff2 = new factory2();
food f1 = ff1.createFood();
drink d1 = ff1.createDrink();
Console.Write("好一点的饭:");
f1.Food();
Console.Write("和");
d1.Drink();
Console.WriteLine();
//差一点的饭
food f2 = ff2.createFood();
drink d2 = ff2.createDrink();
Console.Write("差一点的饭:");
f2.Food();
Console.Write("和");
d2.Drink();
Console.WriteLine();
Console.ReadLine();
}
//结果
.net之抽象工厂模式