zoukankan      html  css  js  c++  java
  • 单利模式

    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 getInsance(){
    
           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
        }
     
    

      

  • 相关阅读:
    【docker】win10安装docker教程
    【大数据】hive 删除临时文件 .hive-staging_hive
    【PostgreSql】生成数据字典
    【python3】基于scrapyd + scrapydweb 的可视化部署
    【python3】将视频转换为代码视频
    博客转移,永久退出博客园
    对dataframe中某一列进行计数
    解决mac上matplotlib中文无法显示问题
    在Jupyter notebook中使用特定虚拟环境中的python的kernel
    ubuntu18.04里更新系统源和pip源
  • 原文地址:https://www.cnblogs.com/HHR-SUN/p/7015683.html
Copyright © 2011-2022 走看看