11-抽象工厂模式
概念
抽象工厂模式,提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类
举例
项目中需要将数据库从SqlServer换成Access,User表和Department表的插入和获取都需要重写
package com.niao.abstractFactory;
public interface IUser {
void insert(User user);
User get(String id);
}
package com.niao.abstractFactory;
public class SqlServerUser implements IUser{
@Override
public void insert(User user) {
System.out.println("在sqlServer中插入user数据");
}
@Override
public User get(String id) {
System.out.println("在sqlServer中获取user数据");
return null;
}
}
package com.niao.abstractFactory;
public class AccessUser implements IUser{
@Override
public void insert(User user) {
System.out.println("在Access中插入user数据");
}
@Override
public User get(String id) {
System.out.println("在Access中获取user数据");
return null;
}
}
package com.niao.abstractFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String id;
private String name;
private String password;
}
package com.niao.abstractFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {
private String id;
private String name;
private int peopleNum;
}
package com.niao.abstractFactory;
public interface IDepartment {
void insert(Department department);
Department get(String id);
}
package com.niao.abstractFactory;
public class AccessDepartment implements IDepartment{
@Override
public void insert(Department department) {
System.out.println("在Access中插入department数据");
}
@Override
public Department get(String id) {
System.out.println("在Access中获取department数据");
return null;
}
}
package com.niao.abstractFactory;
public class SqlServerDepartment implements IDepartment{
@Override
public void insert(Department department) {
System.out.println("在SqlServer中插入department数据");
}
@Override
public Department get(String id) {
System.out.println("在SqlServer中获取department数据");
return null;
}
}
package com.niao.abstractFactory;
public interface IFactory {
IUser createUser();
IDepartment createDepartment();
}
package com.niao.abstractFactory;
public class AccessFactory implements IFactory{
@Override
public IUser createUser() {
return new AccessUser();
}
@Override
public IDepartment createDepartment() {
return new AccessDepartment();
}
}
package com.niao.abstractFactory;
public class SqlServerFactory implements IFactory{
@Override
public IUser createUser() {
return new SqlServerUser();
}
@Override
public IDepartment createDepartment() {
return new SqlServerDepartment();
}
}
package com.niao.abstractFactory;
import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyUtil {
private static Properties props;
static{
loadProps();
}
synchronized static private void loadProps(){
System.out.println("开始加载properties文件内容.......");
props = new Properties();
InputStream in = null;
try {
// <!--第一种,通过类加载器进行获取properties文件流-->
in = PropertyUtil.class.getClassLoader().getResourceAsStream("application.properties");
// <!--第二种,通过类进行获取properties文件流-->
//in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
props.load(in);
} catch (FileNotFoundException e) {
System.out.println("jdbc.properties文件未找到");
} catch (IOException e) {
System.out.println("出现IOException");
} finally {
try {
if(null != in) {
in.close();
}
} catch (IOException e) {
System.out.println("jdbc.properties文件流关闭出现异常");
}
}
System.out.println("加载properties文件内容完成...........");
System.out.println("properties文件内容:" + props);
}
public static String getProperty(String key){
if(null == props) {
loadProps();
}
return props.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
if(null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}
}
package com.niao.abstractFactory;
public class Client {
public static void main(String[] args) {
// 抽象工厂模式实现
// IFactory factory = new SqlServerFactory();
// IFactory factory = new AccessFactory();
// IDepartment department = factory.createDepartment();
// 简单工厂模式实现
IDepartment department = DataAccess.createDepartment();
Department department1 = new Department("1", "教育部", 50);
department.insert(department1);
}
}
package com.niao.abstractFactory;
public class DataAccess {
// private static final String DB = "SqlServer";
private static final String DB = PropertyUtil.getProperty("database.type");
public static IUser createUser(){
IUser result = null;
switch (DB){
case "SqlServer":
result = new SqlServerUser();
break;
case "Access":
result = new AccessUser();
break;
}
return result;
}
public static IDepartment createDepartment(){
IDepartment result = null;
switch (DB){
case "SqlServer":
result = new SqlServerDepartment();
break;
case "Access":
result = new AccessDepartment();
break;
}
return result;
}
}
database.type=Access
待实现
使用反射代替switch还未实现,遗留
标签:11,String,System,工厂,抽象,abstractFactory,println,com,public From: https://www.cnblogs.com/Oh-mydream/p/17071510.html