• 简单工厂模式


    1.UML 类图

    1)首先,看‘动物’这个矩形框:

      

         第一层显示类的名称,如是抽象类

       第二层是类的特性,通常就是字段和属性

       第三层是类的操作,'+'表示public,'_'表示private,'#'表示protected

    2)

     

    interface IFly
    {
      void Fly();
    }
    interface ILanguage
    {
      void Speak();
    }

    3)继承关系用空三角形+实线来表示

     

    4)实现接口用空三角形+虚线来表示

     

    class Bird :Animal //继承动物类
    {
    }
    
    class WideGoose:IFly //实现飞翔接口
    {
    }

     5)关联:用实线箭头表示

     

    class Penguin:Bird
    {
      //在企鹅Penguin中,引用到气候climate 对象
      private Climate climate ;
    }

    6)聚合:表示一种弱的'拥有'关系,体现的是A对象可以包含B对象,但B对象不是A对象的一部分
               用空心的菱形+实线箭头表示

    class WideGooseAggregate
    {
       privat WideGoose[] arrayWideGoose;
    }

    7)组合(Composition):用实心的菱形+箭头表示
     

    class Bird
    {
      private Wing wing;
      public Bird
      {
        //在Bird类中,初始化时,实例化翅膀Wing,它们之间同时生成
         wing=new Wing();
       }
    }

    8)依赖关系(Dependency):用虚箭头表示

    abstract class Animal
    {
       public Metabolism(Oxygen oxygen,Water water)
      {
      }
    }

     2.简单工厂模式

       用一个单独的类来做这个创造实例的过程,就是工厂

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
    namespace OperationFactory
    {
       /// <summary>
       /// 基类
       /// </summary>
      public  class Operation
        {
          private double _numberA=0;
          private double _numberB=0;
          /// <summary>
          /// 数字A
          /// </summary>
          public double NumberA
          {
              get
              {
                  return _numberA;
              }
              set
              {
                  _numberA = value;
              }
          }
          /// <summary>
          /// 数字B
          /// </summary>
          public double NumberB
          {
              get
              {
                  return _numberB;
              }
              set
              {
                  _numberB = value;
              }
          }
          public virtual double GetResult()
          {
              double result=0;
              return result;
          }
    
        }
      class OperationAdd : Operation
      {
          public override double GetResult()
          {
              double result;
              result = NumberA + NumberB;
              return result;
          }
      }
      class OperationSub : Operation
      {
          public override double GetResult()
          {
              double result;
              result = NumberA + NumberB;
              return result;
          }
      }
      class OperationMul : Operation
      {
          public override double GetResult()
          {
              double result;
              result = NumberA * NumberB;
              return result;
             
          }
      }
      class OperationDiv : Operation
      {
          public override double GetResult()
          {
              double result;
              if (NumberB == 0)
                  throw new Exception("除数不能为零");
              result = NumberA / NumberB;
              return result;
          }
      }
      /// <summary>
      /// 简单运算工厂类
      /// </summary>
      public class OperationFactory
      {
          public static Operation CreateFactory(string operation)
          {
              Operation oper=null;
              switch (operation)
              {
                  case "+": oper = new OperationAdd();
                      break;
                  case "-": oper = new OperationSub();
                      break;
                  case "*": oper = new OperationMul();
                      break;
                  case "/": oper = new OperationDiv();
                      break;
              }
              return oper;
          }
      }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    
    namespace OperationFactory
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            
            static void Main(string[] args)
            {
                Console.Write("请输入数字A:");
                string strNumberA = Console.ReadLine();
                Console.Write("请选择运算符号(+、-、*、/):");
                string strOperate = Console.ReadLine();
                Console.Write("请输入数字B:");
                string strNumberB = Console.ReadLine();
                string strResult = "";
    
                Operation oper;
                oper = OperationFactory.CreateFactory(strOperate);
                oper.NumberA =Convert.ToDouble(strNumberA);
                oper.NumberB = Convert.ToDouble(strNumberB);
                strResult = oper.GetResult().ToString();
                Console.WriteLine("结果是:" + strResult);
                Console.ReadLine();
    
            }
        }
    }
    欢迎转载或分享,如果文章对你有帮助,请给予推荐,欢迎交流及关注!!!
  • 相关阅读:
    18文件权限管理
    17用户身份管理
    16Shell脚本—计划任务服务程序
    15Shell脚本—流程控制
    14Shell脚本—判断语句
    13Shell脚本—编写简单脚本
    12Vim在系统配置中的应用示例
    11Vim文本编辑器
    10重要的环境变量
    09命令行通配符和转义字符
  • 原文地址:https://www.cnblogs.com/dong897812629/p/2986965.html
Copyright © 2020-2023  润新知