一、单例模式:它保证一个类只有一个实例,且提供一个全局访问点。使用单例模式是为了节约资源,避免多个实例的产生导致不必要的开销。
1.使用泛型创建通用单例脚本(不能挂载)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//where如果约束为MonoBehaviour,则T可以是派生子MonoBehaviour的组件或脚本
public class SingleMono<T> : MonoBehaviour where T:new()
{
private static T instance;
public static T Instance
{
get
{
if(instance == null) instance = new T();
return instance;
}
}
}
2.不使用泛型创建单例脚本
(1)真单例:在多线程环境下,保证只有一个实例对象,并且线程安全(可以跨场景调用)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RealSingle
{
private static RealSingle instance;
public static RealSingle Instance
{
get
{
if(instance == null)
{
instance = new RealSingle();
}
return instance;
}
}
}
(2)伪单例:适合在单线程环境下使用,多线程环境容易造成多个实例对象的产生(不能跨场景调用,因为它继承了MonoBehaviour)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FakeSingle : MonoBehaviour
{
public static FakeSingle instance;
private void Awake()
{
instance = this;
}
}
更多内容:单例模式 | 菜鸟教程
二、观察者模式:它定义了一种一对多的依赖关系,让多个观察者对象同时监听 某一个主题对象,当主题对象状态发生改变时,所有依赖者(观察者)都会收到通知并自动更新
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObserverDemo : MonoBehaviour
{
public delegate void CallBack(object param);
public event CallBack callBackEvent;
Dictionary<int,List<CallBack>> dicEvent=new Dictionary<int,List<CallBack>>();
//添加事件方法
public void AddEvent(int eventID,CallBack callBack)
{
if(!dicEvent.ContainsKey(eventID))
{
List<CallBack> list = new List<CallBack>();
list.Add(callBack);
dicEvent.Add(eventID, list);
callBackEvent += callBack;
}
else
{
if (!dicEvent[eventID].Contains(callBack))
{
dicEvent[eventID].Add(callBack);
callBackEvent += callBack;
}
else
{
return;
}
}
}
//删除事件方法
public void DelEvent(int eventID,CallBack callBack)
{
if(!dicEvent.ContainsKey(eventID)) return;
if (!dicEvent[eventID].Contains(callBack)) return;
dicEvent[eventID].Remove(callBack);
callBackEvent -= callBack;
if (dicEvent[eventID].Count==0)
{
dicEvent.Remove(eventID);
}
}
//通过委托来通知某个eventID下的CallBack组
public void SendEvent(int eventID,object param)
{
if (dicEvent[eventID].Count>0)
{
foreach(var item in dicEvent[eventID])
{
item(param);
}
}
}
//通过事件来通知所有CallBack
public void SendAllEvent(object param)
{
callBackEvent(param);
}
private void Start()
{
AddEvent(0,Show);
object temp=null;
SendEvent(0, temp);
SendAllEvent(temp);
DelEvent(0, Show);
}
void Show(object param)
{
Debug.Log("更新");
}
}
更多内容:观察者模式 | 菜鸟教程
三、工厂模式:通过定义一个工厂类来创建对象,而不是直接在代码中创建对象。它可以隐藏对象创建的细节,从而使代码更加灵活和可维护。
抽象类:不能被实例化。可以包括抽象方法和非抽象方法,且抽象方法中不能有方法体,抽象类一经继承,就必须实现抽象类中的抽象方法(抽象类继承抽象类除外)
接口:属于引用类型。不能实例化,且不能包含属性字段,只能写方法声明,不能有存在方法体的方法。一经继承,必须实现接口中的所有方法声明(接口继承接口除外)
(1)简单工厂模式:只有一个工厂类来创建所有对象。缺乏灵活性,无法扩展。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FactoryDemo : MonoBehaviour
{
private void Start()
{
Factory factory = GetComputer(Computer.Lenovo);
factory.FactoryObject();
factory = GetComputer(Computer.Huawei);
factory.FactoryObject();
}
public Factory GetComputer(Computer com)
{
Factory factory = null;
switch (com)
{
case Computer.Lenovo:factory = new Lenovo();break;
case Computer.Huawei: factory = new HuaWei(); break;
default: Debug.Log("工厂无法生产"); break;
}
return factory;
}
}
public enum Computer {Lenovo,Huawei}
public abstract class Factory
{
public string name = "电脑";
//虚函数可重写可不重写
public virtual void FactoryObject()
{
Debug.Log("生产" + name);
}
}
class Lenovo:Factory
{
public Lenovo()
{
name = "联想";
}
}
class HuaWei:Factory
{
public HuaWei()
{
name = "华为";
}
}
(2)复杂工厂模式:通常有多个工厂类来创建不同种类的对象。更加容易扩展和修改
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ComplexFactoryDemo : MonoBehaviour
{
private void Start()
{
FactoryElectronics factory = new Factory1();
factory.CreateComputer().GetInfo();
factory.CreatePhone().CallInfo();
factory=new Factory2();
factory.CreateComputer().GetInfo();
factory.CreatePhone().CallInfo();
}
}
interface IElectronics
{
void SameInfo();
}
interface IComputer : IElectronics
{
void GetInfo();
}
interface IPhone : IElectronics
{
void CallInfo();
}
class LenoveComputer : IComputer
{
public void GetInfo()
{
Debug.Log("联想电脑");
}
public void SameInfo()
{
Debug.Log("这是电子产品");
}
}
class AppleComputer : IComputer
{
public void GetInfo()
{
Debug.Log("苹果电脑");
}
public void SameInfo()
{
Debug.Log("这是电子产品");
}
}
class HuaWei : IPhone
{
public void CallInfo()
{
Debug.Log("华为手机");
}
public void SameInfo()
{
Debug.Log("这是电子产品");
}
}
class Apple : IPhone
{
public void CallInfo()
{
Debug.Log("苹果手机");
}
public void SameInfo()
{
Debug.Log("这是电子产品");
}
}
interface FactoryElectronics
{
IComputer CreateComputer();
IPhone CreatePhone();
}
class Factory1 : FactoryElectronics
{
public IComputer CreateComputer()
{
return new LenoveComputer();
}
public IPhone CreatePhone()
{
return new HuaWei();
}
}
class Factory2 : FactoryElectronics
{
public IComputer CreateComputer()
{
return new AppleComputer();
}
public IPhone CreatePhone()
{
return new Apple();
}
}
更多内容:工厂模式 | 菜鸟教程
四、策略模式:将算法的选择和算法的实现分开,使算法可以独立于使用它的客户端而变化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StrategyDemo : MonoBehaviour
{
private void Start()
{
PeopleAttack peopleAttack = new PeopleAttack();
peopleAttack.hitDamage(new Weapon1()).HitAttack();
}
}
interface WeaponSkill
{
void HitAttack();
}
class Weapon1 : WeaponSkill
{
public void HitAttack()
{ Debug.Log("攻击距离1"); }
}
class Weapon2 : WeaponSkill
{
public void HitAttack()
{ Debug.Log("攻击距离2"); }
}
class Weapon3 : WeaponSkill
{
public void HitAttack()
{ Debug.Log("攻击距离3");}
}
class PeopleAttack
{
public WeaponSkill hitDamage(WeaponSkill weaponSkill)
{
weaponSkill = new Weapon3();
return weaponSkill;
}
}
更多内容:策略模式 | 菜鸟教程
五、组合模式:可以使用户处理单个对象一样来处理对象的组合,从而使得用户在使用对象时可以忽略对象与组合之间的差别
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CompositeDemo : MonoBehaviour
{
private void Start()
{
//就是给某些物体加多个或给另外一些物体加一个
this.gameObject.AddComponent<RunInfo>();
this.gameObject.AddComponent<RunInfo>();
}
}
public class RunInfo:MonoBehaviour
{ public void Run() { } }
public class WalkInfo : MonoBehaviour
{ public void Walk() { } }
public class JumpInfo : MonoBehaviour
{ public void Jump() { } }
更多内容:组合模式 | 菜鸟教程
结语:合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。