你好,游客 登录 注册 搜索
背景:
阅读新闻

Java单例模式实现

[日期:2017-10-12] 来源:Linux社区  作者:abelxu [字体: ]

只能生成一个实例的类是实现了Singleton(单例)模式的类。以下为C#实现单例模式的方式

方式一只使用于单线程环境
// 把构造函数设为私有函数以禁止他人创建实例
// 定义一个静态的实例,在需要的时候创建该实例
// 在Singleton的静态属性Instance中,只有在instance为null的时候才创建一个实例以避免重复创建
// 把构造函数定义为私有函数
public final class Singleton1 {
    private Singleton1() {
    }
 
    private static Singleton1 _instance = null;
 
    public static Singleton1 getInstance() {
        if (_instance == null)
            _instance = new Singleton1();
        return _instance;
    }
 
    public static void main(String[] args) {
        Singleton1 s1 = Singleton1.getInstance();
        Singleton1 s2 = Singleton1.getInstance();
 
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }
}

方式二 加同步锁
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public final class Singleton2 {
    private Singleton2(){}
     
    static Lock lock = new ReentrantLock();
     
    private static Singleton2 _instance = null;
     
    public static Singleton2 getInstance(){
        lock.lock();
        try {
            if(_instance == null)
                _instance = new Singleton2();
        } finally {
            lock.unlock();
        }
        return _instance;
    }
 
    public static void main(String[] args) {
        Singleton2 s1 = Singleton2.getInstance();
        Singleton2 s2 = Singleton2.getInstance();
 
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }
}

可行的解法 加同步锁前后两次判断实例是否已存在
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public final class Singleton3 {
    private Singleton3(){}
     
    static Lock lock = new ReentrantLock();
     
    private static Singleton3 _instance = null;
     
    public static Singleton3 getInstance(){
        if(_instance == null){
            lock.lock();
            try{
                if(_instance == null)
                    _instance = new Singleton3();
            }finally {
                lock.unlock();
            }   
        }
        return _instance;
    }
 
    public static void main(String[] args) {
        Singleton3 s1 = Singleton3.getInstance();
        Singleton3 s2 = Singleton3.getInstance();
 
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }
}

推荐的解法一利用静态构造函数

public final class Singleton4 {
    private Singleton4(){}
     
    private static Singleton4 _instance = new Singleton4();
     
    public static Singleton4 getInstance()
    {
        return _instance;
    }
 
    public static void main(String[] args) {
        Singleton4 s1 = Singleton4.getInstance();
        Singleton4 s2 = Singleton4.getInstance();
 
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }
 
}

推荐的解法二 实现按需创建实例

public class Singleton5 {
    private Singleton5(){}
     
    public static Singleton5 getInstance(){
        return Nested.instance;
    }
     
    private static class Nested{
        private Nested(){}
        public static final Singleton5 instance = new Singleton5();
    }
 
    public static void main(String[] args) {
        Singleton5 s1 = Singleton5.getInstance();
        Singleton5 s2 = Singleton5.getInstance();
 
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
    }
 
}

扩展

    定义一个表示总统的类型President可以从该类型继承出FrenchPresident和AmericanPresident等类型。这些派生类型都只能产生一个实例       
public class President {
    private String _name;
     
    public President(){}
     
    public String getName(){
        return _name;
    }
     
    public void setName(String name){
        _name = name;
    }
     
    public static void main(String[] args) {
    }
 
}
 
public class FrenchPresident extends President{
    private FrenchPresident(){}
     
    public static FrenchPresident getInstance()
    {
        return Nested.instance;
    }
     
    private static class Nested{
        private Nested(){}
         
        public static final FrenchPresident instance = new FrenchPresident();
    }
 
    public static void main(String[] args) {
        FrenchPresident s1 = FrenchPresident.getInstance();
        FrenchPresident s2 = FrenchPresident.getInstance();
 
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
 
    }
 
}
 
 
public class AmericanPresident {
    private AmericanPresident() {
    }
 
    public static AmericanPresident getInstance() {
        return Nested.instance;
    }
 
    private static class Nested {
        private Nested() {
        }
 
        public static final AmericanPresident instance = new AmericanPresident();
    }
 
    public static void main(String[] args) {
        AmericanPresident s1 = AmericanPresident.getInstance();
        AmericanPresident s2 = AmericanPresident.getInstance();
 
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
 
    }
 
}

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-10/147514.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款