码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂模式

时间:2016-01-03 20:59:37      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace 
{
interface FoodFactory
{
      Burger CreateBurger();
      Chicken CreateChicken();
      Drink CreateDrink();
}
class KFCFactory:FoodFactory
{
    public Burger CreateBurger()
    {
        return new KFC_Burger();
    }
    public Chicken CreateChicken()
    {
        return new KFC_Chicken();
    }
    public Drink CreateDrink()
    {
        return new KFC_Drink();
    }
}
class McDonaldsFactory:FoodFactory
{
    public Burger CreateBurger()
    {
        return new McDonalds_Burger();
    }
    public Chicken CreateChicken()
    {
        return new McDonalds_Chicken();
    }
    public Drink CreateDrink()
    {
        return new McDonalds_Drink();
    }
}
interface Food
{
    void deal();
}
class Burger:Food
{
    public virtual void deal()
    { 
    }
}
class Chicken:Food
{
    public virtual void deal()
    {
    }
}
class Drink:Food
{
    public virtual void deal()
    {
    }
}
class KFC_Burger:Burger
{
    public override void deal()
    {
        Console.WriteLine("您的KFC汉堡");
    }
}
class KFC_Chicken:Chicken
{
    public override void deal()
    {
        Console.WriteLine("您的KFC炸鸡");
    }
}
class McDonalds_Chicken:Chicken
{
    public override void deal()
    {
        Console.WriteLine("您的麦当劳炸鸡");
    }
}
class KFC_Drink:Drink
{
    public override void deal()
    {
        Console.WriteLine("您的KFC饮料");
    }
}
class McDonalds_Drink:Drink
{
    public override void deal()
    {
        Console.WriteLine("您的麦当劳饮料");
    }
}
class Client
{
    static void Main(string[] args)
    {
        FoodFactory factory=new McDonaldsFactory();
        Burger burger = factory.CreateBurger();
        Chicken chicken = factory.CreateChicken();
        Drink drink = factory.CreateDrink();
        burger.deal();
        chicken.deal();
        drink.deal();
        Console.ReadLine();
    }
}
}

技术分享

 

抽象工厂模式

标签:

原文地址:http://www.cnblogs.com/123456yao/p/5097037.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!