您的位置:首页 > 教育 > 锐评 > 微信公众号创建流程_天华集团设计公司_国内好的seo_净水器十大品牌

微信公众号创建流程_天华集团设计公司_国内好的seo_净水器十大品牌

2025/5/7 9:00:32 来源:https://blog.csdn.net/qq_42603590/article/details/144204518  浏览:    关键词:微信公众号创建流程_天华集团设计公司_国内好的seo_净水器十大品牌
微信公众号创建流程_天华集团设计公司_国内好的seo_净水器十大品牌

系列文章目录

unity知识点


文章目录

  • 系列文章目录
  • 👉前言
  • 👉一、桥接模式(Bridge)
  • 👉二、适配器模式(Adapter)
  • 👉三、 外观模式(Facade)
  • 👉四、原型模式(Prototype)
  • 👉五、建造者模式(Builder)
  • 👉壁纸分享
  • 👉总结


请添加图片描述

👉前言

设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样。项目中合理地运用 设计模式可以完美地解决很多问题,每种模式在现实中都有相应的原理来与之对应,每种模式都描述了一个在我们周围不断重复发生的问题,以及该问题的核心解决 方案,这也是设计模式能被广泛应用的原因。

博客将会介绍Unity中常用的设计模式,每种设计模式都会给出详细的示例代码。希望这篇博客对Unity的开发者有所帮助。
大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。
欢迎点赞评论哦.下面就让我们进入正文吧 !


提示:以下是本篇文章正文内容,下面案例可供参考

👉一、桥接模式(Bridge)

介绍:
Unity中的桥接模式(Bridge Pattern)是一种设计模式,主要用于将抽象部分与其实现部分分离,从而允许它们独立变化‌。这种模式特别适用于那些需要处理多个维度变化的情况,例如在游戏中角色和装备的多样化组合。
桥接模式的核心思想是将抽象部分与实现部分分离,使得两者可以独立变化。在Unity中,这通常用于处理角色和装备的关系,使得每当新增角色或装备时,不需要修改已有的代码,从而减少代码的重复和维护难度‌。

代码如下:

public abstract class Shape 
{protected IDrawAPI _drawAPI;protected Shape(IDrawAPI drawAPI) {_drawAPI = drawAPI;}public abstract void Draw();
}public interface IDrawAPI 
{void DrawCircle(int x, int y, int radius);
}public class DrawAPI1 : IDrawAPI 
{public void DrawCircle(int x, int y, int radius) {Debug.Log("DrawAPI1 Circle " + x + "," + y + "," + radius);}
}public class DrawAPI2 : IDrawAPI 
{public void DrawCircle(int x, int y, int radius){Debug.Log("DrawAPI2 Circle " + x + "," + y + "," + radius);}
}public class Circle : Shape 
{private int _x, _y, _radius;public Circle(int x, int y, int radius, IDrawAPI drawAPI) : base(drawAPI) {_x = x;_y = y;_radius = radius;}public override void Draw() {_drawAPI.DrawCircle(_x, _y, _radius);}
}

👉二、适配器模式(Adapter)

介绍:
‌Unity中的适配器模式‌主要用于将一个类的接口转换为另一个接口,以便原本不兼容的对象能够协同工作。适配器模式在Unity中非常有用,特别是在处理不同版本的API、不同组件间的交互、对接外部库等场景中。
假设有一个老版本的输入系统,它直接处理键盘按键事件,而新的Unity Input System需要一个特定的接口。可以通过创建一个适配器来使老版输入系统与新接口兼容。

代码如下:

public interface ITarget 
{void Request();
}public class Adaptee 
{public void SpecificRequest() {Debug.Log("Specific Request");}
}public class Adapter : ITarget 
{private Adaptee _adaptee = new Adaptee();public void Request() {_adaptee.SpecificRequest();}
}

👉三、 外观模式(Facade)

介绍:
外观模式用于为复杂的子系统提供一个简单的接口。在Unity中,外观模式常用于将复杂的系统或流程封装起来,简化客户端的调用,例如场景管理器和资源管理器。

代码如下:

public class SceneLoader 
{public void LoadScene(string sceneName) {Debug.Log("Loading Scene " + sceneName);}
}public class SoundManager
{public void PlaySound(string soundName) {Debug.Log("Playing Sound " + soundName);}
}public class ResourceManager 
{public void LoadResource(string resourceName) {Debug.Log("Loading Resource " + resourceName);}
}public class GameFacade 
{private SceneLoader _sceneLoader;private SoundManager _soundManager;private ResourceManager _resourceManager;public GameFacade() {_sceneLoader = new SceneLoader();_soundManager = new SoundManager();_resourceManager = new ResourceManager();}public void StartGame(string sceneName, string soundName, string resourceName) {_sceneLoader.LoadScene(sceneName);_soundManager.PlaySound(soundName);_resourceManager.LoadResource(resourceName);}
}

👉四、原型模式(Prototype)

介绍:
Unity中的原型模式‌是一种设计模式,允许通过复制现有的对象来创建新对象,而不是通过直接实例化类。这种模式的核心思想是通过创建一个对象的副本(即原型)来避免昂贵的初始化操作,从而提高性能和资源利用率。

原型模式的工作原理是定义一个原型接口或基类,提供克隆方法(如 Clone)。具体的类实现该接口,提供自己的克隆方法,该方法返回当前对象的深拷贝或浅拷贝。客户端可以通过克隆原型对象来创建新对象,而无需知道如何构造对象。拷贝的类型可以分为浅拷贝和深拷贝:

‌浅拷贝‌:只复制对象的基本数据类型,对于引用类型的字段,只复制引用地址。
‌深拷贝‌:不仅复制对象的所有值,还递归地复制所有引用对象,创建独立的副本。

代码如下:

public abstract class Prototype 
{public string Type;public abstract Prototype Clone();
}public class ConcretePrototypeA : Prototype 
{public ConcretePrototypeA() {Type = "A";}public override Prototype Clone() {return (Prototype)MemberwiseClone();}
}public class ConcretePrototypeB : Prototype 
{public ConcretePrototypeB() {Type = "B";}public override Prototype Clone(){return (Prototype)MemberwiseClone();}
}public class PrototypeManager 
{private Dictionary<string, Prototype> _prototypes = new Dictionary<string, Prototype>();public Prototype this[string key]{get { return _prototypes[key].Clone(); }set { _prototypes.Add(key, value); }}
}

👉五、建造者模式(Builder)

介绍:
‌Unity中的建造者模式‌是一种创建型设计模式,主要用于创建复杂的对象。通过分离对象的构建过程,建造者模式允许以更灵活和可扩展的方式创建不同变体的对象,尤其适用于需要构造具有多个可选或必须组件的对象,并且希望客户端代码无需了解内部构造细节的情况‌。
在Unity中,建造者模式常用于创建复杂的Prefab,例如角色或NPC。

代码如下:

public class Character 
{private string _name;private string _gender;private string _race;private string _class;private int _level;private int _health;private int _mana;public string Name {get { return _name; }set { _name = value; }}public string Gender {get { return _gender; }set { _gender = value; }}public string Race {get { return _race; }set { _race = value; }}public string Class {get { return _class; }set { _class = value; }}public int Level {get { return _level; }set { _level = value; }}public int Health {get { return _health; }set { _health = value; }}public int Mana {get { return _mana; }set { _mana = value; }}
}public abstract class CharacterBuilder 
{protected Character _character;public Character Character {get { return _character; }}public virtual void BuildName(string name) {_character.Name = name;}public virtual void BuildGender(string gender){_character.Gender = gender;}public virtual void BuildRace(string race) {_character.Race = race;}public virtual void BuildClass(string className) {_character.Class = className;}public virtual void BuildLevel(int level) {_character.Level = level;}public virtual void BuildHealth(int health) {_character.Health = health;}public virtual void BuildMana(int mana) {_character.Mana = mana;}
}public class HumanBuilder : CharacterBuilder 
{public HumanBuilder(){_character = new Character();}public override void BuildRace(string race){if (race.Equals("Human")){_character.Race = race;}else{throw new System.Exception("Invalid race for Human builder.");}}public override void BuildClass(string className){if (className.Equals("Warrior") || className.Equals("Mage")){_character.Class = className;}else{throw new System.Exception("Invalid class for Human builder.");}}
}public class CharacterDirector 
{private CharacterBuilder _builder;public CharacterDirector(CharacterBuilder builder) {_builder = builder;}public void Construct(string name, string gender, string race, string className, int level, int health, int mana) {_builder.BuildName(name);_builder.BuildGender(gender);_builder.BuildRace(race);_builder.BuildClass(className);_builder.BuildLevel(level);_builder.BuildHealth(health);_builder.BuildMana(mana);}
}

请添加图片描述

👉壁纸分享

请添加图片描述
请添加图片描述


👉总结

本次总结的就是unity常见的五种设计模式 有需要会继续增加功能
如能帮助到你,就帮忙点个赞吧,三连更好哦,谢谢
你的点赞就是对博主的支持,有问题记得留言评论哦!
不定时更新Unity开发技巧,觉得有用记得一键三连哦。么么哒!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com