• Java Instance return model note!


    1. We have a class and define a static method of  getInstance()
     
    public class GenDarwinGlobal extends GenDarwinGlobalHelper
    {
        
    /**
         * Script Name   : <b>GenDarwinGlobal</b>
         * Generated     : <b>Mar 31, 2011 1:16:48 AM</b>
         * Description   : Functional Test Script
         * Original Host : WinNT Version 5.2  Build 3790 (S)
         * 
         * 
    @since  2011/03/31
         * 
    @author dwei
         
    */
        
    public void testMain(Object[] args) 
        {
            
    // TODO Insert code here
        }
        
    public  static GenDarwinGlobal getInstance()
        {
            
    return new GenDarwinGlobal();
        }
    }
     
    2. Compare these code line, the getInstance sequence will get failed return because it has two new instance belong to different object.
    //        FucLib.GenevaFuc.GenDarwinGlobal.getInstance().openBuyTransaction();
        
    //    FucLib.GenevaFuc.GenDarwinGlobal.getInstance().ClickQuery();
            
            
            FucLib.GenevaFuc.GenDarwinGlobal oGD 
    =  new FucLib.GenevaFuc.GenDarwinGlobal(); 
            oGD.openBuyTransaction();
            oGD.ClickQuery();
    3. Also we can using this method to resolve this problem in the instance code .
     
    private static GenDarwinGlobal instance = null;
    public static synchronized GenDarwinGlobal getInstance()
    {
       
    if( instance == null)
       {
        instance 
    = new GenDarwinGlobal();
       }
        
    return instance;
    }
     
     
  • 相关阅读:
    sqlite轻量型数据库笔记
    WPF学习之MVVM笔记
    Halcon 圆测量与直线测量
    深入浅出WPF_读书笔记
    研华轴卡PCI1245L配ADAM3956接线板与台达ASD-A2伺服驱动器和松下A5伺服驱动器
    dataGridView添加ComboBox 每行绑定不同的集合,显示默认值
    VS2015 注释英文
    java的安装环境配置详细步骤
    万能正则解析 json 数据 解析成键值对
    http 异步 接收 回传 数据文字和文件流
  • 原文地址:https://www.cnblogs.com/zencorn/p/2043294.html
Copyright © 2020-2023  润新知