java实例说明
谁能告诉我这东西是干什么的?还有,如果有人能举个例子,我会很有帮助的java实例说明,java,Java,谁能告诉我这东西是干什么的?还有,如果有人能举个例子,我会很有帮助的 public class ConnectionManager{ private static ConnectionManager instance = null; .....} 以下是完整的代码: package com.gollahalli.main; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLEx
public class ConnectionManager{
private static ConnectionManager instance = null;
.....}
以下是完整的代码:
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) { }
}
}
它只是声明一个名为
实例
的字段,其类型为连接管理器
,并将其初始化为空
(这是多余的,因为这将是它的默认值)
根据
实例
字段声明和类的名称判断,该类很可能是一个单例类(只允许从它们中选择一个实例)。它只声明一个名为实例
的字段,其类型为连接管理器
,并将其初始化为空
(这是多余的,因为这将是它的默认值)
根据实例
字段声明和类的名称判断,该类很可能是一个单例类(只允许来自它们的一个实例)
它用于确保一个类只能创建一个实例
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()
请小心使用单例。许多新手开发人员滥用单例并将其用作全局变量。不要这样做:)
更新:
我在getInstance()
方法中添加了synchronized
,使其成为线程安全的方法。这里有
它用于确保一个类只能创建一个实例
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()
请小心单身人士。许多新手开发人员滥用单例并将其用作全局变量。不要这样做:)
更新:
我在getInstance()
方法中添加了synchronized
,以使其线程安全。它被称为
当您只需要一个类的一个对象,即singleton时,就可以使用此选项。它将只构造一次,然后您可以通过getInstance()访问它
天真的实现
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;
}
}
它叫“世界杯”
当您只需要一个类的一个对象,即singleton时,就可以使用此选项。它将只构造一次,然后您可以通过getInstance()访问它
天真的实现
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;
}
}
似乎是单例模式实现的开始。您能向我们展示完整的代码吗?@sᴜʀᴇsʜᴀᴛᴛᴀ 这是你的电话号码code@user3782963请在此处选择“全部+复制+粘贴”。@RohitJain这是一个相当大的问题。请看一下这似乎是单例模式实现的开始。您能向我们展示完整的代码吗?@sᴜʀᴇsʜᴀᴛᴛᴀ 这是你的电话号码code@user3782963请在这里选择“全部+复制+粘贴”。@RohitJain这是一个相当大的文件。请看一下这个文件。非常感谢您的帮助。但是什么是单例类呢?有时候你真的应该使用谷歌。“单身阶级”在谷歌上首次流行。非常感谢你的帮助。但是什么是单例类呢?有时候你真的应该使用谷歌。“单身阶级”在谷歌上首次流行。使其线程安全的一个最简单的解决方案是将enum用于singleton@HamoriZ不仅仅是最简单的一个。这是在Java中创建延迟加载高效线程安全单例的最简单方法。@brimborium您在我的回答中做了哪些更改?@RafaelOsipov您可以单击编辑链接查看历史记录,每次更改都使用dif。在本例中,您编写了
syncrhonized
,我刚刚解决了这个问题singleton@HamoriZ不仅仅是最简单的一个。这是在Java中创建延迟加载高效线程安全单例的最简单方法。@brimborium您在我的回答中做了哪些更改?@RafaelOsipov您可以单击编辑链接查看历史记录,每次更改都使用dif。在本例中,您编写了syncrhonized
,我刚刚修复了它。