反射+配置文件+抽象工厂

Posted 程姐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反射+配置文件+抽象工厂相关的知识,希望对你有一定的参考价值。

用反射技术去除Switch或者if,解除简单工厂类的判断语句带来的耦合。

用配置文件全项目替代数据库类型实现Oracle、mysql,或者加入SqlServer等数据库实现。

优点:比抽象工厂减少耦合(Ifactory,MysqlFactory,OracleFactory变为一个ReflectSqlFactory,而且切换数据库只需要改配置文件)

     比简单工厂减少耦合(采用反射技术,减少switch或if 语句)

sql.properties

db=Mysql

ReflectSqlFactory类

package reflectAbstractFactory;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

import reflectAbstractFactory.dept.IDeptDao;
import reflectAbstractFactory.user.IUserDao;

public class ReflectSqlFactory {
    public String getDb() {
        Properties prop = new Properties();
        String db = "";
        try {
            // 读取属性文件a.properties
            InputStream in = new BufferedInputStream(new FileInputStream(
                    "E:/workspace/dahuasheji/sampleFactory/src/reflectAbstractFactory/sql.properties"));
            prop.load(in); // /加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                if("db".endsWith(key)){
                    db = prop.getProperty("db");
                    break;
                }
            }
            in.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return db;
    }
    public Class<?> getsqlFactory(String pge,String type) throws ClassNotFoundException{
        String className= "reflectAbstractFactory."+pge+getDb()+type+"Dao";
        System.out.println(className);
        Class<?> c1 = Class.forName(className);
        System.out.println(c1.getName());
        return c1;
    }
    public IUserDao createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
        Class<?> c = getsqlFactory("user.","User");
        IUserDao iu = (IUserDao)c.newInstance();
        return iu;
    };
    public IDeptDao createDept() throws ClassNotFoundException, InstantiationException, IllegalAccessException{
        Class<?> c = getsqlFactory("dept.","Dept");
        IDeptDao id = (IDeptDao)c.newInstance();
        return id;
    };
}

Business类

package reflectAbstractFactory;

import reflectAbstractFactory.dept.Department;
import reflectAbstractFactory.dept.IDeptDao;
import reflectAbstractFactory.user.IUserDao;
import reflectAbstractFactory.user.User;


public class Business {

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        //产品1系列
        User user = new User();
        ReflectSqlFactory reflectSqlFactory = new ReflectSqlFactory();
        IUserDao iuDao = reflectSqlFactory.createUser();
        iuDao.insert(user);
        iuDao.getUser(1);
        
        //产品2系列
        Department dept = new Department();
        IDeptDao idepatDao = reflectSqlFactory.createDept();
        idepatDao.insert(dept);
        idepatDao.getDept(1);
        
    }

}

Department类

package reflectAbstractFactory.dept;

public class Department {
    private long id;
    private String deptName;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    
}

IDeptDao类

package reflectAbstractFactory.dept;

public interface IDeptDao {
    void insert(Department dept);
    void getDept(int id);
}

MysqlDeptDao类

package reflectAbstractFactory.dept;

public class MysqlDeptDao implements IDeptDao {

    @Override
    public void insert(Department dept) {
        System.out.println("在mysql数据库的department表插入一条记录");
    }

    @Override
    public void getDept(int id) {
        System.out.println("在mysql数据库的department表根据ID获取一条记录");
    }

}

OracleDeptDao类

package reflectAbstractFactory.dept;

public class OracleDeptDao implements IDeptDao {

    @Override
    public void insert(Department dept) {
        System.out.println("在oracle数据库的department表插入一条记录");
    }

    @Override
    public void getDept(int id) {
        System.out.println("在oracle数据库的department表根据ID获取一条记录");
    }

}

IUserDao类

package reflectAbstractFactory.user;

public interface IUserDao {
    void insert(User user);
    void getUser(int id);
}

MysqlUserDao类

package reflectAbstractFactory.user;

public class MysqlUserDao implements IUserDao {

    @Override
    public void insert(User user) {
        System.out.println("在mysql数据库的user表插入一条记录");
    }

    @Override
    public void getUser(int id) {
        System.out.println("在mysql数据库的user表根据ID获取一条记录");
    }

}

OracleUserDao类

package reflectAbstractFactory.user;

public class OracleUserDao implements IUserDao {

    @Override
    public void insert(User user) {
        System.out.println("在oracle数据库的user表插入一条记录");
    }

    @Override
    public void getUser(int id) {
        System.out.println("在oracle数据库的user表根据ID获取一条记录");
    }

}

User类

package reflectAbstractFactory.user;

public class User {
    private long id;
    private String name;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

 

package reflectAbstractFactory;
import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.util.Iterator;import java.util.Properties;
import reflectAbstractFactory.dept.IDeptDao;import reflectAbstractFactory.user.IUserDao;
public class ReflectSqlFactory {public String getDb() {Properties prop = new Properties();String db = "";try {// 读取属性文件a.propertiesInputStream in = new BufferedInputStream(new FileInputStream("E:/workspace/dahuasheji/sampleFactory/src/reflectAbstractFactory/sql.properties"));prop.load(in); // /加载属性列表Iterator<String> it = prop.stringPropertyNames().iterator();while (it.hasNext()) {String key = it.next();if("db".endsWith(key)){db = prop.getProperty("db");break;}}in.close();} catch (Exception e) {System.out.println(e);}return db;}public Class<?> getsqlFactory(String pge,String type) throws ClassNotFoundException{String className= "reflectAbstractFactory."+pge+getDb()+type+"Dao";System.out.println(className);Class<?> c1 = Class.forName(className);System.out.println(c1.getName());return c1;}public IUserDao createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException{Class<?> c = getsqlFactory("user.","User");IUserDao iu = (IUserDao)c.newInstance();return iu;};public IDeptDao createDept() throws ClassNotFoundException, InstantiationException, IllegalAccessException{Class<?> c = getsqlFactory("dept.","Dept");IDeptDao id = (IDeptDao)c.newInstance();return id;};}

 

以上是关于反射+配置文件+抽象工厂的主要内容,如果未能解决你的问题,请参考以下文章

设计模式——抽象工厂模式

就不能换DB吗? 抽象工厂模式

反射+抽象工厂

抽象工厂模式

工厂模式——抽象工厂模式(+反射)

抽象工厂模式(JAVA反射)