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;
        }
    }
    
  • 相关阅读:
    反向代理实例
    nginx常用命令和配置
    nginx的安装
    Can Live View boot up images acquired from 64bit OS evidence?
    What is the behavior of lnk files?
    EnCase v7 search hits in compound files?
    How to search compound files
    iOS 8.3 JB ready
    Sunglasses
    现代福尔摩斯
  • 原文地址:https://www.cnblogs.com/xieji233/p/6155608.html
Copyright © 2011-2022 走看看