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();
            }
        }
    }
  • 相关阅读:
    java mail使用qq邮箱发邮件的配置方法
    (利用tempdata判断action是直接被访问还是重定向访问)防止微信活动中用户绕过关注公众号的环节
    判断浏览器为微信浏览器
    解决表单(搜索框)回车的时候直接提交了表单不运行js的问题
    传智播客JavaWeb day11--事务的概念、事务的ACID、数据库锁机制、
    传智播客JavaWeb day10-jdbc操作mysql、连接数据库六大步骤
    页面上常用的一些小功能--QQ、回到顶部
    手机端禁止网页页面放大代码
    Resharp注册码
    NueGet设置package Source
  • 原文地址:https://www.cnblogs.com/wuhailong/p/4496405.html
Copyright © 2011-2022 走看看