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

    步骤

    1.属性私有,静态,为了让静态方法调用

    2.构造器私有,饿汉在构造器里就常见对象

    3.提供静态的访问方法

     1 package jquery;
     2 
     3 
     4 public class Singleton {
     5 
     6     //属性私有,因为提供获取对象的方法所以static声明
     7     private static Singleton singleton;
     8     
     9     //构造器私有,别人没法new对象
    10     private Singleton(){
    11         //在构造器就吃,属于饿汉模式
    12         singleton=new Singleton();
    13     }
    14     
    15     public static Singleton getSingleton(){
    16         return singleton;
    17     }
    18 }
    View Code

    懒汉模式

     1 package jquery;
     2 
     3 public class Singletonlan {
     4 
     5     private static Singletonlan singletonlan;
     6     
     7     private Singletonlan(){
     8         
     9     }
    10     
    11     public static Singletonlan getSingletonlan(){
    12         if(singletonlan==null){
    13             return new Singletonlan();
    14         }
    15         return null;
    16     }
    17 }
    View Code
  • 相关阅读:
    STL next_permutation 全排列
    日期问题
    兰顿蚂蚁
    矩阵翻硬币
    数学问题-排列组合
    h5css3_03练习
    h5css3_03
    h5css3_02练习
    h5css3_02
    h5c3_01练习
  • 原文地址:https://www.cnblogs.com/javaweb2/p/6237725.html
Copyright © 2011-2022 走看看