首页 > 其他分享 >CSharp: Abstract Factory Pattern in donet 6

CSharp: Abstract Factory Pattern in donet 6

时间:2022-11-06 07:11:09浏览次数:47  
标签:Pattern Abstract Factory System IMeal mealName using type public

 

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

namespace Geovinu.Du.DuAbstractFactory.Meals.Common
{

    /// <summary>
    ///抽象工厂 模式 Abstract Factory Pattern
    /// </summary>
    public interface IMeal
    {
        /// <summary>
        /// 
        /// </summary>
        void ShowAmountOfCalories();
        /// <summary>
        /// 
        /// </summary>
        void ShowDescription();
    }
}

    /// <summary>
    ///宫爆鸡丁 Kung Pao Chicken
    /// </summary>
    public class KungPao : IMeal
    {

        /// <summary>
        /// 热量
        /// </summary>
        public void ShowAmountOfCalories()
        {
            Console.WriteLine("热量: 899 卡");
        }
        /// <summary>
        /// 
        /// </summary>
        public void ShowDescription()
        {
            Console.WriteLine("宫爆鸡丁: 鲜鸡肉,酒,味精,盐,蒜,桂皮,八角等");
        }
    }

    /// <summary>
    /// 面条
    /// </summary>
    public class Noodles : IMeal
    {

        /// <summary>
        /// 热量 显示卡路里
        /// </summary>
        public void ShowAmountOfCalories()
        {
            Console.WriteLine("热量: 880 卡");
        }
        /// <summary>
        /// 
        /// </summary>
        public void ShowDescription()
        {
            Console.WriteLine("面条: 面,葱姜,老骨汤等.");
        }
    }


    /// <summary>
    /// 
    /// </summary>
    public class NullMeal : IMeal
    {
        /// <summary>
        /// 
        /// </summary>
        public void ShowAmountOfCalories()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        public void ShowDescription()
        {
        }
    }

using Geovin.Du.DuAbstractFactory.Meals;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Geovinu.Du.DuAbstractFactory.Meals.Common;



namespace Geovin.Du.DuAbstractFactory.Factories
{

    /// <summary>
    /// 
    /// </summary>
    public class MealFactory
    {
        /// <summary>
        /// 
        /// </summary>
        private Dictionary<string, Type> _meals;
        /// <summary>
        /// 
        /// </summary>
        public MealFactory()
        {
            LoadAvailableMealTypes();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mealName"></param>
        /// <returns></returns>
        public IMeal CreateMeal(string mealName)
        {
            var type = GetTypeForCreation(mealName);
            if (type == null)
            {
                return new NullMeal();
            }

            return Activator.CreateInstance(type) as IMeal;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="check"></param>
        /// <returns></returns>
        public IMeal CreateMealInt(string check)
        {
            string mealName = check=="1"? "kungpao" : "noodles";
            //if (check == "0")
            //    mealName = "KungPao";
            //if (check == "1")
            //    mealName = "KungPao";
            //if (check == "2")
            //    mealName = "Noodles";
            var type = GetTypeForCreation(mealName);
            if (type == null)
            {
                return new NullMeal();
            }

            return Activator.CreateInstance(type) as IMeal;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mealName"></param>
        /// <returns></returns>
        private Type GetTypeForCreation(string mealName)
        {
            if (!_meals.TryGetValue(mealName, out Type type))
            {
                return null;
            }

            return type;
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadAvailableMealTypes()
        {
            _meals = new Dictionary<string, Type>();

            Type[] assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();

            foreach (Type type in assemblyTypes)
            {
                if (type.GetInterface(typeof(IMeal).ToString()) != null)
                {
                    _meals.Add(type.Name.ToLower(), type);
                }
            }
        }
    }
}

    /// <summary>
    /// 
    /// </summary>
    public class Executor : PatternExecutor
    {

        /// <summary>
        /// 
        /// </summary>
        public override string Name => "/抽象工厂 模式 Abstract Factory Pattern (Simple Factory) - Creational Pattern";

        /// <summary>
        /// 
        /// </summary>
        public override void Execute()
        {
            Console.WriteLine("请你输入要点的餐数字编号 (1.KungPao,2.Noodles): ");
            string mealName = Console.ReadLine();

           MealFactory factory = new MealFactory();
            IMeal meal = factory.CreateMealInt(mealName);
             //IMeal meal = factory.CreateMeal(mealName);
            meal.ShowDescription();
            meal.ShowAmountOfCalories();
        }
    }

  

调用:

            Geovin.Du.DuAbstractFactory.Executor duex = new Geovin.Du.DuAbstractFactory.Executor();
            duex.Execute();

  

输出:

请你输入要点的餐数字编号 (1.KungPao,2.Noodles):
1
宫爆鸡丁: 鲜鸡肉,酒,味精,盐,蒜,桂皮,八角等
热量: 899 卡

  

 

标签:Pattern,Abstract,Factory,System,IMeal,mealName,using,type,public
From: https://www.cnblogs.com/geovindu/p/16861868.html

相关文章

  • CSharp: Factory Method Pattern in donet 6
     ///<summary>///TheProductdeclarestheinterface,whichiscommontoallobjects///thatcanbeproducedbythecreator<seecref="Restau......
  • 工厂(factory)
    本题显然是一个二分图模型,左边\(n\)个点表示工人,右边\(n\)个点表示机器,左右两个点有边当且仅当对应工人会操作该机器。本题所求的最终情况就等价于,任意一个极大匹配都......
  • org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
    报错:org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname'utilsServiceImpl':Unsatisfieddependencyexpressedthro......
  • 关于 QAbstractItemModel 学习的一点分享
    关于QAbstractItemModel学习的一点分享简介:Qt的模型、视图框架对处理一些复杂数据结构的可视化非常有用。Qt提供了三种常用的数据模型:QAbstractListModel(表单模型)、Q......
  • SpringMVC源码-创建FormattingConversionServiceFactoryBean
    一、FormattingConversionServiceFactoryBeanFormattingConversionServiceFactoryBean实现了FactoryBean接口,可以通过getObject获取ConversionService。FormattingConver......
  • java中的抽象类abstract
    /***学习抽象类和抽象方法*抽象方法:只进行方法的声明,而不提供具体的实现(没有方法体)*抽象类:只要在一个类中有一个抽象方法,那么这个就得声明一个抽象类*抽象类和......
  • SpringMVC源码-创建ContentNegotiationManagerFactoryBean
    一、ContentNegotiationManagerFactoryBean的BeanDifinition及实现的接口上图的属性说的是ContentNegotiationManagerFactoryBean对应的BeanDifinition中设置的属性,需要......
  • SpringMVC-mvc context重载AbstractApplicationContext.refresh方法部分
    一、postProcessBeanFactory(ConfigurableListableBeanFactorybeanFactory)AbstractRefreshableWebApplicationContext.postProcessBeanFactory(ConfigurableListableBea......
  • Checkerboard Pattern Shader
    写在前面:本文章为个人学习笔记,方便以后自己复习,也希望能帮助到他人。由于本人水平有限难免出现错误,还请评论区指出,多多指教。部分图元和素材来源于网络,如有侵权请联系本......
  • 008.从 XML 中构建 SqlSessionFactory
    1.在pom.xml文件中引入依赖<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</vers......