单例模式

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);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值