构造方法私有化(单例与多例设计模式)

/**
 * @program: pro1
 * @description 如果想控制一个类实例化对象产生个数,那么首先就要把锁定类的构造方法‘
 * 只需要一个对象,那么在类的内部用static定义一个公共对象并且每次通过static方法返回唯一的对象。
 * 这样不管外部有多少次调用,都只会产生唯一的对象,这就属于单例设计模式。
 * @author: ydcoding
 * @create: 2019-07-28 09:53
 **/

class  Singleton{
    private static  Singleton instance=new Singleton();
    private  Singleton(){
//构造方法私有化
    }
    public  void print(){
        System.out.println("hello world");
    }

    public static  Singleton getInstance(){
       
        return  instance;
    }
}

public class SingletonDemo {
    public static void main(String[] args) {
        Singleton s1=Singleton.getInstance();
        System.out.println(s1);
        Singleton s2=Singleton.getInstance();
        System.out.println(s2);
    }
}

Singleton@4554617c
Singleton@4554617c

直接返回instance,会得到相同的对象,但是如果是下面的情况呢,在getInstance方法里重新new一个对象,就会得到不同的对象了....

 

/**
 * @program: pro1
 * @description 如果想控制一个类实例化对象产生个数,那么首先就要把锁定类的构造方法‘
 * 只需要一个对象,那么在类的内部用static定义一个公共对象并且每次通过static方法返回唯一的对象。
 * 这样不管外部有多少次调用,都只会产生唯一的对象,这就属于单例设计模式。
 * @author: ydcoding
 * @create: 2019-07-28 09:53
 **/

class  Singleton{
    private static  Singleton instance=new Singleton();
    private  Singleton(){
//构造方法私有化
    }
    public  void print(){
        System.out.println("hello world");
    }

    public static  Singleton getInstance(){
        instance=new Singleton();//重新new了一个对象
        return  instance;
    }
}

public class SingletonDemo {
    public static void main(String[] args) {
        Singleton s1=Singleton.getInstance();
        System.out.println(s1);
        Singleton s2=Singleton.getInstance();
        System.out.println(s2);
//        Singleton@4554617c
//        Singleton@74a14482
    }
}

所以以上的设计发方法还存在不足

/**
 * @program: pro1
 * @description 如果想控制一个类实例化对象产生个数,那么首先就要把锁定类的构造方法‘
 * 只需要一个对象,那么在类的内部用static定义一个公共对象并且每次通过static方法返回唯一的对象。
 * 这样不管外部有多少次调用,都只会产生唯一的对象,这就属于单例设计模式。
 * @author: ydcoding
 * @create: 2019-07-28 09:53
 **/

//class  Singleton{
//    private static  Singleton instance=new Singleton();
//    private  Singleton(){
构造方法私有化
//    }
//    public  void print(){
//        System.out.println("hello world");
//    }
//
//    public static  Singleton getInstance(){
//        instance=new Singleton();//重新new了一个对象
//        return  instance;
//    }
//}

class  Singleton{
    private  static  final Singleton INSTANCE=new Singleton();
    private  Singleton(){
//构造方法私有化
    }
    public  void print(){
        System.out.println("hello world");
    }

    public static  Singleton getInstance(){
//        INSTANCE=new Singleton(); 定义了final就不能new了
        return  INSTANCE;
    }
}
public class SingletonDemo {
    public static void main(String[] args) {
        Singleton s1=Singleton.getInstance();
        System.out.println(s1);
        Singleton s2=Singleton.getInstance();
        System.out.println(s2);
//        Singleton@4554617c
//        Singleton@4554617c
    }
}

上述单例模式是属于饿汉式,在定义时就已经实例化INSTANCE,不关心是否是使用。而懒汉式是在使用时如果没有才实例化对象

/**
 * @program: pro1
 * @description 如果想控制一个类实例化对象产生个数,那么首先就要把锁定类的构造方法‘
 * 只需要一个对象,那么在类的内部用static定义一个公共对象并且每次通过static方法返回唯一的对象。
 * 这样不管外部有多少次调用,都只会产生唯一的对象,这就属于单例设计模式。
 * @author: ydcoding
 * @create: 2019-07-28 09:53
 **/
//饿汉式单例模式
class  Singleton2{
    private  static   Singleton2 instance=null;
    private  Singleton2(){
//构造方法私有化
    }
    public  void print(){
        System.out.println("hello world");
    }

    public static  Singleton2 getInstance(){
        if(instance==null){
            instance=new Singleton2();
        }
        return  instance;
    }
}
public class SingletonDemo {
    public static void main(String[] args) {
        Singleton2 s1=Singleton2.getInstance();
        System.out.println(s1);
        Singleton2 s2=Singleton2.getInstance();
        System.out.println(s2);
//        Singleton@4554617c
//        Singleton@4554617c
    }
}
/**
 * @program: pro1
 * @description
 * @author: ydcoding
 * @create: 2019-07-28 10:46
 **/
class Sex {
    private String title;
    private static final Sex MALE = new Sex("男");
    private static final Sex FEMALE = new Sex("女");
    private Sex(String title) { 				// 构造私有化了
        this.title = title;
    }
    public String toString() {
        return this.title;
    }
    public static Sex getInstance(String ch) {
        switch (ch) {						// 利用字符串判断
            case "man":
                return MALE;
            case "woman":
                return FEMALE;
            default:
                return null;
        }
    }
}
public class Dldemo {
    public static void main(String[] args) {
        Sex sex=Sex.getInstance("man");
        System.out.println(sex);
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值