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

来自: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

posted @ 2022-01-27 17:27  CCNetCore  阅读(49)  评论(0编辑  收藏  举报