论坛首页 入门技术论坛

JAVA模拟 工厂方法

浏览 2214 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-27   最后修改:2008-11-27

定义一个功能性接口

package model;

import java.sql.Connection;
import java.sql.SQLException;

public interface BuildConnection {
	Connection getConnection() throws SQLException;
}

 实现一个具体的connection产品,它实现BuildConnection的接口

package model;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataSourceConnection implements BuildConnection {
	
	private static Properties prop = new Properties();
	
	private DataSource ds;
	
	private String initialContextFactory;
	
	private String providerUrl;
	
	private String securityPrincipal;
	
	private String securityCredentials;
	
	private String dataSource;
	
	public DataSourceConnection(String securityPrincipal, String securityCredentials, String dataSource, String providerUrl, String initialContextFactory) throws NamingException {
		this.securityPrincipal = securityPrincipal;
		this.securityCredentials = securityCredentials;
		this.dataSource = dataSource;
		this.providerUrl = providerUrl; 
		this.initialContextFactory = initialContextFactory;
		loadDriver();
	}

	public Connection getConnection() throws SQLException {
		return ds.getConnection();
	}
	
	private void loadDriver() throws NamingException {
		if (!(prop.contains(initialContextFactory) && prop.contains(providerUrl) && prop.contains(securityPrincipal) && prop.contains(securityCredentials))) {
			prop.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
			prop.put(Context.PROVIDER_URL, providerUrl);
			prop.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
			prop.put(Context.SECURITY_CREDENTIALS, securityCredentials);
		}
		Context context = new InitialContext(prop);
		ds =(DataSource)context.lookup(dataSource); 
	}

}

 实现另一个具体的connection产品,它实现BuildConnection的接口

package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

public class DriverManagerConnection implements BuildConnection {
	
	private static Set<String> set = new HashSet<String>();
	
	private String driver;
	
	private String url;
	
	private String userId;
	
	private String password;

	public DriverManagerConnection(String driver, String url, String userId, String password) throws ClassNotFoundException {
		this.driver = driver;
		this.url = url;
		this.userId = userId;
		this.password = password;
		loadDriver();
	}

	private void loadDriver() throws ClassNotFoundException {
		if (!set.contains(driver)) {
			Class.forName(driver); 
			set.add(driver);	   
		}
	}

	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, userId, password);
	}

}

 定义一个可以生产connection的工厂

package model;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.NamingException;

public class ConnectionFactory {
	
	private BuildConnection delegate;

	public ConnectionFactory(String aDriver, String aUrl, String aUserid, String aPassword) throws ClassNotFoundException {
		super();
		delegate = new DriverManagerConnection(aDriver, aUrl, aUserid, aPassword);
	}

	public ConnectionFactory(String userId, String password, String dataSource, String providerUrl, String initialContextFactory) throws NamingException {
		super();
		delegate = new DataSourceConnection(userId, password, dataSource, providerUrl, initialContextFactory);
	}

	public Connection getConnection() throws SQLException {
		Connection connection = delegate.getConnection();
		connection.setAutoCommit(false);
		return connection;
	}
}

 

定义一个管理connection的类

package model;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.NamingException;
import tool.DriverNotFound;

public class DatabaseManagement {
	
	private static final String TYPE_JDBC = "jdbc";
	private static final String TYPE_DS = "ds";
	private static final String TYPE = "type";
	private static final String USERID = "userid";
	private static final String PASSWORD = "password";
	private static final String DRIVER = "driver";
	private static final String URL = "url";
	private static final String DATASOURCE = "datasource";
	private static final String PROVIDER_URL = "provider_url";
	private static final String INITIAL_CONTEXT_FACTORY = "initial_context_factory";

	private static final String file = "databaseInformation.properties";
	
	private static String type = null;
	private static String userid = null;
	private static String password = null;
	private static String driver = null;
	private static String url = null;
	private static String datasource = null;
	private static String initialContextFactory = null;
	private static String providerUrl = null;

	static {
		refresh();
	}

	public static void refresh() {
		try {
			InputStream inputStream = DatabaseManagement.class.getClassLoader().getResourceAsStream(file);
			if (inputStream != null) { 
				Properties properties = new Properties();
				properties.load(inputStream);
				type = properties.getProperty(TYPE, type);
				if (type.equalsIgnoreCase(TYPE_JDBC)) {
					userid = properties.getProperty(USERID, userid);
					password = properties.getProperty(PASSWORD, password);
					driver = properties.getProperty(DRIVER, driver);
					url = properties.getProperty(URL, url);
				}
				if (type.equalsIgnoreCase(TYPE_DS)) {
					userid = properties.getProperty(USERID, userid);
					password = properties.getProperty(PASSWORD, password);
					datasource = properties.getProperty(DATASOURCE, datasource);
					providerUrl = properties.getProperty(PROVIDER_URL, providerUrl);
					initialContextFactory = properties.getProperty(INITIAL_CONTEXT_FACTORY, initialContextFactory);
				}
			} 
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	public static Connection getConnection() throws SQLException, DriverNotFound {
		ConnectionFactory factory = null;
		try {
			if (type.equalsIgnoreCase(TYPE_JDBC)) {
				factory = new ConnectionFactory(driver, url, userid, password);
			} else {
				factory = new ConnectionFactory(userid, password, datasource, providerUrl, initialContextFactory);
			}
		} catch (ClassNotFoundException e) {
			throw new DriverNotFound(driver);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return factory.getConnection();
	}

}

 

定义一个异常类

package tool;

public class DriverNotFound extends Exception {
	
	private static final long serialVersionUID = -6926830075212095043L;
	
	private String driver;

	public DriverNotFound(String driver) {
		super("Driver " + driver + " not found");
		setDriver(driver);
	}

	public String getDriver() {
		return driver;
	}

	private void setDriver(String driver) {
		this.driver = driver;
	}
}

 

   发表时间:2008-11-27  
其实多态一个比较麻烦的问题就是"何时榜定具体类型",接口确实能降低耦合和依赖性但是早晚要帮定到具体类型的,如果处理不好这个榜定的时机就会导致诸如"过多分层"的问题导致一些无谓的传递.
0 请登录后投票
   发表时间:2008-11-30  
这个时机确实很难把握
0 请登录后投票
   发表时间:2008-11-30  
感觉此文应被放入新手区~~~
为什么我发的关于具体技术的文章老被放到入门区,
而此类虽然内容较多,但价值不大的文章却可以堂而皇之的不放到新手区呢?

难道管理员评价贴子质量是因内容多少,或更宏观来作为评价标准?
哎~~~
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics