D语言中国主页  D语言编辑器SciTE4D   DWin库 D语言官方网站
D语言编译器1.x最新版 OpenSource   Tango   webnews  Wiki

查看完整版本: D语言设计模式Singleton

yidabu 2007-4-27 19:50

D语言设计模式Singleton

D语言设计模式Singleton知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070427点击下面网址查看原文:http://www.d-programming-language-china.org        by:        ideage        from:        http://ideage.javaeye.com/blog/57951        关键字:        设计模式 D Singleton        引言 语言的进步,可以简化设计模式的实现.        Singleton模式        类型:创建型        意义:保证一个类仅有一个实例,并提供一个访问它的全局访问点。        1.D的实现        一个类的实现        代码        [Copy to clipboard] [ - ]CODE:                                class Singleton                {                public:                    static Singleton opCall()                    {                        if(_instance is null) _instance = new Singleton;                        return _instance;                    }                protected void init(){}                private:                    this() {this.init();}                    static Singleton _instance;                }                实现后每次都要复制粘贴,很累,用模版类,更方便:        代码        [Copy to clipboard] [ - ]CODE:                                class Singleton(T)                {                    public static T opCall()                    {                    if(_instance is null) _instance = new T;                    return _instance;                    }                                    protected void init(){}                                    private:                    this() { this.init(); }                                    static T _instance;                }                2.使用例子        [Copy to clipboard] [ - ]CODE:                                class Option:Singleton!(Option)                {                    char[] foo(){ return "Hello!";}                }                                int main()                {                    writefln(Option().foo);                    return 0;                }                永久链接        http://ideage.javaeye.com/blog/57951( 本文出处: http://www.d-programming-language-china.org )        oldrev        2007-03-07 11:37        我写了一个不使用继承的:( 本文出处: http://www.d-programming-language-china.org )        [Copy to clipboard] [ - ]CODE:                                final class SingletonHolder(T)                {                    private static T m_instance;                                    private static this()                    {                        static if(is(T == class))                            m_instance = new T();                    }                                    public static T instance()                    {                        assert(m_instance);                        return m_instance;                    }                                    public static T opCall()                    {                        return instance;                    }                                    public static T opCast()                    {                        return instance;                    }                                    private ~this()                    {                    }                }                                unittest                {                    class Foo                    {                        public int x;                                    }                                    alias SingletonHolder!(Foo) S;                    S().x = 2;                    (cast(Foo)S).x = 2;                }                代码        [Copy to clipboard] [ - ]CODE:                                alias SingletonHolder!(Foo) S;                S().x = 2;                D语言里面可以省去0参数方法的括号,S().x改成S.x能不能编译?我现在出差不方便测试。。。        qiezi        D语言里面可以省去0参数方法的括号,S().x改成S.x能不能编译?我现在出差不方便测试。。。        测试结果:编译错误( 本文出处: http://www.d-programming-language-china.org )        楼上说的那个特性好像是0.177新添加的两个struct特性        1. struct S; S s(x) 将被编译器实现为 S s = S(x)        2. S s = x; 将实现为 S s = S(x)        也就是 struct 的 static opCall 用起来像 ctor        oldrev        2007-03-07 17:16        SingletonHolder 类避免了继承的麻烦,而且可以支持struct和内置类型        oldrev        2007-03-07 17:18        终极修正版:        [Copy to clipboard] [ - ]CODE:                                final class SingletonHolder(T)                {                    private static T m_instance;                                    private static this()                    {                        static if(is(T == class))                            m_instance = new T();                    }                                    public static T instance()                    {                        static if(is(T == class))                            assert(!(m_instance is null));                        return m_instance;                    }                                    public static T opCall()                    {                        return instance;                    }                                    public static T opCast()                    {                        return instance;                    }                }                引用        SingletonHolder 类避免了继承的麻烦,而且可以支持struct和内置类型        1.用法简洁,不用修改现存的类.        2.优先使用组合,尽量避免继承.        3.支持多数类型!!!( 本文出处: http://www.d-programming-language-china.org )        oldrev        2007-03-07 18:48        引用        3.支持多数类型!!!        经过实践证明这只是理论上的,D没有值类型的透明引用类型,这使得SingletonHolder类只能作右值。        如果要支持内置类型(比如int)的话需要修改 instance() 让其返回指针才能修改int的值,而且Singleton本来就是面向对象的概念,支持内置类型也没太大的必要,那么干脆就只支持 class 、struct、union 好了。( lastupdate:20070427 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:yidabu提倡在交流中学习,在分享中提高收集感兴趣的知识,写下心得,通过网络与别人一起分享理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台
页: [1]
查看完整版本: D语言设计模式Singleton