设计模式(一)之简单工厂模式

178 阅读4分钟

这是我参与8月更文挑战的第1天,活动详情查看:8月更文挑战

简单工厂模式,他并不属于23种设计模式; 它的实现和它的名字气质很符; 就是简单;

先来说下应用场景:当你不确定,有多少种操作的时候,例如:计算器中的 + - * /

我们可以使用简单工厂模式。

我们就以上边说过的加减乘除运算举例:建立一个控制台应用,输入两个数字和一个运算符,得到结果。

不好的实例:我这里使用C#语言编写程序

static void Main(string[] args)
        {
            // 数字A
            double strNumberA;
            // 数字B
            double strNumberB;
            // 运算符
            string signStr;
            // 运算结果
            double result;
            try
            {
                Console.WriteLine("请输入第一个数字");
                strNumberA = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入运算符号");
                signStr = Console.ReadLine();
                Console.WriteLine("请输入第二个数字");
                strNumberB = Convert.ToDouble(Console.ReadLine());
                switch (signStr)
                {
                    case "+":
                        result = strNumberA + strNumberB;
                        break;
                    case "-":
                        result = strNumberA - strNumberB;
                        break;
                    case "*":
                        result = strNumberA * strNumberB;
                        break;
                    default:
                        Console.WriteLine("暂不支持您输入的运算符");
                        break;
                }
                Console.WriteLine("运算结果为:" + result);
                Console.ReadLine();
            }
            catch (Exception qq )
            {
                // 输出错误信息
                Console.WriteLine(qq.Message);
            }
        }

上边的实例可以满足要求。但是加减乘除的操作在别的地方也会用到,如果像上边这样写,在别的地方用到的时候,还是需要将对应的功能重新写一次,这就造成了代买的冗余,后期维护的时候,需求改变,需要改多个地方。

使用面向对象程序设计思想,同过多态,继承,封装吧程序的耦合性降低。后期维护更容易修改,并且易于复用。

我们把运算部分的方法封装起来,将业务逻辑和界面逻辑分开。

代码如下所示:

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            // 数字A
            double strNumberA;
            // 数字B
            double strNumberB;
            // 运算符
            string signStr;
            // 运算结果
            double result;
            try
            {
                Console.WriteLine("请输入第一个数字");
                strNumberA = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入运算符号");
                signStr = Console.ReadLine();
                Console.WriteLine("请输入第二个数字");
                strNumberB = Convert.ToDouble(Console.ReadLine());
  
                switch (signStr)
                {
                    case "+":
                        Plus plus = new Plus();
                        result = plus.PlusPlus(strNumberA, strNumberB);
                        break;
                    case "-":
                        Cut cut = new Cut();
                        result = cut.CutCut(strNumberA, strNumberB);
                        break;
                    case "*":
                        Ride ride = new Ride();
                        result = ride.RideRide(strNumberA, strNumberB);
                        break;
                    default:
                        result = 0;
                        Console.WriteLine("暂不支持您输入的运算符");
                        break;
                }
                Console.WriteLine("运算结果为:" + result);
                Console.ReadLine();
            }
            catch (Exception qq )
            {
                // 输出错误信息
                Console.WriteLine(qq.Message);
            }
        }
    }
}

Plus.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    public class Plus
    {
        public double result;
        public double PlusPlus(double strNumberA, double strNumberB)
        {
            result = strNumberA + strNumberB;
            return result;
        }
    }
}

Cut.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    public class Cut
    {
        public double result;
        public double CutCut(double strNumberA, double strNumberB)
        {
            result = strNumberA - strNumberB;
            return result;
        }
    }
}

Ride.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    public class Ride
    {
        public double result;
        public double RideRide(double strNumberA, double strNumberB)
        {
            result = strNumberA * strNumberB;
            return result;
        }
    }
}

注意主程序中我分别定义了三个算法类的对象,这样写也不是特别的好。

现在,主角登场了:简单工厂模式,通俗点说,就是有一个基类(class abstract interface)以上三种类型都可以,在基类中定义方法,由子类来实现重写或者实现,那我们声明变量类型的时候,直接声明成这个基类的类型就好了。

我这里使用接口(interface)

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            // 数字A
            double strNumberA;
            // 数字B
            double strNumberB;
            // 运算符
            string signStr;
            // 运算结果
            double result;
            try
            {
                Console.WriteLine("请输入第一个数字");
                strNumberA = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("请输入运算符号");
                signStr = Console.ReadLine();
                Console.WriteLine("请输入第二个数字");
                strNumberB = Convert.ToDouble(Console.ReadLine());
                // 使用工厂类返回实例化子类的对象(因为子类中都是重写基类中的方法,指定类型的时候,直接声明基类就可以)
                Ireckon reckon = Factory.CreateTreckon(signStr);
                // 调用
                result = reckon.getResult(strNumberA, strNumberB);
                Console.WriteLine("运算结果为:" + result);
            }
            catch (Exception qq )
            {
                // 输出错误信息
                Console.WriteLine(qq.Message);
            }
            Console.ReadLine();
        }
    }
}
 

计算类:基类接口

Ireckon.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    public interface Ireckon
    {
        double getResult(double strNumberA, double strNumberB);
    }
}
 

乘类:子类

Ride.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    public class Ride:Ireckon
    {
        public double result;
        public double getResult(double strNumberA, double strNumberB)
        {
            result = strNumberA * strNumberB;
            return result;
        }
    }
}

减类:子类

Cut.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    public class Cut:Ireckon
    {
        public double result;
        public double getResult(double strNumberA, double strNumberB)
        {
            result = strNumberA - strNumberB;
            return result;
        }
    }
}

加类:子类

Plus.cs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    public class Plus:Ireckon
    {
        public double result;
        public double getResult(double strNumberA, double strNumberB)
        {
            result = strNumberA + strNumberB;
            return result;
        }
    }
}

工厂类:

Factory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace simpleFactory
{
    public class Factory
    {
        public static Ireckon CreateTreckon(string signStr)
        {
            Ireckon reckon = null;
  
            switch (signStr)
            {
                case "+":
                    reckon = new Plus();
                    break;
                case "-":
                    reckon = new Cut();
                    break;
                case "*":
                    reckon = new Ride();
                    break;
                default:
                    Console.WriteLine("暂不支持您输入的运算符");
                    break;
            }
            return reckon;
        }
  
    }
}
 

上边测试使用的代码的精髓就在于接口那一部分:

public interface Ireckon
    {
        double getResult(double strNumberA, double strNumberB);
    }

简单工厂模式的精髓就是在基类(abstract,interface ,class)中定义一个方法,由其子类来实现或者重写他。将逻辑计算部分封装成一个工厂类,工厂类只返回对应的子类的对象。再由这个对象调用其下的方法。

工厂类那部分代码,完全可以写到主程序中,但是这样会提高主程序代码的耦合性,所以说,简单工厂类还是需要有的。

上边的代码看似很美好。

然而又一个问题呀,

我们在增加一个功能的时候,比如除。

我们需要修改工厂类中的switch-case部分,还需要增加一个类。

这个也可以说是耦合性很高的表现。

所以,有了工厂模式的出现。后边会看到。

欢迎访问个人博客 guanchao.site

欢迎访问小程序:

在这里插入图片描述