用户注册登录系统
内容
- 实体类
- 层级关系
- 静态代理
- 工厂模式
- 依赖注入
一、实体类
包:com.mine.usermanage.entity
package com.mine.usermanage.entity;
import java.util.Date;
public class User {
private Integer id;
private String username;
private String password;
private String email;
private Date date;
private Integer sex;
public User(){
}
public User(Integer id,String username,String password,String email,Date date,Integer sex){
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.date = date;
this.sex = sex;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", date=" + date +
", sex=" + sex +
'}';
}
}
二、层级关系
controller:数据交互层
service:业务层
dao:数据层
-
结构
com.mine.xxxx
dao:UserDao
impl:UserDaoImpl1,2,3
service:UserService
impl:UserServiceImpl1,2,3
controller:UserController
Main
-
增强的功能性结构
com.mine.xxxx
proxy:UserDaoProxy , UserServiceProxy
factory:BeanFactory
三、静态代理
代理service
UserService.java
package com.mine.usermanage.service;
import com.mine.usermanage.entity.User;
//service接口
public interface UserService {
public void register(User user);
}
UserServiceImpl1.java
package com.mine.usermanage.service.impl;
import com.mine.usermanage.entity.User;
import com.mine.usermanage.proxy.UserDaoProxy;
import com.mine.usermanage.service.UserService;
public class UserServiceImpl1 implements UserService {
private UserDaoProxy dao;
public UserDaoProxy getDao(){
return dao;
}
public void setDao(UserDaoProxy dao){
this.dao = dao;
}
@Override
public void register(User user) {
}
}
代理工作
package com.mine.usermanage.proxy;
import com.mine.usermanage.entity.User;
import com.mine.usermanage.service.UserService;
public class UserServiceProxy {
private UserService userService;
public UserServiceProxy(UserService userService){
this.userService = userService;
}
public void register(User user){
System.out.println("验证数据合法性!");
userService.register(user);
}
}
代理模式使用
UserServiceProxy userServiceProxy = new UserServiceProxy(new UserServiceImpl1());
代理dao
package com.mine.usermanage.dao;
import com.mine.usermanage.entity.User;
//dao接口
public interface UserDao {
public int insertUser(User user);
}
package com.mine.usermanage.dao.impl;
import com.mine.usermanage.dao.UserDao;
import com.mine.usermanage.entity.User;
public class UserDaoImpl1 implements UserDao {
@Override
public int insertUser(User user) {
return 0;
}
}
dao的代理类
package com.mine.usermanage.proxy;
import com.mine.usermanage.dao.UserDao;
import com.mine.usermanage.entity.User;
public class UserDaoProxy {
private UserDao dao;
public UserDaoProxy(UserDao dao){
this.dao = dao;
}
public int insertUser(User user){
System.out.println("生成ID序列");
int id = -1;//生成好的id
user.setId(id);
return dao.insertUser(user);
}
}
代理模式使用
UserDaoProxy userDaoProxy = new UserDaoProxy(new UserDaoImpl1());
四、工厂模式
package com.mine.usermanage.factory;
import com.mine.usermanage.dao.UserDao;
import com.mine.usermanage.dao.impl.UserDaoImpl1;
import com.mine.usermanage.dao.impl.UserDaoImpl2;
import com.mine.usermanage.dao.impl.UserDaoImpl3;
import com.mine.usermanage.service.UserService;
import com.mine.usermanage.service.impl.UserServiceImpl1;
import com.mine.usermanage.service.impl.UserServiceImpl2;
import com.mine.usermanage.service.impl.UserServiceImpl3;
public interface BeanFactory {
//工厂模式
public static UserService getServiceImplFactory(int version){
UserService userService = null;
if(version == 1){
userService = new UserServiceImpl1();
}else if(version == 2){
userService = new UserServiceImpl2();
}else if(version == 3){
userService = new UserServiceImpl3();
}
return userService;
}
public static UserDao getDaoImplFactory(int version){
UserDao userDao = null;
if(version == 1){
userDao = new UserDaoImpl1();
} else if (version == 2) {
userDao = new UserDaoImpl2();
} else if (version == 3) {
userDao = new UserDaoImpl3();
}
return userDao;
}
}
完成bean的构建工作
UserService service = BeanFactory.getServiceImplFactory(version);
UserServiceProxy userServiceProxy = new UserServiceProxy(service);
UserDao dao = BeanFactory.getDaoImplFactory(version);
UserDaoProxy userDaoProxy = new UserDaoProxy(dao);
五、依赖注入
依赖关系:
1.controller依赖service;service依赖dao
2.把dao的代理注入service
注意:我们现在的service是代理类,而我们要注入的是实现类,实现类在代理类的类里面,所以要改造service的代理类,使其可以代理注入dao
注入逻辑:
UserServiceProxy.java
public void ioc(UserDaoProxy userDaoProxy){
//多态拆箱(向下转型)
if(userService instanceof UserServiceImpl1){
UserServiceImpl1 serviceImpl1 = (UserServiceImpl1) userService;
serviceImpl1.setDao(userDaoProxy);
}else if(userService instanceof UserServiceImpl2){
UserServiceImpl2 serviceImpl2 = (UserServiceImpl2) userService;
serviceImpl2.setDao(userDaoProxy);
} else if (userService instanceof UserServiceImpl3) {
UserServiceImpl3 serviceImpl3 = (UserServiceImpl3) userService;
serviceImpl3.setDao(userDaoProxy);
}
}
构造器重载注入
//构造重载
public UserServiceProxy(UserService userService,UserDaoProxy userDaoProxy){
this.ioc(userDaoProxy);//注入
this.userService = userService;
}
提供一个set方法,由调用者手动注入
//提供一个set方法,由调用者手动注入
private UserDaoProxy userDaoProxy;
public UserDaoProxy getUserDaoProxy(){
return userDaoProxy;
}
public void setUserDaoProxy(UserDaoProxy userDaoProxy){
this.ioc(userDaoProxy); //注入
this.userDaoProxy = userDaoProxy;
}
完成三层的最终注入:Main类当中
public class Main {
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎使用注册登录系统");
int version =checkVersion();//获取版本号
beanIoc(version);
}
//对所有层进行统一注入的工作
public static UserController beanIoc(int version){
//根据版本号构建依赖关系
UserDaoProxy userDaoProxy = new UserDaoProxy(BeanFactory.getDaoImplFactory(version));
UserServiceProxy userServiceProxy = new UserServiceProxy(BeanFactory.getServiceImplFactory(version),userDaoProxy);
userServiceProxy.setUserDaoProxy(userDaoProxy);
UserController userController = new UserController();
userController.setService(userServiceProxy);
return userController;
}
public static int checkVersion(){
int version = -1;
while (true){
System.out.print("请输入版本号(1-3):");
version = scanner.nextInt();
if(version >= 1 && version <= 3){
break;
}else {
System.out.println("输入版本号错误!");
}
}
return version;
}
}
标签:登录,用户注册,dao,系统,mine,usermanage,import,com,public
From: https://www.cnblogs.com/DFshmily/p/17245552.html