单例软件设计模式初解
来源:互联网 发布:matlab 生成稀疏矩阵 编辑:程序博客网 时间:2024/06/06 10:38
在java中单例设计模式被字面定义为:“一个类有且仅有一个实例,并且自行实例化向整个系统提供。
在内存中,一个类,或者一个对象被static修饰。用来被所有的项目中的其他类调用。
如下 实例化hiberbat框架中的工厂类
package hibernate1;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HiberbatUtils {
private static final SessionFactory sf= buildSessionFactory();
public static SessionFactory buildSessionFactory(){
try{
Configuration configuration = new Configuration().configure();
StandardServiceRegistry standardServiceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(standardServiceRegistry);
return sessionFactory;
}catch(Throwable error){
System.err.println("Initial SessionFactory creation failed." + error);
throw new ExceptionInInitializerError(error);
}
}
public static SessionFactory getSessionFactory(){
return sf;
}
}
在代码最后返回 SessionFactory对象sf,使用单例设计模式适用于,频繁访问的占有大量运行空间的对象或者类。同时该类中的对象没有变量,不然出现程序bug。
利用单例设计模式,可以大量的节省内存空间,提升系统的运行流畅度。优化程序。
- 单例软件设计模式初解
- [软件设计模式]单例模式
- 软件设计模式- 单例模式
- 软件设计模式之单例模式
- 软件设计模式之单例模式
- 软件设计模式之单例设计模式
- 软件设计模式之单例模式
- 软件设计中的单例模式开发
- 单件模式--软件设计模式
- 软件设计模式之单例模式(JAVA实现)
- 软件设计模式——单例模式(Singleton)
- 面向对象软件设计模式(1)--单例模式(Singleton)
- Android中使用软件设计模式之观察者模式、Builder模式、单例模式、简单工厂模式
- 软件设计模式
- 软件设计模式
- 软件设计模式
- 软件设计模式
- 软件设计模式
- Uva 10615 Rooks(二分图完美匹配+补边)
- 选择Spring Boot项目的内嵌容器
- C++ 构造函数后面的冒号的作用
- laravel 学习笔记——路由(路由与控制器、资源路由)
- 数据库主从、主主,负载均衡,高可用
- 单例软件设计模式初解
- 深入理解Spring MVC 思想
- BootStrap(一)
- 文字超出隐藏并显示省略号
- restful接口
- HDU 5277 YJC counts stars 最大团+暴力
- FP-growth
- VMDK(VMWare Virtual Machine Disk Format)是虚拟机VMware创建的虚拟硬盘格式
- 2.1 浏览因特网资源