文章目录
创建design-demo项目创建UserController创建UserService创建UserServiceImpl创建LoginFunc创建WbLoginFunc创建ZfbLoginFunc创建AbstractLoginProcessor创建ThirdPartLogin
创建design-demo项目
项目代码:https://gitee.com/java_wxid/java_wxid/tree/master/demo/design-demo
项目结构如下(示例):
创建UserController
代码如下(示例):
package com.example.designdemo.controller;import com.example.designdemo.pojo.TicketParam;import com.example.designdemo.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;/** * @Author: zhiwei Liao * @Date: 2022/9/25 19:26 * @Description: */@RestControllerpublic class UserController { @Autowired private UserService userService; @PostMapping("/login") public Boolean login(@RequestParam String name, @RequestParam String pwd, @RequestParam String type) { return userService.login(name, pwd, type); }}
创建UserService
代码如下(示例):
package com.example.designdemo.service;import com.example.designdemo.pojo.TicketParam;import java.util.List;/** * @Author: zhiwei Liao * @Date: 2022/9/25 9:28 * @Description: */public interface UserService { Boolean login(String name, String pwd, String type);}
创建UserServiceImpl
代码如下(示例):
package com.example.designdemo.service.impl;import com.example.designdemo.handler.SuggestRequirementHandlerProcess;import com.example.designdemo.implementor.LoginFunc;import com.example.designdemo.implementor.WbLoginFunc;import com.example.designdemo.implementor.abstractlogin.AbstractLoginProcessor;import com.example.designdemo.implementor.abstractlogin.ThirdPartLogin;import com.example.designdemo.pojo.TicketParam;import com.example.designdemo.pojo.UserInfo;import com.example.designdemo.service.UserService;import com.example.designdemo.ticket.builder.CompanyTicketBuilder;import com.example.designdemo.ticket.builder.PersonalTicketBuilder;import com.example.designdemo.ticket.builder.TicketBuilder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.ArrayList;import java.util.List;@Servicepublic class UserServiceImpl implements UserService { public Boolean login(String name, String pwd, String type) { // 这部分我就不进行封装了,直接在 service里做了。小伙伴可以自行实现封装,类似享元。 // 这部分是桥接模式的实现。 if(type.equals("wb")) { LoginFunc func = new WbLoginFunc(); AbstractLoginProcessor processor = new ThirdPartLogin(func); return processor.loginExecute(name, pwd, type); } return true; }}
创建LoginFunc
代码如下(示例):
package com.example.designdemo.implementor;public interface LoginFunc { public boolean login(String name, String pwd, String type);}
创建WbLoginFunc
代码如下(示例):
package com.example.designdemo.implementor;public class WbLoginFunc implements LoginFunc{ @Override public boolean login(String name, String pwd, String type) { // 进行第三方账号的校验流程。 System.out.println("Wb 验证通过,可以登录。"); return true; }}
创建ZfbLoginFunc
代码如下(示例):
package com.example.designdemo.implementor;public class ZfbLoginFunc implements LoginFunc{ @Override public boolean login(String name, String pwd, String type) { // 进行第三方账号的校验流程。 System.out.println("Zfb 验证通过,可以登录。"); return true; }}
创建AbstractLoginProcessor
代码如下(示例):
package com.example.designdemo.implementor.abstractlogin;import com.example.designdemo.implementor.LoginFunc;public abstract class AbstractLoginProcessor { protected LoginFunc loginFunc; public AbstractLoginProcessor(LoginFunc loginFunc) { this.loginFunc = loginFunc; } public abstract boolean loginExecute(String name, String pwd, String type);}
创建ThirdPartLogin
代码如下(示例):
package com.example.designdemo.implementor.abstractlogin;import com.example.designdemo.implementor.LoginFunc;public class ThirdPartLogin extends AbstractLoginProcessor{ public ThirdPartLogin(LoginFunc loginFunc) { super(loginFunc); } @Override public boolean loginExecute(String name, String pwd, String type) { return super.loginFunc.login(name, pwd, type); }}