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

    单例模式的要点有三个;一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。我现在用的系统中的上传平台的类就是一个单例模式,最近看了些资料才知道的一些好处:1.实例控制:Singleton 会阻止其他对象实例化其自己的 Singleton 对象的副本,从而确保所有对象都访问唯一实例;2.灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程。使用单例模式的时机是当实例存在多个会引起程序逻辑错误的时候。比如类似有序的号码生成器这样的东西。

    单例模式的两种模式:

    饿汉式:

    public class Singleton {
    private static Singleton instance = new Singleton();
    private Singleton(){
     
    }
    public static Singleton getInstance(){
    return instance;
    }
    }
    懒汉式:
    public class Singleton {
    private static Singleton instance = null;
    private Singleton(){
    }
    public static Singleton getInstance(){
    if(instance==null){
    synchornized(Singleton.class){
    if(null == instance){
    instance = new Singleton();
    }
    }
    }
    return instance;
    }
    }
  • 相关阅读:
    爬虫的基本原理
    爬虫的分类
    gcc编译
    C++字符串总结
    PE文件格式学习笔记
    学习SDR过程中的参考网页
    Linux下源码编译安装遇到的问题
    web | jsp考试复习要点整理
    爬虫 | php封装 | file_get_contents
    re | [NPUCTF2020]EzObfus-Chapter2
  • 原文地址:https://www.cnblogs.com/hdsbk/p/2863100.html
Copyright © 2011-2022 走看看