java单例设计模式和多例设计模式

单例设计模型

教学视频链接:https://edu.aliyun.com/course/1011

1,private不可以在类外部访问,但可以在内部访问

2,此时Singleton类内部的instance属于一个普通属性,而普通属性是在有实例化对象产生之后才会被调用的,那么这个时候外部无法产生实例化对象,所以这个属性就不能访问到了,那么就必须考虑如何在没有实力化对象的时候可以获取此属性,那么只有static属性可以访问。

 Singleton.java

1 class Singleton {
2     static Singleton instance=new Singleton();
3     private Singleton(){}
4     public void print(){
5         System.out.println("这是一个单例");
6     }
7 }

Main.java

1 public class Main {
2     public static void main(String[] args) {
3         Singleton instance=null;
4         instance=Singleton.instance;
5         instance.print();
6         System.out.println("Hello World!");
7     }
8 }

3,类中的属性应该封装后使用,所以理论上此时的instance需要封装起来,那么就需要一个static方法获取

 1 class Singleton {
 2     static Singleton instance=new Singleton();
 3     private Singleton(){}
 4     public static  Singleton getInstance(){
 5         return instance;
 6     }
 7     public void print(){
 8         System.out.println("这是一个单例");
 9     }
10 }
1 public class Main {
2 
3     public static void main(String[] args) {
4         Singleton instance=null;
5         instance=Singleton.getInstance();
6         instance.print();
7         System.out.println("Hello World!");
8     }
9 }

4,整个代码强调的是只有一个实例化对象,这个时候虽然提供有static的实例化对象,但是这个对象依然可以被重新实例化,所以需要保证此时Singleton类内部的instance无法再次实例化定义,那么应该使用final定义。

 1 class Singleton {
 2     private static final Singleton INSTANCE=new Singleton();
 3     private Singleton(){}
 4     public static  Singleton getInstance(){
 5         return INSTANCE;
 6     }
 7     public void print(){
 8         System.out.println("这是一个单例");
 9     }
10 }
1 public class Main {
2 
3     public static void main(String[] args) {
4         Singleton instance=null;
5         instance=Singleton.getInstance();
6         instance.print();
7         System.out.println("Hello World!");
8     }
9 }

在很多情况下有些类是不需要重复产生对象的,例如:如果一个程序启动,那么现在肯定需要有一个类负责保存有一些程序加载的数据信息。

对于单例设计模型也分为两种:懒汉式和饿汉式。在之前所定义的都属于饿汉式。在系统加载类的时候就会自动提供Singleton类的实例化对象,而还有一种懒汉式,在第一次使用的时候进行实例化对象处理。

范例:将我们的单例修改成懒汉式

 1 class Singleton {
 2     private static Singleton instance;
 3     private Singleton(){}
 4     public static  Singleton getInstance(){
 5         if (instance==null){
 6             instance=new Singleton();
 7         }
 8         return instance;
 9     }
10     public void print(){
11         System.out.println("这是一个单例");
12     }
13 }

5,面试题:请编写一个Singleton程序,并说明其主要特点?

l 代码如上,可以把懒汉式(需要考虑到线程同步问题)和饿汉式都写上;

特点:构造方法私有化,类内部提高getInstance方法获取实例化对象,不管外部如何操作都只有一个实例化对象提供;

 

多例设计模式

与单例设计相对应的还有一个称为多例设计,单例设计指的是只保留有一个实例化对象,例如:如果现在要定义一个描述性别的类,那么这个对象只有两个:男、女。或者描述颜色基色的类,可以使用:R G B 三原色。这种情况下就可以利用多例设计来解决:

范例:多例设计模式

 1 class Color {
 2     private static final Color RED = new Color("红色");
 3     private static final Color GREEN = new Color("绿色");
 4     private static final Color BLUE = new Color("蓝色");
 5     private String title;
 6     private Color(String title){//构造方法私有化
 7         this.title=title;
 8     }
 9     public static Color getInstance(String color){
10         switch (color){
11             case "red":return RED;
12             case "green":return GREEN;
13             case "blue":return BLUE;
14             default:return null;
15         }
16     }
17     public String toString(){
18         return this.title;
19     }
20 }
public class Main {
    public static void main(String[] args) {
        Color color=Color.getInstance("green");
        System.out.println(color);
        System.out.println("Hello World!");
    }
}

猜你喜欢

转载自www.cnblogs.com/Mufasa/p/11076745.html