1.

2.加法操作类

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. //加法操作类
  6. public class AddOperation : Operation
  7. {
  8. //重写父类方法
  9. public override double GetResult()
  10. {
  11. double dReult = 0;
  12. dReult = m_dNumberA + m_dNumberB;
  13. return dReult;
  14. }
  15. }

3.减法操作类

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. //减法操作类
  6. public class SubOperation : Operation
  7. {
  8. //重写父类方法
  9. public override double GetResult()
  10. {
  11. double dReult = 0;
  12. dReult = m_dNumberA - m_dNumberB;
  13. return dReult;
  14. }
  15. }

4.乘法操作类

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 乘法操作类
  6. /// </summary>
  7. public class RideOperation : Operation
  8. {
  9. public override double GetResult()
  10. {
  11. double dResult = 0;
  12. dResult = m_dNumberA * m_dNumberB;
  13. return dResult;
  14. }
  15. }

5.公共类

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Operation
  6. {
  7. public double m_dNumberA;
  8. public double m_dNumberB;
  9. //虚方法提供子类重写
  10. public virtual double GetResult()
  11. {
  12. double dResult = 0;
  13. return dResult;
  14. }
  15.  
  16. }
  17.  
  18. public class OperationFactory
  19. {
  20. public Operation CreateOperate(string str)
  21. {
  22. Operation operation = null;
  23. switch (str)
  24. {
  25. case "+":
  26. operation = new AddOperation ();
  27. break;
  28. case "-":
  29. operation = new SubOperation();
  30. break;
  31. case "*":
  32. operation = new RideOperation();
  33. break;
  34. default:
  35. break;
  36. }
  37. return operation;
  38. }
  39. }

6.使用

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class OperateTest : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. OperationFactory factory = new OperationFactory();
  11. Operation operation = factory.CreateOperate("+");
  12. operation.m_dNumberA = 5;
  13. operation.m_dNumberB = 2;
  14. double addResult = operation.GetResult();
  15. Debug.Log("加:" + addResult);
  16. Operation Suboperation = factory.CreateOperate("-");
  17. Suboperation.m_dNumberA = 10;
  18. Suboperation.m_dNumberB = 2;
  19. double subResult = Suboperation.GetResult();
  20. Debug.Log("减:" + subResult);
  21. Operation Rideoperation = factory.CreateOperate("*");
  22. Rideoperation.m_dNumberA = 2;
  23. Rideoperation.m_dNumberB = 2;
  24. double rideResult = Rideoperation.GetResult();
  25. Debug.Log("乘:" + rideResult);
  26.  
  27. //1+(1+2)+(1+2+3)+...+(1+2+3+...+10)之和
  28. int i = 1, j = 1, s = 0, s1 =0;
  29. while (j<=10)
  30. {
  31. while (i<=j)
  32. {
  33. s += i;
  34. i++;
  35. }
  36. s1 += s;
  37. j++;
  38. }
  39. Debug.Log(s1);
  40.  
  41. }
  42.  
  43. }

7.打印结果

8.好用的代码块

单例模式  代码如下

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 单例模式基类模块的作用主要是——减少单例模式重复代码的书写
  6. /// </summary>
  7. /// <typeparam name="T"></typeparam>
  8. public class BaseManager<T> where T :new ()
  9. {
  10. private static T _instance;
  11. public static T GetInstance()
  12. {
  13. if (_instance ==null)
  14. {
  15. _instance = new T();
  16. }
  17. return _instance;
  18. }
  19.  
  20. }

8.1 怎么使用单例,新建脚本继承它就行了  需要新建Resources文件夹 放两个预制体测试 一个Cube一个Sphere 具体使用情况自行扩展即可

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CeShi2 : BaseManager <CeShi2>
  6. {
  7. public int oko = 0;
  8. }

怎么继承单例的

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CeShi3 : BaseManager <CeShi3>
  6. {
  7. public bool bol = false;
  8. }

怎么继承单例的

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CeShi1 : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. CeShi2.GetInstance().oko++;
  11. CeShi3.GetInstance().bol = true;
  12. Debug.Log(CeShi2.GetInstance().oko
  13. +" " + CeShi3.GetInstance().bol);
  14. }
  15.  
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. if (Input.GetMouseButtonDown(0))
  20. {
  21. //向缓存池中拿东西
  22. PoolManager.GetInstance().GetObj("Cube");
  23. }
  24. if (Input.GetMouseButtonDown(1))
  25. {
  26. PoolManager.GetInstance().GetObj("Sphere");
  27. }
  28. Debug.Log(PoolManager.GetInstance().pool1Dic.Count);
  29. }
  30. }

怎么使用单例的

8.2 缓存池模块 代码如下 怎么调用的在上面脚本Update里面点击鼠标调用的缓存池拿东西

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 缓存池模块
  6. /// </summary>
  7. public class PoolManager : BaseManager <PoolManager>
  8. {
  9. //这里是缓存池模块
  10.  
  11. //创建字段存储容器
  12. public Dictionary<string, List<GameObject>> pool1Dic = new Dictionary<string, List<GameObject>>();
  13.  
  14. private GameObject poolObj;
  15. //取得游戏物体
  16. public GameObject GetObj(string name)
  17. {
  18. GameObject obj = null;
  19. //ContainsKey判断是否包含指定的“键名”
  20. //.count 获得符合条件的个数
  21. if (pool1Dic.ContainsKey(name) && pool1Dic[name].Count > 0)
  22. {
  23. //取得List中的第一个
  24. obj = pool1Dic[name][0];
  25. //移除第零个(这样才能允许同时创建多个物体)
  26. //这样才是真正的“拿出来”
  27. pool1Dic[name].RemoveAt(0);
  28. }
  29. else
  30. {
  31. //缓存池中没有该物体,我们去目录中加载
  32. //外面传一个预设体的路径和名字,我内部就去加载它
  33. //Resources类允许你从指定的路径查找或访问资源(api)
  34. obj = GameObject.Instantiate(Resources.Load<GameObject>(name));
  35. //创建对象后,将对象的名字与池中名字相符
  36. obj.name = name;
  37. }
  38. //让物体显示出来
  39. obj.SetActive(true);
  40. obj.transform.parent = null;
  41. return obj;
  42. }
  43.  
  44. //外界返还游戏物体
  45. public void PushObj(string name, GameObject obj)
  46. {
  47. if (poolObj == null)
  48. {
  49. poolObj = new GameObject("Pool");
  50.  
  51. }
  52. //将这个物体设置父亲为空物体
  53. obj.transform.parent = poolObj.transform;
  54.  
  55. //让物体失活
  56. obj.SetActive(false);
  57. //里面有记录这个键(有这个抽屉)
  58. if (pool1Dic.ContainsKey(name))
  59. {
  60. pool1Dic[name].Add (obj);
  61. }
  62. //未曾记录这个键(没有这个抽屉)
  63. else
  64. {
  65. pool1Dic.Add(name, new List<GameObject>() { obj });
  66. }
  67. }
  68. }

缓存池代码

8.3 下面加一个缓存池返还东西  脚本直接挂在物体上即可

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 挂到物体身上的
  6. /// </summary>
  7. public class DelPush : MonoBehaviour
  8. {
  9. void OnEnable()
  10. {
  11. //unity自带的延迟方法 在time秒后,延迟调用方法methodName
  12. Invoke("Push", 1);
  13. }
  14.  
  15. //放回去
  16. void Push()
  17. {
  18. PoolManager.GetInstance().PushObj(transform.name, this.gameObject);
  19. }
  20. }

挂在物体上的代码

简单记录一下

Unity 设计模式-简单工厂模式和其他好用的通用代码块的更多相关文章

  1. C++设计模式——简单工厂模式

    简单工厂模式(Simple Factory Pattern) 介绍:简单工厂模式不能说是一个设计模式,说它是一种编程习惯可能更恰当些.因为它至少不是Gof23种设计模式之一.但它在实际的编程中经常被用 ...

  2. 3. 星际争霸之php设计模式--简单工厂模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  3. 设计模式 — 简单工厂模式(staticFactory)

    这篇博文介绍简单工厂模式,设计模式并不是固定的二十三种,不同的书介绍的可能有出入,这篇介绍的简单工厂模式在有些书上就忽略不介绍了.设计模式是一套被反复使用的.多数人知晓的.经过分类编目的.代码设计经验 ...

  4. Golang设计模式—简单工厂模式(Simple Factory Pattern)

    Golang设计模式--简单工厂模式 背景 假设我们在做一款小型翻译软件,软件可以将德语.英语.日语都翻译成目标中文,并显示在前端. 思路 我们会有三个具体的语言翻译结构体,或许以后还有更多,但现在分 ...

  5. 深入浅出设计模式——简单工厂模式(Simple Factory)

    介绍简单工厂模式不能说是一个设计模式,说它是一种编程习惯可能更恰当些.因为它至少不是Gof23种设计模式之一.但它在实际的编程中经常被用到,而且思想也非常简单,可以说是工厂方法模式的一个引导,所以我想 ...

  6. C#设计模式--简单工厂模式

    简单工厂模式是属于创建型模式,但不属于23种GOF设计模式之一. 举一个例子:一个公司有不同的部门,客户根据需要打电话到不同的部门.客户相当于上端,不同部门相当于下端.不使用简单工厂模式来实现的例子如 ...

  7. 设计模式 | 简单工厂模式(static factory method)

    按理说应该把书全都看完一遍,再开始写博客比较科学,会有比较全面的认识. 但是既然都决定要按规律更新博客了,只能看完一个设计模式写一篇了. 也算是逼自己思考了,不是看完就过,至少得把代码自己都敲一遍. ...

  8. Yii2设计模式——简单工厂模式

    除了使用 new 操作符之外,还有更多的制造对象的方法.你将了解到实例化这个活动不应该总是公开进行,也会认识到初始化经常造成"耦合"问题. 应用举例 yii\db\mysql\Sc ...

  9. Yii2 设计模式——简单工厂模式

    除了使用 new 操作符之外,还有更多的制造对象的方法.你将了解到实例化这个活动不应该总是公开进行,也会认识到初始化经常造成“耦合”问题. 应用举例 yii\db\mysql\Schema 中: // ...

  10. 在商城系统中使用设计模式----简单工厂模式之在springboot中使用简单工厂模式

    1.前言: 不了解简单工厂模式请先移步:在商城中使用简单工厂.在这里主要是对springboot中使用简单工厂模式进行解析. 2.问题: 什么是简单工厂:它的实现方式是由一个工厂类根据传入的参数,动态 ...

随机推荐

  1. 关于CMDB

    关于CMDB: CMDB运维管理平台是由CMDB开发团队,针对目前服务器运维.监控,批量管理提出的一个开源. 易用.实用的跨平台服务器运维管理平台. 统筹来说cmdb就是将已有的规则化运维技术规则到运 ...

  2. python经典例题

    [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? num_list=[] cou=0 for i in range(1,5): for j in rang ...

  3. JS-变量存储

    1.存储(变量)JS中变量是存在栈内存中JS中的内存分两种:栈内存.堆内存 栈内存:存放变量 堆内存:存代码块(object和function) var fn=function()和function ...

  4. Mysql 索引心得

    1. 频繁查询的字段,应该创建索引. 2.更新非常频繁的字段,不应该创建索引. 3.唯一性太差的字段,比如 gender字段,就不应该创建索引. 4.不会出现在where条件之后的字段,不应该创建索引 ...

  5. docker compose服务编排简介、基于发布包构建多个webapi容器 和 基于镜像实现Nginx反向代理webapi

    一. docker compose服务编排简介 1. 背景 微服务架构的应用系统中一般包含若干个微服务,每个微服务一般都会部署多个实例,如果每个微服务都要手动启停,维护的工作量会很大: A. 要创建镜 ...

  6. 虚拟机redis无法连接

    1.cp redis.conf /etc/ 2.vi /etc/redis.conf 3.设置 Redis 可以后台运行 daemonize yes 4.关闭 redis 保护模式,使得可以远程连接 ...

  7. linux 上使用pm2启动nodejs服务

    1.安装pm2: npm install -g pm2 2.在启动文件夹内新建文件processes.json: {   "apps": [     {       "n ...

  8. class_task

    #!/usr/bin/python # -*- coding: UTF-8 -*- class Task():     _cls_name = "cls name"     def ...

  9. 调度器45—wake_affine

    基于 Linux-5.10 一.wake_affine 简介 1. 背景 在进程唤醒选核路径中, wake_affine 倾向于将被唤醒进程(wakee)尽可能安排在 waker所在 CPU 上, 这 ...

  10. libnode使用addon

    自己编译的一个libnode.so后,在js里调用hello.node的 addon时候会报错. Error: dlopen failed: cannot locate symbol "na ...