zoukankan      html  css  js  c++  java
  • 设计模式笔记1:简单工厂模式

      如果想成为一名更优秀的软件设计师,了解优秀软件设计的演变过程比学习优秀设计本身更有价值。

    1.1 面向对象的好处

      通过封装、继承多态把程序的耦合度降低,使用设计模式使得程序更加灵活,容易修改,易于复用。

    1.2  类图

    1.3 代码

    几个运算类;

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 设计模式
     8 {
     9     abstract class Operator
    10     {
    11         public double NumberA { get; set; }
    12         public double NumberB { get; set; }
    13         public abstract double GetResult();
    14     }
    15 
    16     class Add : Operator
    17     {
    18 
    19         public override double GetResult()
    20         {
    21             return NumberA + NumberB;
    22         }
    23     }
    24 
    25     class Sub : Operator
    26     {
    27         public override double GetResult()
    28         {
    29             return NumberA - NumberB;
    30         }
    31     }
    32 
    33     class Mul : Operator
    34     {
    35         public override double GetResult()
    36         {
    37             return NumberA * NumberB;
    38         }
    39     }
    40 
    41     class Div : Operator
    42     {
    43         public override double GetResult()
    44         {
    45             if (NumberA == 0)
    46             {
    47                 throw new Exception("被除数不用为0");
    48             }
    49             return NumberA / NumberB;
    50         }
    51     }
    52 
    53 }
    View Code

    工厂代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 设计模式
     8 {
     9     class OperationFactory
    10     {
    11         public static Operator CreateOperator(string oper)
    12         {
    13             switch (oper)
    14             {
    15                 case "+": return new Add();
    16                 case "-": return new Sub();
    17                 case "*": return new Mul();
    18                 case "/": return new Div();
    19                 default: return null;
    20 
    21             }
    22         }
    23     }
    24 }
    View Code

    调用代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 设计模式
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.WriteLine("请输入运算数1:");
    14             double numberA = int.Parse(Console.ReadLine());
    15             Console.WriteLine("请输入运算数2:");
    16             double numberB = int.Parse(Console.ReadLine());
    17             Console.WriteLine("请输入运算符:");
    18             string oper = Console.ReadLine();
    19             // 实例化计算结果
    20             Operator calc=OperationFactory.CreateOperator(oper);
    21             calc.NumberA=numberA;
    22             calc.NumberB=numberB;
    23             Console.WriteLine("结果:" + calc.GetResult());
    24             Console.ReadKey(true);
    25         }
    26     }
    27 }
    View Code

    1.4  总结

      一个简简单单的计算功能。 要用我们的面向对象的思路做,也可以很精彩。  我们把功能定义到了计算的父类中。  然后由不用的子类去重写父类的方法实现不同的功能。两点好处:1、达到了功能的分割和封装。  2、多态,实际调用是调用某个子类的具体实现方法。

      而简单工厂类的作用就是帮助我们创建子类对象。  多么简单而又实用的设计。 这就是简单工厂设计模式。

       

  • 相关阅读:
    基础知识---抽象类和接口
    基础知识---数组和链表
    基础知识---枚举
    基础知识---IEnumerable、ICollection、IList、IQueryable
    [翻译]微软 Build 2019 正式宣布 .NET 5
    基础知识---const、readonly、static
    简说设计模式
    Java修行之路
    简说设计模式——迭代器模式
    简说设计模式——备忘录模式
  • 原文地址:https://www.cnblogs.com/simple-blog/p/4133897.html
Copyright © 2011-2022 走看看