把手写的简单工厂变为一个通用工厂

如图工厂类里的方法雷同,可以优化

 

BeanFactory类修改为如下

package com.baizhiedu.basic;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class BeanFactory {
    private static Properties env = new Properties();

    static {
        try {
            //第一步 获取IO输入流
            InputStream inputStream = BeanFactory.class.getResourceAsStream("/applicationContext.properties");
            //第二步 文件内容 封装 Properties集合中 key = userService , value = com.baizhixx.UserServiceImpl
            env.load(inputStream);
            //关流
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 对象的创建方式:
     *    1.直接调用构造方法 创建对象 UserService userService = new UserServiceImpl();
     *    2.通过反射的形式 创建对象 解耦合
     *       Class clazz = Class.forName("com.baizhiedu.basic.UserServiceImpl");
     *       UserService userService = (UserService)clazz.newInstance();
     */
/*    public static UserService getUserService(){
        UserService userService = null;
        try {
            Class clazz = Class.forName(env.getProperty("userService"));
            //Object obj = clazz.newInstance();
            userService = (UserService)clazz.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return userService;
    }

    public static UserDAO getUserDAO(){
        UserDAO userDAO = null;
        try {
            Class clazz = Class.forName(env.getProperty("userDAO"));
            //Object obj = clazz.newInstance();
            userDAO = (UserDAO)clazz.newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return userDAO;
    }*/

    /**
     * 方法参数key 是小配置文件中的key [userDAO, userService] 
     */
    public static Object getBean(String key){
        Object ret = null;
        try{
            Class clazz = Class.forName(env.getProperty(key));
            ret = clazz.newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ret;
    }
    
}

  

 UserServiceImpl修改为如下

package com.baizhiedu.basic;

public class UserServiceImpl implements UserService {
    //private UserDAO userDAO = new UserDAOImpl();

//    private UserDAO userDAO = BeanFactory.getUserDAO();

    private UserDAO userDAO = (UserDAO) BeanFactory.getBean("userDAO");
    
    @Override
    public void register(User user) {
        userDAO.save(user);
    }

    @Override
    public void login(String name, String password) {
        userDAO.queryUserByNameAndPassword(name, password);
    }
}

  

测试类TestSpring修改为如下

package com.baizhiedu;

import com.baizhiedu.basic.BeanFactory;
import com.baizhiedu.basic.User;
import com.baizhiedu.basic.UserService;
import com.baizhiedu.basic.UserServiceImpl;
import org.junit.Test;

public class TestSpring {
    /**
     * 用于测试:工厂类进行解耦合的操作
     */
    @Test
    public void test1(){
        //UserServiceImplNew

        //UserService userService = new UserServiceImplNew();
//        UserService userService = new UserServiceImpl();

//        UserService userService = BeanFactory.getUserService();

        UserService userService = (UserService) BeanFactory.getBean("userService");

        userService.login("name", "suns");

        User user = new User("suns", "123456");
        userService.register(user);
    }
}

  

posted @ 2020-08-29 12:26  尐鱼儿  阅读(159)  评论(0编辑  收藏  举报