• 单例模式


    1.创建一个Student类

    public  class Student {   //学生的实体类
        
        private  String  name; //姓名
        private  int  age;    //年龄
        
        public Student() {
            super();
        }
        public Student(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }        
    }

    2.创建测试类

    public class SingletonTest {
    
        public static void main(String[] args) {
            //创建第一个学生对象
            Student stu1=new Student("学生1", 21);
            //创建第二个学生对象
            Student stu2=new Student("学生2", 22);
            
            //输出对应学生的信息
            System.out.println("学生1的姓名:"+stu1.getName());
            System.out.println("学生2的姓名:"+stu2.getName());
            
            System.out.println("***************************");
        }
    }

    3.运行查看结果

     /*
              * 在项目中  只想让Student这个类 实例化一次!也就是整个程序中只有一个对象!
              * 操作的所有Student对象 都是一个!
              * 
              * 单例模式: 保证整个系统中一个类 只有一个实例!而且这个实例只能是自身创建!
              * 外部不能直接创建对象! 不能new !  只能通过类中的固定方法获取!
              * 
              * 01.懒汉式
              * 02.饿汉式
              * 03.双重锁
              * 
              */

    4.创建SingletonStudent实体类

    public  class SingletonStudent {   //懒汉式
        
        //01.创建了一个类型是本类对象的静态属性
        private  static SingletonStudent student=null;
        
        private  String  name;
        private  int  age;
        
        //02.构造私有化
        private SingletonStudent() {
            
        }
    
        //03.提供外部访问的接口   返回当前类的对象
        public static synchronized SingletonStudent getInstance(){
            if (student==null) {
                student=new SingletonStudent();
            }
            return student;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    5.在测试类中增加代码

    //通过SingletonStudent类中提供的方法来构造实例
            SingletonStudent student1=SingletonStudent.getInstance();
            //给第一个学生的年龄和姓名赋值
            student1.setAge(50);
            student1.setName("小黑");  
            
            SingletonStudent student2=SingletonStudent.getInstance();
            //给第二个学生的年龄赋值
            student2.setAge(55);
            
            
            System.out.println(student1.getAge());
            System.out.println(student2.getName());
            System.out.println(student1==student2);  //true

    6.运行查看结果后发现 student1和student2其实就是一个对象!实现了单例

     7.饿汉式实现单例  创建实体类SingletonStudent2

    public  class SingletonStudent2 {   //饿汉式
        
        //01.创建一个自己的实例  仅供自身访问
        private  static SingletonStudent2 student=new SingletonStudent2();
        
        private  String  name;
        private  int  age;
        
        //02.构造私有化
        private SingletonStudent2() {
            
        }
    
        //03.提供外部访问的接口   返回当前类的对象
        public static synchronized SingletonStudent2 getInstance(){
            return student;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    8.在测试类中增加测试代码 运行查看效果  还是单例

    @Test// 02.饿汉式
        public  void   test02(){
            //通过SingletonStudent2类中提供的方法来构造实例
            SingletonStudent2 student1=SingletonStudent2.getInstance();
            //给第一个学生的年龄和姓名赋值
            student1.setAge(50);
            student1.setName("小黑");  
            
            SingletonStudent2 student2=SingletonStudent2.getInstance();
            //给第二个学生的年龄赋值
            student2.setAge(55);
            
            System.out.println(student1.getAge());
            System.out.println(student2.getName());
            System.out.println(student1==student2); //true
        }

    9.双重锁的机制实现单例 创建实体类

    public  class SingletonStudent3 {   //双重锁
        
        //01.创建了一个类型是本类对象的静态属性
        private  static SingletonStudent3 student=null;
        
        private  String  name;
        private  int  age;
        
        //02.构造私有化
        private SingletonStudent3() {
            
        }
    
        //03.提供外部访问的接口   返回当前类的对象
        public static synchronized SingletonStudent3 getInstance(){
            if (student==null) {
                synchronized (SingletonStudent3.class) {  //再次加锁
                    if (student==null) {
                        student=new SingletonStudent3();
                    }
                }
            }
            return student;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    10.在测试类中增加测试代码 运行查看效果  还是单例

    @Test//03.双重锁
        public  void   test03(){
            //通过SingletonStudent2类中提供的方法来构造实例
            SingletonStudent3 student1=SingletonStudent3.getInstance();
            //给第一个学生的年龄和姓名赋值
            student1.setAge(50);
            student1.setName("小黑");  
            
            SingletonStudent3 student2=SingletonStudent3.getInstance();
            //给第二个学生的年龄赋值
            student2.setAge(55);
            
            System.out.println(student1.getAge());
            System.out.println(student2.getName());
            System.out.println(student1==student2); //true
        }
  • 相关阅读:
    ik_max_word ik_smart
    使用elasticsearch遇到的一些问题以及解决方法(不断更新)
    Install elasticsearch-head: – for Elasticsearch 5.x
    Spring实战5-基于Spring构建Web应用
    如何使用 Android Studio 的 git hub 功能
    windows中使用Git工具连接GitHub(配置篇)
    Git链接到自己的Github(2)进阶使用
    Git链接到自己的Github(1)简单的开始
    Android 自定义控件玩转字体变色 打造炫酷ViewPager指示器
    Android Studio 中快速提取方法
  • 原文地址:https://www.cnblogs.com/xtdxs/p/7093926.html
Copyright © 2020-2023  润新知