千家信息网

设计模式之单例设计模式

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,SINGLETON(单件)-对象创建型模式1. 意图保证一个类仅有一个实例,并提供一个访问它的全局访问点。2. 动机对一些类来说,只有一个实例是很重要的。虽然系统中可以有许多打印机,但却只应该有一个打
千家信息网最后更新 2026年10月31日设计模式之单例设计模式

SINGLETON(单件)-对象创建型模式

1. 意图

保证一个类仅有一个实例,并提供一个访问它的全局访问点。

2. 动机

对一些类来说,只有一个实例是很重要的。虽然系统中可以有许多打印机,但却只应该有一个打印假脱机(printer spooler),只应该有一个文件系统和一个窗口管理器。一个数字滤波器只能有一个A / D转换器。一个会计系统只能专用于一个公司。

我们怎么样才能保证一个类只有一个实例并且这个实例易于被访问呢?一个全局变量使得一个对象可以被访问,但它不能防止你实例化多个对象。一个更好的办法是,让类自身负责保存它的唯一实例。这个类可以保证没有其他实例可以被创建(通过截取创建新对象的请求),并且它可以提供一个访问该实例的方法。这就是S i n g l e t o n模式。

3. 适用性

在下面的情况下可以使用S i n g l e t o n模式

当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。

当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。

适用场景:

将来系统是高并发的,比如dbinfo,数据库信息放在配置文件里面,高并发作用是只读取一次配置文件。

dbinfo

servlet

监听器

过滤器

注:request和response不是单例对象。每个人访问的都是同一个servlet,但是不同在于,每个人的请求request是不同的,request对象是servlet容器创建的。request对象跟HTTP请求绑定在一起的,一个HTTP请求绑定一个request。

application是一个全局变量,也是一个单例对象。

4. 结构

5. 参与者

S i n g l e t o n

- 定义一个 I n s t a n c e操作,允许客户访问它的唯一实例。 I n s t a n c e是一个类操作(即

S m a l l t a l k中的一个类方法和C + +中的一个静态成员函数)。

- 可能负责创建它自己的唯一实例。

6. 协作

客户只能通过S i n g l e t o n的I n s t a n c e操作访问一个S i n g l e t o n的实例。

7.实现

常见单例的两种写法:

写法一:

public class ConnectionFactory {                private static ConnectionFactory factory;                    //单例对象        private DbInfo dbinfo;                private ConnectionFactory(DbInfo dbinfo){                        this.dbinfo = dbinfo;         }                public static ConnectionFactory instance(){                if(factory == null){                          DbInfo dbinfo = DbInfo.instance();                        factory = new ConnectionFactory(dbinfo);                                 }                return factory;        }                /**         * 打开一个数据库连接         * @return         */        public DbConnection openConnection(){                DbConnection connection;                if(this.dbinfo.getDriver().equals("oracle")){                        connection = new OracleConnection(factory);                }else{                        connection = new MysqlConnection(factory);                }                                return connection;        }               }

写法二:

public class ConnectionFactory2 {                private static ConnectionFactory2 factory;              //1.要有单例对象                private ConnectionFactory2(){                        }                static{                                factory = new ConnectionFactory2();                        }                public static DbConnection openConnection(){                                DbConnection connection;                                DbInfo dbinfo = DbInfo.instance();                if(dbinfo.getDriver().equals("oracle")){                        connection = new OracleConnection(factory);                }else{                        connection = new MysqlConnection(factory);                }                                return connection;                                 }}


线程相关单例写法:

//饿汉式。(常用)

class Single{    private static final Single s = new Single();    private Single(){}    public static Single getInstance()    {        return s;    }}

//懒汉式(延迟加载单例设计模式)

class Single{    private static Single s = null;    private Single(){}    public static  Single getInstance(){        if(s==null){       //多重判断            synchronized(Single.class){     //注意锁的用法                if(s==null)                    s = new Single();            }        }        return s;    }}

单例模式有以下特点:
1、单例类只能有一个实例。
2、单例类必须自己创建自己的唯一实例。
3、单例类必须给所有其他对象提供这一实例。


8.将数据库配置文件(文件配置,xml配置)内容解析到单例对象中

01.解析配置文件

配置文件

dbURL=jdbc:oracle:thin:@10.0.19.252:1521:orcldbDriver=oracle.jdbc.driver.OracleDriverusername=motopassword=123456

单例类

public class DbInfo {        private static DbInfo dbInfo;                      //单例对象        private String dbURL;        private String dbDriver;        private String username;        private String password;                private DbInfo(){                        }                public static DbInfo instance() throws Exception{                if(dbInfo == null){                        dbInfo = new DbInfo();                        dbInfo.init();                }                                       return dbInfo;        }                /**         * 读取配置文件,给属性初始化         */        private void init() throws Exception {                Properties prop = new Properties();                String path = DbInfo.class.getResource("/").getPath() + "db.properties";                prop.load(new FileInputStream(new File(path)));                this.dbDriver = prop.getProperty("dbDriver");                this.dbURL = prop.getProperty("dbURL");                this.password = prop.getProperty("password");                this.username = prop.getProperty("username");        }        public String getDbURL() {                return dbURL;        }               public String getDbDriver() {                return dbDriver;        }                public String getUsername() {                return username;        }               public String getPassword() {                return password;        }}

02.解析xml文件

xml文件

   jdbc:oracle:thin:@10.0.19.252:1521:orcl   oracle.jdbc.driver.OracleDriver   moto   123456       

单例类

public class DbInfo2 {                private static DbInfo2 dbInfo;                      //单例对象        private String dbURL;        private String dbDriver;        private String username;        private String password;        private Document document;                private DbInfo2(){                        }                public static DbInfo2 instance() throws Exception{                if(dbInfo == null){                        dbInfo = new DbInfo2();                        dbInfo.init();                }                                       return dbInfo;        }                /**         * 读取配置文件,给属性初始化         */        private void init() throws Exception {                                String path = DbInfo.class.getResource("/").getPath() + "config.xml";                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();                DocumentBuilder db = dbf.newDocumentBuilder();                document = db.parse(new File(path));                                                             //解析XML文件                       Node node = document.getElementsByTagName("DbInfo").item(0);                Element element = (Element)node;                dbURL = element.getAttribute("dbUrl");                dbDriver = element.getAttribute("dbDriver");                this.username = element.getAttribute("username");                this.password = element.getAttribute("password");        }        public String getDbURL() {                               return dbURL;        }               public String getDbDriver() {                return dbDriver;        }                public String getUsername() {                return username;        }               public String getPassword() {                return password;        }}
/*** 读取配置文件,给属性初始化*///解析//private void init() throws Exception {                        String path = DbInfo.class.getResource("/").getPath() + "config.xml";        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();        DocumentBuilder db = dbf.newDocumentBuilder();        document = db.parse(new File(path));                                                             //解析XML文件               Node node = document.getElementsByTagName("DbInfo").item(0);        Element element = (Element)node;        dbURL = element.getAttribute("dbUrl");        dbDriver = element.getAttribute("dbDriver");        this.username = element.getAttribute("username");        this.password = element.getAttribute("password");}
实例 对象 文件 配置 模式 写法 客户 系统 全局 属性 数据 数据库 保证 不同 变量 只有 方法 设计模式 设计 重要 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 哈尔滨大商圈互联网科技有限公司 科技互联网政务服务 智能科技公司是互联网公司吗 北京互联网科技 互联网科技金融公司 互联网科技公司怎么样 深圳卡尔迅互联网科技有限公司怎么样 海南购全球互联网科技 北京三清互联网科技 欧洲十大科技互联网公司 互联网科技商业 亳州好赞互联网科技有限公司 群盈汇互联网科技有限公司 烟台灵鸽互联网科技有限公司 上海科技产业互联网平台 怎样加入科技互联网 智帮互联网科技公司最近动向 富国互联网金融科技混合基金 浙南科技城互联网大厦招聘 六子台科技互联网展会 河南真二互联网科技有限公司 有木互联网科技 国外互联网科技网站 左右互联网络科技 联恩商钥互联网科技有限公司 字节跳动和互联网科技 栖莱科技互联网股份有限公司 互联网时代的科技公司 湖北辰鑫科技互联网 10位科技互联网大佬
0