抽象工厂模式
Posted yfy-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽象工厂模式相关的知识,希望对你有一定的参考价值。
一、定义
抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定一个具体的类。
二、优点
1.方便更换产品的类型,由于具体工厂类在一个应用只需要初始化的时候出现一次,使得改变一个应用的具体工厂变得非常容易,只需改变具体的工厂即可使用不同的产品配置。
2.让具体的创建实例过程与客户端分离。客户端通过抽象接口操纵实例,产品的具体类名也被具体工厂的实现分离。
三、示例代码
利用抽象工厂模式操纵数据库。
//抽象工厂类,利用了简单工厂模式+反射+读取配置文件来改造
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DataAccess {
private static String packageName = "com.pattern.abstractFactory.";
private static String db = readDb();
public static String readDb() {
/*解析器*/
SAXReader saxReader = new SAXReader();
String db=null;
/*读取配置文件*/
try {
Document document = saxReader.read("D:com\pattern\abstractFactory\AppConfig.xml");
Element element = document.getRootElement();
Element em = element.element("appSettings");
db = em.elementText("db");
} catch (DocumentException e) {
e.printStackTrace();
}
return db;
}
public static IUser createUser() {
IUser dataClass = null;
try {
dataClass = (IUser) Class.forName(packageName + db + "User").newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return dataClass;
}
public static IDepartment createDepart() {
IDepartment dataClass = null;
try {
dataClass = (IDepartment) Class.forName(packageName + db + "Depart").newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return dataClass;
}
}
//抽象产品类
public interface IUser {
void insert(User user);
User getUser(int id);
}
public interface IDepartment {
void insert(Department department);
Department getDepartment(int id);
}
//实体类
public 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;
}
}
//实体类
public class Department {
private String departName;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
}
//具体工厂类
public class mysqlDepart implements IDepartment {
@Override
public void insert(Department department) {
System.out.println("插入一条部门数据");
}
@Override
public Department getDepartment(int id) {
System.out.println("获取部门对象");
return null;
}
}
public class MysqlUser implements IUser {
@Override
public void insert(User user) {
System.out.println("插入一条用户数据");
}
@Override
public User getUser(int id) {
System.out.println("获取用户对象");
return null;
}
}
public class OracleDepart implements IDepartment{
@Override
public void insert(Department department) {
System.out.println("插入一条部门数据");
}
@Override
public Department getDepartment(int id) {
System.out.println("获取部门对象");
return null;
}
}
public class OracleUser implements IUser {
@Override
public void insert(User user) {
System.out.println("插入一条用户数据");
}
@Override
public User getUser(int id) {
System.out.println("获取用户对象");
return null;
}
}
//客户端
public class Client {
public static void main(String[] args) {
User user=new User();
Department department=new Department();
IUser iUser=DataAccess.createUser();
iUser.insert(user);
iUser.getUser(3);
IDepartment iDepartment=DataAccess.createDepart();
iDepartment.insert(department);
iDepartment.getDepartment(5);
}
}
//配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appSettings>
<db>Mysql</db>
</appSettings>
</configuration>
以上是关于抽象工厂模式的主要内容,如果未能解决你的问题,请参考以下文章