32. 代码优化

1.代码优化


1.代码优化

csharp 复制代码
基于枚举创建不同对象

1).根据工厂和字典模式

public static class BuffEffectFactory
{
    // 字典:枚举 -> 对象构造委托
    private static readonly Dictionary<BuffEffect, Func<BuffEffectBase>> _effectCreators = new();

    static BuffEffectFactory()
    {
        // 预注册所有Buff类型
        _effectCreators.Add(BuffEffect.RebuildCard, () => new RebuildBuffEffect());
        _effectCreators.Add(BuffEffect.ControlDice, () => new ControlDiceBuffEffect());
        _effectCreators.Add(BuffEffect.BuildLvVariation, () => new BuildLvBuffEffect());
        _effectCreators.Add(BuffEffect.AddBuffByCondition, () => new AddBuffEffect());
        _effectCreators.Add(BuffEffect.DirectionChange, () => new DirectionChangeBuffEffect());
    }

    public static BuffEffectBase Create(BuffEffect effectType)
    {
        if (_effectCreators.TryGetValue(effectType, out var creator))
        {
            return creator();
        }
        throw new ArgumentOutOfRangeException(nameof(effectType), $"未注册的Buff类型:{effectType}");
    }
}

csharp 复制代码
2).通过特性和反射的方式

// 1. 定义特性:标记类对应的枚举值
[AttributeUsage(AttributeTargets.Class)]
public class BuffEffectAttribute : Attribute
{
    public BuffEffect EffectType { get; }
    public BuffEffectAttribute(BuffEffect effectType) => EffectType = effectType;
}

// 2. 给Buff类标记特性
[BuffEffect(BuffEffect.RebuildCard)]
public class RebuildBuffEffect : BuffEffectBase { }

[BuffEffect(BuffEffect.ControlDice)]
public class ControlDiceBuffEffect : BuffEffectBase { }
csharp 复制代码
static BuffEffectFactory()
{
    // 优化1:只扫描指定程序集(避免遍历所有程序集,减少扫描量)
    var targetAssembly = Assembly.GetExecutingAssembly(); // 假设Buff类都在当前程序集
    var buffTypes = targetAssembly.GetTypes()
        .Where(type => typeof(BuffEffectBase).IsAssignableFrom(type) 
                     && !type.IsAbstract 
                     && type.GetCustomAttribute<BuffEffectAttribute>() != null);

    foreach (var type in buffTypes)
    {
        var attribute = type.GetCustomAttribute<BuffEffectAttribute>();
        if (attribute == null) continue;

        // 优化2:缓存构造函数委托,替代Activator.CreateInstance
        var constructor = type.GetConstructor(Type.EmptyTypes); // 无参构造
        if (constructor == null)
        {
            throw new InvalidOperationException($"{type.Name} 必须提供无参构造函数");
        }
        // 将构造函数转为委托,调用时无反射损耗
        var creator = (Func<BuffEffectBase>)Delegate.CreateDelegate(
            typeof(Func<BuffEffectBase>), constructor);
        
        _effectCreators.Add(attribute.EffectType, creator);
    }
}
相关推荐
LONGZETECH1 小时前
新能源汽车充电设备装配与调试仿真教学软件 技术架构与核心实现解析
大数据·算法·3d·unity·架构·汽车
丁小未1 小时前
Unity 几种常见合批手段的要求
游戏·unity·合批·srpbatcher·动态合批·静态合批
旧物有情21 小时前
游戏开发常用架构 #MVP,MVC
游戏·unity·架构·mvc
勇踏前人未索之境1 天前
Unity打包运行于鸿蒙手机
unity·智能手机·harmonyos
玖玥拾1 天前
Unity 3D 笔记(十一)UI 框架进阶:栈弹窗交互、BasePanel 基类虚方法、DoTween 界面动画
笔记·3d·unity
郝学胜-神的一滴1 天前
中级OpenGL教程 023:Assimp模型加载全解——从源码到架构的骈文探秘
c++·unity·游戏引擎·cmake·unreal engine·opengl
xcLeigh2 天前
Unity基础:GameObject与Component——Unity核心架构思想彻底理解
unity·教程·component·gameobject
郝学胜-神的一滴2 天前
中级OpenGL教程 022:探秘三维世界的血脉传承——物体父子关系与矩阵递归奥义
c++·线性代数·unity·矩阵·游戏引擎·unreal engine·opengl
weixin_424294673 天前
Unity的测试Edit Mode和Play Mode,有什么区别?
unity