【黑马程序员】java基础_单例设计模式
2014-06-10 15:29
337 查看
------- android培训、java培训、期待与您交流! ----------
什么是设计模式?设计模式就是 解决某一类问题最有效的方法。
java的设计模式一共有23种
常见的如单例设计模式、模板设计模式、装饰设计模式等等,其中单例设计模式又分为懒汉式和饿汉式。
单例设计模式:保证对象唯一。
1、为了避免其他程序过多建立该类对象。先禁止其他程序建立该类对象。
2、还为了让其他程序可以访问到该类对象,只好在本类中,自定义一个对象。
3、为了方便其他程序对自定义对象的访问,可以对外提供一些访问方式
代码步骤:
1、将构造函数私有化
2、在类中创建一个本类对象,并静态私有化
3、提供一个方法获取到该对象
class Student
{
private String name;
private int age;
public void setInformation(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
//懒汉式(延时加载):什么时候调用,什么时候进内存
private static Student s = null; //饿汉式:private static final Student s = new Student();
private Student(){}
public static Student getInstance()
{
if(s == null) //饿汉式:不用
{ //饿汉式:不用
synchronized(Student.class) //饿汉式:不用
{ //饿汉式:不用
if(s == null) //饿汉式:不用
s = new Student(); //饿汉式:不用
} //饿汉式:不用
}
return s;
}
}
public class SingleDemo
{
public static void main(String[] args)
{
Student s1 = Student.getInstance();
Student s2 = Student.getInstance();
s1.setInformation("张三",21);
System.out.println("S1:"+s1.getName()+"\t"+s1.getAge());
System.out.println("S2:"+s2.getName()+"\t"+s2.getAge());
}
}
PS:一般情况下最好使用饿汉式,因为多人调用时懒汉式容易出错
PS2:解决懒汉式问题,用synchronized(锁)进行双重判断
什么是设计模式?设计模式就是 解决某一类问题最有效的方法。
java的设计模式一共有23种
常见的如单例设计模式、模板设计模式、装饰设计模式等等,其中单例设计模式又分为懒汉式和饿汉式。
单例设计模式:保证对象唯一。
1、为了避免其他程序过多建立该类对象。先禁止其他程序建立该类对象。
2、还为了让其他程序可以访问到该类对象,只好在本类中,自定义一个对象。
3、为了方便其他程序对自定义对象的访问,可以对外提供一些访问方式
代码步骤:
1、将构造函数私有化
2、在类中创建一个本类对象,并静态私有化
3、提供一个方法获取到该对象
class Student
{
private String name;
private int age;
public void setInformation(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
//懒汉式(延时加载):什么时候调用,什么时候进内存
private static Student s = null; //饿汉式:private static final Student s = new Student();
private Student(){}
public static Student getInstance()
{
if(s == null) //饿汉式:不用
{ //饿汉式:不用
synchronized(Student.class) //饿汉式:不用
{ //饿汉式:不用
if(s == null) //饿汉式:不用
s = new Student(); //饿汉式:不用
} //饿汉式:不用
}
return s;
}
}
public class SingleDemo
{
public static void main(String[] args)
{
Student s1 = Student.getInstance();
Student s2 = Student.getInstance();
s1.setInformation("张三",21);
System.out.println("S1:"+s1.getName()+"\t"+s1.getAge());
System.out.println("S2:"+s2.getName()+"\t"+s2.getAge());
}
}
PS:一般情况下最好使用饿汉式,因为多人调用时懒汉式容易出错
PS2:解决懒汉式问题,用synchronized(锁)进行双重判断
相关文章推荐
- 黑马程序员_java基础复习之十常用设计模式总结
- 黑马程序员__4java基础 静态和几种设计模式
- 黑马程序员 JAVA学习笔记——java基础 单例设计模式
- 黑马程序员_Java基础_线程基础,创建,同步(单例设计模式的同步),死锁
- 黑马程序员——JAVA基础之抽象和接口 , 模版方法设计模式
- 黑马程序员:Java基础总结----装饰设计模式
- 黑马程序员:Java基础总结----单例设计模式
- 黑马程序员 _ java基础 _ 之装饰者设计模式
- 黑马程序员_Java基础加强_Java类加载器,委托机制,模板方法设计模式
- 黑马程序员_Java基础[14]_模版设计模式、习题练习
- 黑马程序员-------Java基础之单例设计模式
- 黑马程序员_Java基础_面向对象,封装,继承,单例设计模式,构造函数,构造代码块
- 黑马程序员:Java基础总结----模版方法设计模式
- 黑马程序员 _ Java基础 _ 之单例设计模式
- 黑马程序员-JAVA基础-IO流中的装饰设计模式
- 黑马程序员_java基础学习笔记之单例设计模式
- 黑马程序员—Java基础加强 (基本数据的自动拆装箱及享元设计模式)
- 黑马程序员_Java基础_面向对象(Static的使用、对象初始化和调用成员过程、单例设计模式)
- 黑马程序员---JAVA基础JAVA23种设计模式之---单例设计模式
- 黑马程序员:java基础——单例设计模式,synchronized及面试常考内容