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

设计模式之工厂方法模式

时间:2015-05-12 11:06:06      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractFactoryPattern
{
    public class Pizza
    {
        public string name;
        internal void Prepare()
        {
            Console.WriteLine("准备"+name);
        }

        internal void Bake()
        {
            Console.WriteLine("烘焙" + name);
        }

        internal void Cut()
        {
            Console.WriteLine("切片" + name);
        }

        internal void Box()
        {
            Console.WriteLine("装盒" + name);
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractFactoryPattern
{
    public abstract class PizzaStore
    {

        public Pizza OrderPizza(string type)
        {
            Pizza pizza;
            pizza = this.CreatePizza(type);
            pizza.Prepare();
            pizza.Bake();
            pizza.Cut();
            pizza.Box();
            return pizza;
        }

        public abstract Pizza CreatePizza(string type);

    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractFactoryPattern
{
//必胜客Pizza店
    class PizzaHut:PizzaStore
    {
        Pizza pizza;
        public override Pizza CreatePizza(string type)
        {
            if (type == "芝士烤肉Pizza")
            {
                pizza = new Pizza();
                pizza.name = type;
            }
            else if (type == "蔬菜Pizza")
            {
                pizza = new Pizza();
                pizza.name = type;
            }
            else if (type == "海鲜Pizza")
            {
                pizza = new Pizza();
                pizza.name = type;
            }
            return pizza;
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractFactoryPattern
{
//来一家Pizza店
    class MrPizza:PizzaStore
    {
        Pizza pizza;
        public override Pizza CreatePizza(string type)
        {
            if (type == "MrPizza1号")
            {
                pizza = new Pizza();
                pizza.name = type;
            }
            else if (type == "MrPizza2号")
            {
                pizza = new Pizza();
                pizza.name = type;
            }
            else if (type == "MrPizza3号")
            {
                pizza = new Pizza();
                pizza.name = type;
            }
            return pizza;
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractFactoryPattern
{
    class Program
    {
    //开吃,生产Pizza
        static void Main(string[] args)
        {
            PizzaStore ps = new PizzaHut();
            ps.OrderPizza("芝士烤肉Pizza");
            Console.ReadLine();
        }
    }
}

 

设计模式之工厂方法模式

标签:

原文地址:http://www.cnblogs.com/wuhailong/p/4496405.html

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