码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式

时间:2017-10-04 19:35:14      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:this   post   使用   archive   person   饿汉式   ati   执行   single   

Java:单例模式的七种写法

饿汉式:

package com.witwicky.domain;

/**
 * 饿汉式
 */
public class Person2 {

    public static final Person2 person = new Person2();

    private Person2() {
    }

    public static Person2 getInstance() {
        return person;
    }

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

 

懒汉式:

1.

package com.witwicky.domain;

/**
 * 懒汉式1
 */
public class Person3 {

    public static Person3 person = null;

    private Person3() {
    }

    public static Person3 getInstance() {
        if (null == person) {
            person = new Person3();
            return person;
        }

        return person;
    }

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

 

2.

package com.witwicky.domain;

/**
 * 懒汉式2
 */
public class Person4 {

    public static Person4 person = null;

    private Person4() {
    }

    /**
     * 使用同步锁,锁定当前线程所执行的方法
     * @return
     */
    public static synchronized Person4 getInstance() {
        if (null == person) {
            person = new Person4();
            return person;
        }

        return person;
    }

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

 

3.

package com.witwicky.domain;

/**
 *  懒汉式3
 */
public class Person5 {

    public static Person5 person = null;

    private Person5() {
    }

    /**
     * 使用同步锁,锁定当前线程所执行的方法中的具体代码
     * @return
     */
    public static Person5 getInstance() {
        if (null == person) {
            synchronized (Person5.class) {
                person = new Person5();
            };

            return person;
        }

        return person;
    }

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

 

双重检测式:

package com.witwicky.domain;

/**
 *  双重检测
 */
public class Person6 {

    public static Person6 person = null;

    private Person6() {
    }

    /**
     * 使用同步锁,锁定当前线程所执行的方法中的具体代码
     * @return
     */
    public static Person6 getInstance() {
        if (null == person) {
            synchronized (Person6.class) {
                if(null == person) {
                    person = new Person6();
                }
            };

            return person;
        }

        return person;
    }

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ‘}‘;
    }
}

 

===

 

测试Main方法.java:

package com.witwicky.test;

import com.witwicky.domain.*;

public class TestSingleton {
    public static void main(String[] args) {
//        Person p1 = new Person();
//        p1.setName("bob");
//        p1.setAge(23);
//
//        Person p2 = new Person();
//        p2.setName("jack");
//        p2.setAge(32);

//        Person2 p1 = Person2.getInstance();
//        p1.setName("bob");
//        p1.setAge(23);
//
//        Person2 p2 = Person2.getInstance();
//        p2.setName("jack");
//        p2.setAge(32);

//        Person3 p1 = Person3.getInstance();
//        p1.setName("bob");
//        p1.setAge(23);
//
//        Person3 p2 = Person3.getInstance();
//        p2.setName("jack");
//        p2.setAge(32);

//        Person4 p1 = Person4.getInstance();
//        p1.setName("bob");
//        p1.setAge(23);
//
//        Person4 p2 = Person4.getInstance();
//        p2.setName("jack");
//        p2.setAge(32);

//        Person5 p1 = Person5.getInstance();
//        p1.setName("bob");
//        p1.setAge(23);
//
//        Person5 p2 = Person5.getInstance();
//        p2.setName("jack");
//        p2.setAge(32);

        Person6 p1 = Person6.getInstance();
        p1.setName("bob");
        p1.setAge(23);

        Person6 p2 = Person6.getInstance();
        p2.setName("jack");
        p2.setAge(32);

        System.out.println(p1);
        System.out.println(p2);
    }
}

 

单例模式

标签:this   post   使用   archive   person   饿汉式   ati   执行   single   

原文地址:http://www.cnblogs.com/hfultrastrong/p/7627012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!