首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java实例的解释

java实例的解释
EN

Stack Overflow用户
提问于 2014-09-01 07:13:01
回答 3查看 179关注 0票数 0

有人能告诉我这东西是干什么的吗?另外,如果有人可以举一个例子,如果会有帮助的话。

代码语言:javascript
运行
复制
public class ConnectionManager{
    private static ConnectionManager instance = null;
  .....}

以下是完整的代码:

代码语言:javascript
运行
复制
package com.gollahalli.main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager
{
    private static ConnectionManager instance = null;

    private final String USERNAME = "root";
    private final String PASSWORD = "root";
    private final String H_CONN_STRING = "jdbc:hsqldb:data/explorecalifornia";
    private final String M_CONN_STRING = "jdbc:mysql://localhost/explorecalifornia";

    private DBType dbType = DBType.MYSQL;

    private Connection conn = null;

    private ConnectionManager() { }

    public static ConnectionManager getInstance() {
        if (instance == null) {
            instance = new ConnectionManager();
        }
        return instance;
    }

    public void setDBType(DBType dbType) {
        this.dbType = dbType;
    }

    private boolean openConnection() {
        try {
            switch (dbType) {
            case MYSQL:
                conn = DriverManager.getConnection(M_CONN_STRING, USERNAME, PASSWORD);
                return true;

            case HSQLDB:
                conn = DriverManager.getConnection(H_CONN_STRING, USERNAME, PASSWORD);
                return true;

            default: 
                return false;
            }
        }
        catch (SQLException e) {
            System.err.println(e);
            return false;
        }
    }

    public Connection getConnection() {
        if (conn == null) {
            if (openConnection()) {
                System.out.println("Connection opened");
                return conn;
            } else {
                return null;
            }
        }
        return conn;
    }

    public void close() {
        System.out.println("Closing connection");
        try {
            conn.close();
            conn = null;
        } catch (Exception e) { }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-01 07:24:32

这是单例设计模式

它用来确保只能创建一个类的一个实例。

代码语言:javascript
运行
复制
public class MySingletonClass {

    private static MySingletonClass instance;


    public synchronized static MySingletonClass getInstance() {
        if (instance == null) {
            instance = new MySingletonClass(); // "lazy" initialization
        }

        return instance;
    }


   /**
    * private constructor can be called only inside of MySingleton class, but not from    outside.
   */
   private MySingletonClass() {
       // your code here
   }
}

因此,要在代码中获取该类的实例,开发人员不使用构造函数。

开发人员使用静态方法getInstance()

代码语言:javascript
运行
复制
MySingletonClass mySingleton = MySingletonClass.getInstance();

请小心对待单身汉。许多新手开发人员滥用单例,并将它们作为全局变量使用。不要这样做:)

更新:

我将synchronized添加到getInstance()方法中,以使其线程安全。

票数 2
EN

Stack Overflow用户

发布于 2014-09-01 07:15:26

它只声明一个名为instance的字段,其类型为ConnectionManager,并将其初始化为null (这是多余的,因为这无论如何都是它的默认值)。

根据instance字段声明和类的名称,这个类很可能是一个单例类(它们只允许一个实例)。

票数 1
EN

Stack Overflow用户

发布于 2014-09-01 07:24:41

这叫单例模式

当您只需要类的一个对象,即单例时,就会使用它。它将只构建一次,然后您可以通过getInstance()访问它。

朴素实现

代码语言:javascript
运行
复制
public class SingletonDemo {
    //Holds the singleton
    private static SingletonDemo instance = null;

    //Overrides default constructor, not to instantiate another one.
    //Only getInstance will construct
    private SingletonDemo() { }

    //Only this method can construct a singleton, always call this one
    public static SingletonDemo getInstance() {
        if (instance == null) { //No singleton yet, create one
            instance = new SingletonDemo();
        }
        //return the singleton (created this time or not)
        return instance;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25600575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档