用户注册登录系统

用户注册登录系统

内容

  • 实体类
  • 层级关系
  • 静态代理
  • 工厂模式
  • 依赖注入

一、实体类

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

六、用户输入验证

1.Main当中实现功能选择

//功能选择
    private static int getOption(){
        int opt = -1;
        while (true){
            System.out.println("请选择1.注册\t2.登录\t3.退出");
            opt = scanner.nextInt();
            if(opt < 0 || opt > 3){
                System.out.println("输入错误,请重新输入");
                continue;
            }
            return opt;
        }
    }

2.启动应用选择功能

public static void main(String[] args) {
System.out.println("欢迎使用用户注册登录系统");
int version = checkVersion();//获取版本号
UserController controller = beanIoc(version);
boolean flag = true;
while(flag){
	switch (getOption()){
		case 1:
			User user = inputUser();
			controller.register(user);
			break;
		case 2:
			break;
		case 3:
			System.out.println("欢迎再次使用~!");
			flag = false;
		}
	}
}

3.用户信息输入

private static User inputUser(){
            User user = new User();
            System.out.print("请输入用户名(格式Xxxx6-18):");
            4.验证数据信息实例
            5.各个属性的验证方法:不全的部分自行补充完整
            user.setUsername(scanner.next());
            System.out.print("请输入密码(格式Xxxx6-18):");
            user.setPassword(scanner.next());
            System.out.print("请再次输入密码(格式Xxxx6-18):");
            user.setRepassword(scanner.next());
            System.out.print("请输入邮箱(格式xxxx@xxx.xxx):");
            user.setEmail(scanner.next());
            System.out.print("请输入生日(格式yyyy-MM-dd):");
            user.setDateStr(scanner.next());
            System.out.print("请输入性别(男/女):");
            user.setSexStr(scanner.next());
            return user;
        }

4.验证数据信息实例

package com.mine.usermanage.entity;
public class ValidateMessage {
    private String message;
    private int code;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
}

5.各个属性的验证方法:不全的部分自行补充完整

 private boolean checkedEmail(String email){
        //@不在第一位,.不在最后一位
        //@在.前面
        int index1 = email.indexOf("@");
        int index2 = email.indexOf(".");
        return true;
    }
    private Date checkedDate(String datestr){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(datestr);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }
    private boolean checkedUsernameAndPassword(String usernameAndPassword){
        //首字母
        if(usernameAndPassword.charAt(0) > 90 || usernameAndPassword.charAt(0) < 65){
            return false;
        }
        char[] cs = usernameAndPassword.toCharArray();
        //是否包含字母和数值,没有特殊符号
        boolean hasAlphabet = false;
        boolean hasNumber = false;
        for(char c : cs){
            if((c >= 65 && c <= 90) || (c >= 97 && c <= 122)|| ( c >= 48 && c <= 57 )){
                if((c >= 65 && c <= 90) || (c >= 97 && c <= 122)){
                    hasAlphabet = true;
                }
                if( c >= 48 && c <= 57 ){
                    hasNumber = true;
                }
            }else{
                return false;
            }
        }
        if(!hasAlphabet || !hasNumber){
            return false;
        }
        //长度
        if(usernameAndPassword.length() < 6 || usernameAndPassword.length() > 18){
            return false;
        }
        return true;
    }
    private boolean checkUsername(String username){
        if(this.checkedUsernameAndPassword(username)){
        //验证重复
            return true;
        }
        return false;
    }

6.实现service的注册方法

public boolean register(User user, ValidateMessage vm){
        System.out.println("验证数据合法性");
        if(!this.checkUsername(user.getUsername())){
            vm.setCode(500);
            vm.setMessage("用户名格式输入错误");
            return false;
        }else if(!this.checkedUsernameAndPassword(user.getPassword())){
            vm.setCode(500);
            vm.setMessage("密码格式输入错误");
            return false;
        }else if(!user.getPassword().equals(user.getRepassword())){
            vm.setCode(500);
            vm.setMessage("两次密码输入不一致");
            return false;
        }
        vm.setCode(200);
        vm.setMessage("输入验证通过");
        userService.register(user);
        return true;
    }
7.实现controller的注册功能
public void register(User user){
	ValidateMessage vm = new ValidateMessage();
	if(service.register(user,vm)){
		System.out.println(vm.getMessage());//成功
	}else{
		System.out.println(vm.getMessage());//验证失败
	}
}

七、单例模式

1.确保一个对象内存唯一

2.确保该对象,无法二次创建

模式:懒汉和饿汉模式

  • 懒汉:
package com.mine.usermanage.database;
public class DataBase {
	private static DataBase db;
	private DataBase(){
	}
	public synchronized static DataBase getInstance(){
		if(db == null){
			db = new DataBase();
		}
		return db;
	}
}
  • 饿汉:
package com.mine.usermanage.database;
public class DataBase {
	private static DataBase db = new DataBase();
	private DataBase(){
	}
	public synchronized static DataBase getInstance(){
		return db;
	}
}
  • 模拟内存数据库
package com.qf.usermanage.database;
import com.qf.usermanage.entity.User;
import java.util.HashMap;
import java.util.Map;
public class DataBase {
    private static DataBase db;
    private DataBase(){
    }
    public synchronized static DataBase getInstance(){
        if(db == null){
            db = new DataBase();
        }
        return db;
    }private Map<String, User> userTable = new HashMap<>();
    private Integer id = 0;
    public synchronized int getIdSequence(){
        id ++;
        return id;
    }
    //这是一个有风险的操作
    public Map<String, User> getUserTable() {
        return userTable;
    }
}

实现dao

接口改造

package com.mine.usermanage.dao;
import com.mine.usermanage.entity.User;
import java.util.List;
public interface UserDao {
	public int insertUser(User user);
	//加一个查询
	public List<User> selectUser(User user);
}

实现

package com.mine.usermanage.dao.impl;
import com.mine.usermanage.dao.UserDao;
import com.mine.usermanage.database.DataBase;
import com.mine.usermanage.entity.User;
import java.util.*;
public class UserDaoImpl1 implements UserDao {
    private DataBase db = DataBase.getInstance();
    @Override
    public int insertUser(User user) {
        Map<String,User> userTable = db.getUserTable();
        userTable.put(user.getUsername(),user);
        System.out.println("dao被调用,插入成功");
        return 1;
    }@Override
    public List<User> selectUser(User user) {
        List<User> ls = new ArrayList<>();
        //如果用户名进行筛选
        Map<String,User> userTable = db.getUserTable();
        if(userTable.containsKey(user.getUsername()) && user.getPassword() == null){
            User u = userTable.get(user.getUsername());
            ls.add(u);
            return ls;
        }
        //不考虑用户名的情况下
        Collection<User> users = userTable.values();
        Iterator<User> userIter = users.iterator();
        while(userIter.hasNext()){
            User u = userIter.next();//集合里的user元素
            if(user.getId()!=null){
                if(u.getId()!=user.getId()){
                    continue;
                }
            }
            if(user.getUsername()!=null && user.getPassword()!=null){
                if(!u.getUsername().equals(user.getUsername())){
                    continue;
                }else if(!u.getPassword().equals(user.getPassword())){
                    continue;
                }else{
                    ls.add(u);
                }
            }
        }
        return ls;
    }
}
  • service的代理类验证用户名重复
private boolean checkUsername(String username){
	if(this.checkedUsernameAndPassword(username)){
		//验证重复
		User user = new User();
		user.setUsername(username);
		List<User> us = this.userDaoProxy.selectUser(user);
		if(us.size() >= 1){
			return false;//用户名重复了
		}
		return true;//格式和用户名都通过
	}
	return false;//格式有没通过的
}
  • dao的代理类完成统一的id序列添加
package com.mine.usermanage.proxy;
import com.mine.usermanage.dao.UserDao;
import com.mine.usermanage.database.DataBase;
import com.mine.usermanage.entity.User;
import java.util.List;
public class UserDaoProxy {
    private UserDao dao;
    public UserDaoProxy(UserDao dao) {
        this.dao = dao;
    }
    public int insertUser(User user){
        System.out.println("生成ID序列");
        int id = DataBase.getInstance().getIdSequence();
        user.setId(id);
        return dao.insertUser(user);
    }
    public List<User> selectUser(User user) {
        return dao.selectUser(user);
    }
}

八、完成用户登入功能

posted @ 2023-03-22 21:33  DFshmily  阅读(17)  评论(0编辑  收藏  举报