Unity 游戏框架搭建 (十三) 无需继承的单例的模板

来源:互联网 发布:阿里云服务器安全配置 编辑:程序博客网 时间:2024/06/05 04:40
之前的文章中介绍的 游戏框架搭建(二) 单例的模板 http://www.manew.com/thread-89635-1-1.html
和Unity游戏框架搭建(三) MonoBehaviour单例的模板http://www.manew.com/thread-89636-1-1.html有一些问题。
存在的问题:
  • 只要继承了单例的模板就无法再继承其他的类。
  虽然单例继承其他类是比较脏的设计,但是难免会遇到不得不继承的时候。没有最好的设计,只有最合适的设计。
解决方案:
  • 首先实现单例的类从使用方式上应该不变,还是

[C#] 纯文本查看 复制代码
?
 
XXX.Instance.ABCFunc()
之前的单利的模板代码如下所示:

[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
usingSystem; 
usingSystem.Collections.Generic; 
usingSystem.Text; 
usingSystem.Reflection;
 
/// <summary>
/// 1.泛型
/// 2.反射
/// 3.抽象类
/// 4.命名空间
/// </summary>
namespaceQFramework { 
    publicabstract class QSingleton<T> where T : QSingleton<T>
    {
        protectedstatic T mInstance = null;
 
        protectedQSingleton()
        {
        }
 
        publicstatic T Instance
        {
 
            get{
                if(mInstance == null) {
                    // 先获取所有非public的构造方法
                    ConstructorInfo[] ctors = typeof(T).GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic);
                    // 从ctors中获取无参的构造方法
                    ConstructorInfo ctor = Array.Find (ctors, c => c.GetParameters ().Length == 0);
                    if(ctor == null)
                        thrownew Exception ("Non-public ctor() not found!");
                    // 调用构造方法
                    mInstance = ctor.Invoke (null)asT;
                }
 
                returnmInstance;
            }
        }
 
        publicvoid Dispose()
        {
            mInstance = null;
        }
    }
}

按照以前的方式,如果想实现一个单例的代码应该是这样的:

[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
usingQFramework; 
// 1.需要继承QSingleton。
// 2.需要实现非public的构造方法。
publicclass XXXManager : QSingleton<XXXManager> { 
    privateXXXManager() {
        // to do ...
    }
}
 
 
publicstatic void main(string[] args) 
{
    XXXManager.Instance().xxxyyyzzz();
}

如果我想XXXManager继承一个BaseManager代码就变成这样了

[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
usingQFramework; 
// 1.需要继承QSingleton。
// 2.需要实现非public的构造方法。
publicclass XXXManager : BaseManager { 
    privateXXXManager() {
        // to do ...
    }
}
 
 
publicstatic void main(string[] args) 
{
    XXXManager.Instance().xxxyyyzzz();
}

这样这个类就不是单例了,怎么办?
答案是通过C#的属性。

[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
usingQFramework; 
// 1.需要继承QSingleton。
// 2.需要实现非public的构造方法。
publicclass XXXManager : BaseManager { 
    privateXXXManager() {
        // to do ...
    }
    publicstatic XXXManager Instance {
        get{
            returnQSingletonComponent<XXXManager>.Instance;
        }
    }
}
 
 
publicstatic void main(string[] args) 
{
    XXXManager.Instance().xxxyyyzzz();
}

好了,又看到陌生的东西了,QSingletonComponent是什么?
和之前的单例的模板很相似,贴上代码自己品吧...

[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
usingSystem; 
usingSystem.Collections.Generic; 
usingSystem.Text; 
usingSystem.Reflection;
 
/// <summary>
///    组合方式实现单例子
/// </summary>
namespaceQFramework {
 
    /// <summary>
    /// class是引用类型
    /// </summary>
    publicclass QSingletonComponent<T> where T : class
    {
        protectedstatic T mInstance = null;
 
        publicstatic T Instance
        {
 
            get{
                if(mInstance == null) {
                    // 先获取所有非public的构造方法
                    ConstructorInfo[] ctors = typeof(T).GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic);
                    // 从ctors中获取无参的构造方法
                    ConstructorInfo ctor = Array.Find (ctors, c => c.GetParameters ().Length == 0);
                    if(ctor == null)
                        thrownew Exception ("Non-public ctor() not found!");
                    // 调用构造方法
                    mInstance = ctor.Invoke (null)asT;
                }
 
                returnmInstance;
            }
        }
 
        publicstatic void Dispose()
        {
            mInstance = null;
        }
    }
}

这样无法继承的问题就解决啦。
缺点是:相比于QSingleton,QSingletonComponent在使用时候多了一次函数调用,不过做中小型项目应该可以应付了。
介绍完毕,睡觉了。。。

附:我的框架地址:https://github.com/liangxiegame/QFramework
转载请注明地址:凉鞋的笔记 liangxiegame.com
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑打开是乱码怎么办 液相色谱柱柱压柱压高怎么办 手机销卡存的钱怎么办 41周宫颈未成熟怎么办 运费险拒绝理赔怎么办 淘宝运费险拒赔怎么办 订单险的运费怎么办 拼多多要换货怎么办 大件退货运费险怎么办 冲浪助手退订了怎么办 小蓝车押金退不了怎么办 小蓝车押金没退怎么办 有订单险退货怎么办 订单险交不了怎么办 小孩手机瘾大不写作业怎么办 运费险不到账怎么办 淘金币要过期怎么办 手机一直扣费怎么办 手机卡自动扣费怎么办 淘宝退货运费险不赔怎么办 类目不能开直通车怎么办 淘宝虚拟恶意退款怎么办 淘宝虚拟单退款怎么办 虚拟物品被骗了怎么办 网络选修课挂了怎么办 美团商家退出怎么办 卖家版运费险太贵了怎么办 美瞳没有客源怎么办 购物车满了怎么办 手机程序无响应怎么办 三星手机无响应怎么办 游戏无响应了怎么办 手机百度无响应怎么办 新手机响应慢怎么办 vivo手机无响应怎么办 vivo软件无响应怎么办 退货商家不处理怎么办 淘宝页面变小了怎么办 淘宝卖家让微信交易被骗怎么办 苹果下载特别慢怎么办 淘宝没有支付宝怎么办