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

    作用:

    只需要一个实例的类
    应用环境:黄帝、父母亲人、唯一的事务等

    实现方式:

    懒汉模式

    /**
     * 
     */
    package com.singleton;
    
    /**
     * <pre>
     * <b>.</b>
     * <b>Description:</b> 
     *    
     * <b>Date:</b> 2016年11月7日 下午9:01:18
     * <b>Copyright:</b> Copyright ©2000-2016 
     * <b>Changelog:</b>
     *   Ver   Date                         Author                   Detail
     *   --------------------------------------------------------------------------------
     *   单例模式之懒汉模式
     * </pre>
     */
    public class LazyModel {
        // 构造函数私有化,防止生成多个实例
        private LazyModel() {
    
        }
    
        // 建自身私有静态属性(为了使用)
        private static LazyModel lazy;
    
        // 创建获取单例的方法
        private static LazyModel getModel() {
            // 判断实例条件
            if (lazy == null) {
                // 实例
                lazy = new LazyModel();
            }
            return lazy;
        }
    }
    

    饿汉模式

    /**
     * 
     */
    package com.singleton;
    
    /**
     * <pre>
     * <b>.</b>
     * <b>Description:</b> 
     *    
     * <b>Author:</b> 
     * <b>Date:</b> 2016年11月7日 下午9:01:18
     * <b>Copyright:</b> Copyright ©2000-2016 reserved.
     * <b>Changelog:</b>
     *   Ver   Date                         Author                   Detail
     *   --------------------------------------------------------------------------------
     *   1.0   2016年11月7日 下午9:01:18     
     *   单例模式之饿汉模式
     * </pre>
     */
    public class HugryModel {
        // 构造函数私有化,防止生成多个实例
        private HugryModel() {
    
        }
    
        // 建自身私有静态属性(为了使用),并实例
        private static HugryModel lazy=new HugryModel();
    
        // 创建获取单例的方法
        private static HugryModel getModel() {
            return lazy;
        }
    }
    
  • 相关阅读:
    git this exceeds GitHub's file size limit of 100.00 MB
    使用vue-cli创建vue工程
    【转】Visual Studio Code必备插件
    linux安装openssl
    Centos7离线安装mysql8
    使用nmon来按频率采集数据
    Mac下编译android4.0.4遇到的问题
    32位ubuntu16.4编译android4.1.1
    vmvare安装vmtools菜单灰色
    Substrate 使用
  • 原文地址:https://www.cnblogs.com/xieji233/p/6155608.html
Copyright © 2011-2022 走看看