大话设计模式之抽象工厂模式

抽象工厂模式,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类

大话设计模式之抽象工厂模式_第1张图片

AbstractProductA和AbstractProductB是两个抽象产品,ProductA1、ProductA2和ProductB1、ProductB2就是对两个抽象产品的具体分类的实现,IFactory是一个抽象工厂接口,里面应该包含所有的产品创建的抽象方法,ConcreteFactory1和ConcreteFactory2就是具体工厂,通常是在运行时刻再创建一个ConcreteFactory类的实例,这个具体的工厂再创建具有特定实现的产品对象。

抽象工厂模式主要是采用面向接口编程的思想。

实际上应用配置文件+反射+抽象工厂模式能更进一步提高程序的可维护、可扩展的问题

大话设计模式之抽象工厂模式_第2张图片

配置文件配置db,根据db加载类,动态加载生成User和Department接口实例

package AbstractFactory;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * dbPriority.priority文件中包含db的配置
 * db=Oracle
 */
public class MainClass {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		User user = DataAccess.createUser();
		user.insert("user");
		user.getUser();
		
		Department department = DataAccess.createDepartment();
		department.insert("department");
		department.getDepartment();
	}
}

interface User {
	public void insert(String user);
	public void getUser();
}

class AccessUser implements User {

	@Override
	public void insert(String user) {
		// TODO Auto-generated method stub
		System.out.println("Access insert User");
	}

	@Override
	public void getUser() {
		// TODO Auto-generated method stub
		System.out.println("Access get User");
	}
}

class OracleUser implements User {

	@Override
	public void insert(String user) {
		// TODO Auto-generated method stub
		System.out.println("Oracle insert User");
	}

	@Override
	public void getUser() {
		// TODO Auto-generated method stub
		System.out.println("Oracle get User");
	}
}

interface Department {
	public void insert(String department);
	public void getDepartment();
}

class AccessDepartment implements Department {

	@Override
	public void insert(String department) {
		// TODO Auto-generated method stub
		System.out.println("Access insert department");
	}

	@Override
	public void getDepartment() {
		// TODO Auto-generated method stub
		System.out.println("Access get Department");
	}
	
}

class OracleDepartment implements Department {

	@Override
	public void insert(String department) {
		// TODO Auto-generated method stub
		System.out.println("Oracle insert department");
	}

	@Override
	public void getDepartment() {
		// TODO Auto-generated method stub
		System.out.println("Oracle get Department");
	}
}

class DataAccess {
	static {
		Properties properties = new Properties();
		try (FileReader freader = new FileReader("src\\AbstractFactory\\dbProperty.property")) {
			properties.load(freader);
			db = properties.getProperty("db");
			System.out.println(DataAccess.db);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static String db;
	
	public static User createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (User) Class.forName("AbstractFactory."+db+"User").newInstance();
	}
	
	public static Department createDepartment() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (Department) Class.forName("AbstractFactory."+db+"Department").newInstance();
	}
}


你可能感兴趣的:(大话设计模式之抽象工厂模式)