很好的抽象工厂设计模式

使用过了很多的工厂模式了,还是觉得这种工厂模式最好.
1、类的配置文件:
daoconfig.properties
#DAO Factory config
DAOFactory.className=com.hejianjiao.hibernate.dao.factory.HibernateDAOFactory


2、读取配置文件:
DAOConfig.java
package com.hejianjiao.hibernate.dao.factory;

import java.io.InputStream;
import java.util.Properties;

public abstract class DAOConfig {

	private static Properties props = null;

	private synchronized static void loadProperties() {
		try {
			props = new Properties();
			InputStream input = DAOConfig.class
					.getResourceAsStream("daoconfig.properties");
			props.load(input);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static String getProperty(String propName) {
		loadProperties();
		return props.getProperty(propName);
	}
}


3、抽象工厂类:
DAOFactory.java
package com.hejianjiao.hibernate.dao.factory;

import com.hejianjiao.hibernate.dao.ClassesDAO;
import com.hejianjiao.hibernate.dao.CourseDAO;
import com.hejianjiao.hibernate.dao.EnrolDAO;
import com.hejianjiao.hibernate.dao.StudentDAO;
import com.hejianjiao.hibernate.dao.TeacherDAO;

public abstract class DAOFactory {

	private static Object initLock = new Object();
	private static String className = "com.hejianjiao.dao.factory.HibernateDAOFactory";
	private static DAOFactory factory = null;

	public static DAOFactory getInstance() {
		if (factory == null) {
			// 以下使用了Singleton 单态模式
			synchronized (initLock) {
				if (factory == null) {
				}
				;

				String classNameProp = DAOConfig
						.getProperty("DAOFactory.className");

				if (classNameProp != null) {
					className = classNameProp;
				}
				try {
					// 动态转载类
					Class c = Class.forName(className);
					factory = (DAOFactory) c.newInstance();
				} catch (Exception ex) {
					System.out.println("Failed to loadDAOFactoryclass:"
							+ className + ".StuMan cannot function normally.");
					ex.printStackTrace();
					return null;
				}
			}
		}
		return factory;
	}

	// 定义抽象方法
	public abstract StudentDAO createStudentDAO();

	public abstract TeacherDAO createTeacherDAO();

	public abstract ClassesDAO createClassesDAO();

	public abstract CourseDAO createCourseDAO();

	public abstract EnrolDAO createEnrolDAO();

}


4、抽象工厂类继承者
HibernateDAO.java
package com.hejianjiao.hibernate.dao.factory;

import com.hejianjiao.hibernate.dao.ClassesDAO;
import com.hejianjiao.hibernate.dao.CourseDAO;
import com.hejianjiao.hibernate.dao.EnrolDAO;
import com.hejianjiao.hibernate.dao.StudentDAO;
import com.hejianjiao.hibernate.dao.TeacherDAO;
import com.hejianjiao.hibernate.dao.impl.ClassesDAOImpl;
import com.hejianjiao.hibernate.dao.impl.CourseDAOImpl;
import com.hejianjiao.hibernate.dao.impl.EnrolDAOImpl;
import com.hejianjiao.hibernate.dao.impl.StudentDAOImpl;
import com.hejianjiao.hibernate.dao.impl.TeacherDAOImpl;

public class HibernateDAOFactory extends DAOFactory {

	@Override
	public ClassesDAO createClassesDAO() {
		// TODO Auto-generated method stub
		return new ClassesDAOImpl();
	}

	@Override
	public CourseDAO createCourseDAO() {
		// TODO Auto-generated method stub
		return new CourseDAOImpl();
	}

	@Override
	public EnrolDAO createEnrolDAO() {
		// TODO Auto-generated method stub
		return new EnrolDAOImpl();
	}

	@Override
	public StudentDAO createStudentDAO() {
		// TODO Auto-generated method stub
		return new StudentDAOImpl();
	}

	@Override
	public TeacherDAO createTeacherDAO() {
		// TODO Auto-generated method stub
		return new TeacherDAOImpl();
	}

}

你可能感兴趣的:(java,DAO,设计模式,Hibernate,idea)