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

以一种方法的两种数据库实现为例子.


第一种:抽象工厂

package com.wzs.three;

/**
 * 大话设计模式--page128 抽象工厂
 * 
 * @author Administrator
 * 
 */
public class AbstractFactory {
	public static void main(String[] args) {
		// IFactory factory = new SqlServerFactory();
		IFactory factory = new AccessFactory();

		User user = new User();
		IUser iu = factory.createUser();
		iu.insert(user);
		iu.getUser(1);

		Department department = new Department();
		IDepartment id = factory.createDepartment();
		id.insert(department);
		id.getDepartment(1);

	}
}

/*
 * 用户
 */
class User {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

/*
 * 部门
 */
class Department {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

/*
 * 抽象工厂
 */
interface IFactory {
	IUser createUser();

	IDepartment createDepartment();
}

/*
 * sql 工厂
 */
class SqlServerFactory implements IFactory {
	@Override
	public IUser createUser() {
		return new SqlServerUser();
	}

	@Override
	public IDepartment createDepartment() {
		return new SqlServerDepartment();
	}
}

/*
 * access 工厂
 */
class AccessFactory implements IFactory {
	@Override
	public IUser createUser() {
		return new AccessUser();
	}

	@Override
	public IDepartment createDepartment() {
		return new AccessDepartment();
	}

}

/*
 * 用户方法抽象接口
 */
interface IUser {
	void insert(User user);

	User getUser(int id);
}

/*
 * sql用户方法
 */
class SqlServerUser implements IUser {

	@Override
	public User getUser(int id) {
		System.out.println("在sql server中得到id=" + id + "的User记录.");
		return null;
	}

	@Override
	public void insert(User user) {
		System.out.println("在sql server中插入User新记录.");
	}

}

/*
 * access用户方法
 */
class AccessUser implements IUser {

	@Override
	public User getUser(int id) {
		System.out.println("在Access中得到id=" + id + "的User记录.");
		return null;
	}

	@Override
	public void insert(User user) {
		System.out.println("在Access中插入新User记录.");
	}

}

/*
 * 部门抽象方法接口
 */
interface IDepartment {
	void insert(Department department);

	Department getDepartment(int id);
}

/*
 * sql部门方法
 */
class SqlServerDepartment implements IDepartment {

	@Override
	public Department getDepartment(int id) {
		System.out.println("在sql server中得到id=" + id + "的Department记录.");
		return null;
	}

	@Override
	public void insert(Department department) {
		System.out.println("在sql server中插入新Department记录.");
	}
}

/*
 * access部门方法
 */
class AccessDepartment implements IDepartment {

	@Override
	public Department getDepartment(int id) {
		System.out.println("在Access中得到id=" + id + "的Department记录.");
		return null;
	}

	@Override
	public void insert(Department department) {
		System.out.println("在Access中插入新Department记录.");
	}

}


第二种:反射+抽象工厂

package com.wzs.two;

/**
 * 大话设计模式--page128 反射+抽象工厂
 * 
 * @author Administrator
 * 
 */
public class AbstractFactory {
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		// IFactory factory = new SqlServerFactory();

		User user = new User();
		IUser iu = DataAccess.createUser();
		iu.insert(user);
		iu.getUser(1);

		Department department = new Department();
		IDepartment id = DataAccess.createDepartment();
		id.insert(department);
		id.getDepartment(1);

	}
}

class DataAccess {
	private static String db = "Access";

	public static IUser createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IUser) Class.forName("com.wzs.two." + db + "User").newInstance();
	}

	public static IDepartment createDepartment() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IDepartment) Class.forName("com.wzs.two." + db + "Department").newInstance();
	}
}

class DataSqlServer {
	private static String db = "SqlServer";

	public static IUser createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IUser) Class.forName("com.wzs.two." + db + "User").newInstance();
	}

	public static IDepartment createDepartment() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IDepartment) Class.forName("com.wzs.two." + db + "Department").newInstance();
	}
}

/*
 * 用户
 */
class User {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

/*
 * 部门
 */
class Department {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

/*
 * 用户方法抽象接口
 */
interface IUser {
	void insert(User user);

	User getUser(int id);
}

/*
 * sql用户方法
 */
class SqlServerUser implements IUser {

	@Override
	public User getUser(int id) {
		System.out.println("在sql server中得到id=" + id + "的User记录.");
		return null;
	}

	@Override
	public void insert(User user) {
		System.out.println("在sql server中插入User新记录.");
	}

}

/*
 * access用户方法
 */
class AccessUser implements IUser {

	@Override
	public User getUser(int id) {
		System.out.println("在Access中得到id=" + id + "的User记录.");
		return null;
	}

	@Override
	public void insert(User user) {
		System.out.println("在Access中插入新User记录.");
	}

}

/*
 * 部门抽象方法接口
 */
interface IDepartment {
	void insert(Department department);

	Department getDepartment(int id);
}

/*
 * sql部门方法
 */
class SqlServerDepartment implements IDepartment {

	@Override
	public Department getDepartment(int id) {
		System.out.println("在sql server中得到id=" + id + "的Department记录.");
		return null;
	}

	@Override
	public void insert(Department department) {
		System.out.println("在sql server中插入新Department记录.");
	}
}

/*
 * access部门方法
 */
class AccessDepartment implements IDepartment {

	@Override
	public Department getDepartment(int id) {
		System.out.println("在Access中得到id=" + id + "的Department记录.");
		return null;
	}

	@Override
	public void insert(Department department) {
		System.out.println("在Access中插入新Department记录.");
	}

}


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