zoukankan      html  css  js  c++  java
  • 简单工厂VS工厂方法

    简单工厂模式

    背景需求:

    用面向对象语言实现一个计算器控制台程序,要求输入两个数和运算符号,得到结果。

    标准:

    代码规范,通过封装、继承、多态把程序的耦合度降低,程序灵活,易于修改和复用。

    主要类图

    1、加减乘除类继承Operation运算类

    2、简单运算工厂类作为一个单独的类,用来创造实例。

    代码部分如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine("请输入数字A:");
                    string strNumberA = Console.ReadLine();
                    Console.WriteLine("请选择运算符号(+、-、*、/):");
                    string strOperate = Console.ReadLine();
                    Console.WriteLine("请输入数字B:");
                    string strNumberB = Console.ReadLine();
                    string strResult = "";
                    strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNumberB), strOperate));
                    Console.WriteLine("结果是:" + strResult);
                    Console.ReadLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("您输入有错:" + ex.Message);
                }
            }
        }
        //业务逻辑和界面逻辑分开
        //Operation运算类
        public class Operation
        {
            public static double GetResult(double numberA, double numberB, string operate)
            {
                double result = 0d;
                switch (operate )
                {
                    case "+":
                        result =numberA +numberB;
                        break;
                    case "-":
                        result =numberA -numberB;
                        break;
                     case "*":
                        result =numberA*numberB;
                        break;
                     case "/":
                        result =numberA/numberB;
                        break;
                } 
                return  result;
            }
           
        }
    }


    运行结果:

    其结构图为:



    而类似的例子在工厂方法模式的结构图为



    工厂方法模式

    背景需求:雷锋依然在人间。

    1、雷锋帮老人扫地、洗衣、买米等


    //雷锋类
        class LeiFeng
        {
            public void Sweep()
            {
                Console.WriteLine ("扫地");
            }
    
            public void Wash()
            { 
                Console.WriteLine("洗衣");
            }
    
            public void BuyRice()
            {
                Console.WriteLine("买米");
            }
        }
    2、三个学雷锋的大学生要代替他去做这些事情,并且大学生总要毕业的,这时候就要社区志愿者来帮忙干同样的事情了。
     //学雷锋的大学生,继承“雷锋”
        class Undergraduate : LeiFeng
        { }
        //社区志愿者
        class Volunteer : LeiFeng
        { }

    3、简单工厂类

    //简单雷锋工厂类
        class SimpleFactory
        {
            public static LeiFeng CreateLeiFeng(string type)
            {
                LeiFeng result = null;
                switch (type )
                    {
                        case  "学雷锋的大学生":
                        result =new Undergraduate ();
                        break ;
                        case "社区志愿者":
                        result =new Volunteer ();
                        break ;
                    }
                        return result ;
            }
        }

    4、每次实例化的时候写出这个工厂的代码,为了避免重复,我们尝试用工厂方法模式写一遍。

     //雷锋工厂
        interface IFactory
        {
            LeiFeng CreateLeiFeng();
        }
    
        //学雷锋的大学生工厂
        class UndergraduateFactory : IFactory
        {
            public LeiFeng CreateLeiFeng()
            {
                return new Undergraduate();
            
            }
        }
    
        //社区志愿者工厂
        class VolunteerFactory : IFactory
        {
            public LeiFeng CreateLeiFeng()
            {
                return new Volunteer();
            }
        }

    5、客户端调用

     static void Main(string[] args)
            {
                
                //工厂方法模式
                IFactory factory = new UndergraduateFactory();
                LeiFeng student = factory.CreateLeiFeng();
    
                student.BuyRice();
                student.Sweep();
                student.Wash();
            }


    运行结果:

  • 相关阅读:
    解决npm 下载速度慢的问题
    mongodb可视化工具mongobooster
    页面响应时间测试
    pyH支持python3
    python正则表达式
    Windows下TeX Live + Sublime Text 3 + Sumatra PDF配置
    Octocat,看着喜欢就都下载下来了
    [c#]记一次实验室局域网的ARP欺骗
    [FQ]Tor + Chrome + PAC 尝试 FQ
    [c#][福利]BTTool种子文件修改工具
  • 原文地址:https://www.cnblogs.com/saixing/p/6730351.html
Copyright © 2011-2022 走看看