美文网首页
设计模式(一)之简单工厂模式

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

作者: camellias__ | 来源:发表于2020-09-24 08:48 被阅读0次

简单工厂模式,他并不属于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部分,还需要增加一个类。

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

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

有好的建议,请在下方输入你的评论。

相关文章

  • iOS设计模式(三)之抽象工厂模式

    设计模式系列传送门 iOS设计模式(一)之简单工厂模式iOS设计模式(二)之工厂模式iOS设计模式(三)之抽象工厂...

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

    设计模式系列传送门 iOS设计模式(一)之简单工厂模式iOS设计模式(二)之工厂模式iOS设计模式(三)之抽象工厂...

  • iOS设计模式(二)之工厂模式

    设计模式系列传送门 iOS设计模式(一)之简单工厂模式iOS设计模式(二)之工厂模式iOS设计模式(三)之抽象工厂...

  • 简单工厂模式

    Android进阶之设计模式 简单工厂模式 简单工厂模式(又叫作静态工厂方法模式), 其属于创建型设计模式,但并不...

  • 设计模式之工厂模式

    设计模式之工厂模式 1. 简单工厂模式 1.1 模式定义 简单工厂模式又称为静态工厂方法,它属于创建型模式。在简单...

  • 设计模式之简单工厂、工厂方法、抽象工厂

    设计模式之简单工厂、工厂方法、抽象工厂 什么是设计模式? 设计模式(Design pattern)代表了最佳的实践...

  • 重学设计模式(五):《Head First设计模式》工厂模式

    前言 接上篇重学设计模式(四):《Head First设计模式》工厂模式之简单工厂 概念 工厂方法模式:定义了一个...

  • 2016.06笔记

    iOS设计模式之工厂模式(简单工厂,工厂方法,抽象工厂) 简单工厂:简单工厂模式的工厂类一般是使用静态方法,通过接...

  • 设计模式-工厂模式

    设计模式1 设计模式2 工厂模式 工厂模式可简单的分为三类:简单工厂,工厂方法,抽象工厂 简单工厂模式 定义 简单...

  • 设计模式一、单例模式

    系列传送门设计模式一、单例模式设计模式二、简单工厂模式设计模式三、工厂模式设计模式四、抽象工厂模式 简单单例(推荐...

网友评论

      本文标题:设计模式(一)之简单工厂模式

      本文链接:https://www.haomeiwen.com/subject/epifdktx.html