JAVA单例设计模式饿汉式

单例设计模式

  • 设计模式是在大量的实践中总结和理论化之后优选的代码结构、编程风格、以及解决问题的思考方式
  • 所谓类的单例设计模式,采用一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法
  • 比如在做开发中,有一个类是很重要且非常耗用资源的,但是我们只需要一个它
  • 单例模式有两种方法
    • 1.饿汉式
    • 2.懒汉式

饿汉式

  • 为了方便理解 创建一个类
class Student{
        private String name;//name属性
        public Student(String name){//构造器
            this.name=name;
        }
        public String getName() {//name的get方法
            return name;
        }
        public void setName(String name) {//name的set方法
            this.name = name;
        }
 }
  • 当我们在main方法中创建一个Student对象 可以创造多个 比如
public class test {
   public static void main(String[] args) {
    Student student1=new Student("小明");
    Student student2=new Student("小红");
   } 
}
  • 他们两个都有各自的内存空间 很显然不是我们需要的 因为我们的单例设计模式只需要一个Student类对象 那我们应该怎么办?
class Student{
        private String name;
        private Student(String name){//把public 换成private 私有的构造器
            this.name=name;
        }
        public String getName() {//name的get方法
            return name;
        }
        public void setName(String name) {//name的set方法
            this.name = name;
        }
 }
  • 我们把类的构造器换成私有的 此时main方法中的
Student student1=new Student("小明");
Student student2=new Student("小红");

都会报错 因为构造器被private 修饰后 是私有的 只有Student类的里面他才能调用

  • 接下来 我们在Student类的内部创建Student类`
class Student{
        private String name;
        private static Student student=new Student("小红");//在类的内部直接创建
        private Student(String name){//把public 换成private 私有的构造器
            this.name=name;
        }
        public String getName() {//name的get方法
            return name;
        }
        public void setName(String name) {//name的set方法
            this.name = name;
        }
 }
  • 我们已经有了我们要的对象且仅有一个
  • 但是我们现在无法把他取出来使用 只能在Student里面使用 加上static这样在类的加载中之会执行一次
  • 我们只需要加上一个公开的static方法 用来返回我们刚刚在Student里面创建的对象
class Student{
        private String name;
        private static Student student=new Student("小红");//在类的内部直接创建
        private Student(String name){//把public 换成private 私有的构造器
            this.name=name;
        }
        public static Student getStudent(){//加上一个private修饰的静态方法来调取Student类内部对象
            rentun student;
        }
        public String getName() {//name的get方法
            return name;
        }
        public void setName(String name) {//name的set方法
            this.name = name;
 }
  • 我们在主类中创建两个对象
public class test {
   public static void main(String[] args) {
      Student student =Student.getStudent();//使用刚才写的getStudent()方法创建了student和student1
      Student student1 =Student.getStudent();
      System.out.println(student);//我们输出一下他们 看看他们的内存地址引用的是不是同一个
      System.out.println(student1);
      System.out.println(student.getName());//先输出student的name值
      student.setName("小明");//再把student的name值设成小明
      System.out.println(student.getName());//输出student和student1的name值
      System.out.println(student1.getName());
   } 
}
  • 运行后输出
Student@2d363fb3
Student@2d363fb3
小红
小明
小明
  • 很显然 这个类到现在为止 他已经只能创建且拥有一个对象 就算我们多创建几个 他们指向的都是同一块地址
    当一方做了更改 都会更改 因为它们用的都是同一块内存空间

总结一下步骤

1.将构造器私有化

2.在类的内部直接创建对象(必须要是static修饰)

3.提供一个公共的static方法 返回对象

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值