手写一个简单的工厂模式(一)
项目结构
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
完成项目后,只需修改applicationContext.properties文件里的值,就可以切换实现类。(例如从实现类UserServiceImpl,切换到UserServiceImplNew,只需按下图操作)
改变前的配置
改变前的输出
query User name = name password = suns
insert into user = User{name='suns', password='123456'}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
改变后的配置
改变后的输出
UserServiceImplNew.login
UserServiceImplNew.register
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
User
package com.baizhiedu.basic; public class User { private String name; private String password; public User(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
UserService
package com.baizhiedu.basic; public interface UserService { public void register(User user); public void login(String name, String password); }
UserServiceImpl
package com.baizhiedu.basic; public class UserServiceImpl implements UserService { private UserDAO userDAO = new UserDAOImpl(); @Override public void register(User user) { userDAO.save(user); } @Override public void login(String name, String password) { userDAO.queryUserByNameAndPassword(name, password); } }
UserDAO
package com.baizhiedu.basic; public interface UserDAO { public void save(User user); public void queryUserByNameAndPassword(String name, String password); }
UserDAOImpl
package com.baizhiedu.basic; public class UserDAOImpl implements UserDAO { @Override public void save(User user) { System.out.println("insert into user = " + user); } @Override public void queryUserByNameAndPassword(String name, String password) { System.out.println("query User name = " + name + " password = " + password); } }
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(){ // return new UserServiceImpl(); 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; } }
UserServiceImplNew
package com.baizhiedu.basic; public class UserServiceImplNew implements UserService { @Override public void register(User user) { System.out.println("UserServiceImplNew.register"); } @Override public void login(String name, String password) { System.out.println("UserServiceImplNew.login"); } }
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.login("name", "suns"); User user = new User("suns", "123456"); userService.register(user); } }
applicationContext.properties
第一种
userService = com.baizhiedu.basic.UserServiceImpl
第二种
userService = com.baizhiedu.basic.UserServiceImplNew