zoukankan      html  css  js  c++  java
  • 设计模式之工厂方法模式

    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();
            }
        }
    }
  • 相关阅读:
    select/poll/epoll 对比
    I/O Mutiplexing poll 和 epoll
    Socket 编程IO Multiplexing
    ubuntu12.04 lts 安装gcc 4.8
    time since epoch
    ceph-RGW Jewel版新概念
    支持向量机(svm)
    MachineLearning之Logistic回归
    ML之回归
    ML之监督学习算法之分类算法一 ——— 决策树算法
  • 原文地址:https://www.cnblogs.com/wuhailong/p/4496405.html
Copyright © 2011-2022 走看看