ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【23种GOF设计模式】C#代码完整案例详解--工厂方法

2022-01-27 17:33:45  阅读:130  来源: 互联网

标签:23 C# IRace System War3 new using FactoryPattern 设计模式


来自:CCNetCore社区,一个关注.Netcore领域的社区团队。

工厂方法FactoryMethod

工厂方法FactoryMethod 创建型设计模式

是简单工厂的进阶,单纯将简单工厂一个类分开成多给类,将之前的一个类的耦合进行解耦。
每一个类中都有创建实例的方法,而不是通过枚举进行判断。通过调用不同工厂来返回不同的对象即可。

Program.cs

using FactoryPattern.War3.Interface;
using FactoryPattern.War3.Service;
using FactoryPattern.War3.ServiceExtend;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethodPattern
{
   /// <summary>
   /// 1 对比简单工厂,建立工厂方法(FactoryMethod)
   /// 2 工厂方法的优缺点和应用
   /// </summary>
   class Program
   {
       static void Main(string[] args)
       {
           try
           {
               {
                   Human human = new Human();
                   Undead undead = new Undead();
                   NE ne = new NE();
                   ORC orc = new ORC();

                   //Six six=new Six()//参数信息很麻烦

               }
               {
                   IRace human = new Human();
                   IRace undead = new Undead();
                   IRace ne = new NE();
                   IRace orc = new ORC();
               }
               {
                   IFactory humanFactory = new HumanFactory();
                   IRace human = humanFactory.CreateInstance();

                   IFactory fiveFactory = new FiveFactory();
                   IRace five = fiveFactory.CreateInstance();

                   IFactory sixFactory = new SixFactoryExtend();// new SixFactory();

                   IRace six = sixFactory.CreateInstance();
               }
           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.Message);
           }
           Console.Read();
       }
   }
}

SixFactory.cs

using FactoryPattern.War3.Interface;
using FactoryPattern.War3.Service;
using FactoryPattern.War3.ServiceExtend;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethodPattern
{
   /// <summary>
   /// 工厂类    框架原始工厂
   /// </summary>
   public class SixFactory : IFactory
   {
       public virtual IRace CreateInstance()
       {
           IRace race = new Six("Yoyo", 123, new Undead(), new Undead(), new Human(), new NE());//一些具体业务
           return race;
       }
   }


   /// <summary>
   /// 我们扩展的工厂
   /// </summary>
   public class SixFactoryExtend : SixFactory
   {
       public override IRace CreateInstance()
       {
           Console.WriteLine("这里是我们扩展的工厂");

           return base.CreateInstance();
       }
   }

}

IFactory.cs

using FactoryPattern.War3.Interface;
using FactoryPattern.War3.Service;
using FactoryPattern.War3.ServiceExtend;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FactoryMethodPattern
{
   /// <summary>
   /// 工厂类
   /// </summary>
   public interface IFactory
   {
       IRace CreateInstance();
   }

}

IRace.cs

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

namespace FactoryPattern.War3.Interface
{
   public interface IRace
   {
       void ShowKing();
   }
}

Six.cs

using FactoryPattern.War3.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryPattern.War3.Service
{
   /// <summary>
   /// war3种族之一
   /// </summary>
   public class Human : IRace
   {
       public void ShowKing()
       {
           Console.WriteLine("The King of {0} is Sky", this.GetType().Name);
       }
   }
}

最后,想了解更多,可加入CCNetCore社区,橙子带你走上.netcore学习之路。
你可以免费获取到:

  • 设计模式
  • 面试八股文
  • 问题答疑
  • 闲聊茶馆
  • Asp.Netcore图文源码解读
  • 第一代软件架构单体应用相关技术
  • 第二代软件架构分布式应用相关技术
  • 第三代软件架构微服务应用相关技术
  • 第四代软件架构网格服务应用相关技术

站点网址:ccnetcore.com

标签:23,C#,IRace,System,War3,new,using,FactoryPattern,设计模式
来源: https://www.cnblogs.com/ccnetcore/p/15850560.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有