zoukankan      html  css  js  c++  java
  • 手写一个简单的工厂模式(二)

    图中这里仍然是耦合的

    所以首先在BeanFactory类中新增方法getUserDAO()

        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;
        }
    

    然后修改 UserServiceImpl 类获取userDAO对象的方式,改为 private UserDAO userDAO = BeanFactory.getUserDAO();

    package com.baizhiedu.basic;
    
    public class UserServiceImpl implements UserService {
        //private UserDAO userDAO = new UserDAOImpl();
    
        private UserDAO userDAO = BeanFactory.getUserDAO();
    
        @Override
        public void register(User user) {
            userDAO.save(user);
        }
    
        @Override
        public void login(String name, String password) {
            userDAO.queryUserByNameAndPassword(name, password);
        }
    }
    

      

     修改applicationContext.properties文件,新增 userDao = com.baizhiedu.basic.UserDAOImpl

    userService = com.baizhiedu.basic.UserServiceImpl
    userDAO = com.baizhiedu.basic.UserDAOImpl

      

    [ 尐鱼儿的QQ群:726994578 ] --- [ https://github.com/godmaybelieve ]
  • 相关阅读:
    计算机编程基础
    css3 压缩及验证工具
    BFC
    【原创】bootstrap框架的学习 第五课
    曼珠沙华
    仓央嘉措
    waiting for spring......
    一天
    21-chttp连接池该取多大
    守护线程会不会执行finally?默认情况new thread怎么样确定守护状态?
  • 原文地址:https://www.cnblogs.com/yuyu666/p/13581608.html
Copyright © 2011-2022 走看看