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();
            }
        }
    }
  • 相关阅读:
    burpsuit学习--修改来源地址
    JPEG Exif 学习
    debug 和 release 版本间堆栈平衡的区别
    sscanf用法
    磁盘学习+MBR学习
    如何进行DLL调试
    Proj THUDBFuzz Paper Reading: Order Matters: Semantic Aware Neural Networks for Binary Code Similarity Detection
    Proj THUDBFuzz Paper Reading: VulSeeker-Pro: Enhanced Semantic Learning Based Binary Vulnerablity Seeker with Emulation
    Asynchronous Programming in Rust
    Proj THUDBFuzz Paper Reading: MemLock: Memory Usage Guided Fuzzing
  • 原文地址:https://www.cnblogs.com/wuhailong/p/4496405.html
Copyright © 2011-2022 走看看