Strategy mode with the application of java8

Strategy mode with the application of java8

In the process of writing business code in java, it is often encountered that a business has different realizations. Typically, such as user login, there is only one login entry, but it must match different login channels such as WeChat, NetEase, QQ, Alipay, and Sina Weibo. The specific implementation of various business scenarios is different, and some implementation details are very complicated. Unreasonable coding will result in poor code look and feel, affecting reading and maintenance. Today we use this scenario to discuss how to implement elegant coding in strategy mode with java8. The code is as follows:

1. Define the login request object, the specific parameters will not be discussed too much, here only the login channel code is identified
public class LogingRequest {
    
    

	private String loginTypeCode;

	/** 其他业务参数 **/

	public String getLoginTypeCode() {
    
    
		return loginTypeCode;
	}

	public void setLoginTypeCode(String loginTypeCode) {
    
    
		this.loginTypeCode = loginTypeCode;
	}

}
2. The general programmer may start coding according to business logic without thinking, the code should be like this
// 普通程序员的写法
	public void loginWithIfElse(LogingRequest logingRequest) {
    
    
		if ("wechat".equals(logingRequest.getLoginTypeCode())) {
    
    
			// 微信登录的逻辑,
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从微信渠道登录");
		} else if ("qq".equals(logingRequest.getLoginTypeCode())) {
    
    
			// qq登录的逻辑
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从QQ渠道登录");
		} else if ("netease".equals(logingRequest.getLoginTypeCode())) {
    
    
			// 网易登录的逻辑
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从网易渠道登录");
		} else if ("sina".equals(logingRequest.getLoginTypeCode())) {
    
    
			// 新浪微博登录的逻辑
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从新浪微博渠道登录");
		} else if ("alipay".equals(logingRequest.getLoginTypeCode())) {
    
    
			// 支付宝登录的逻辑
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从支付宝渠道登录");
		}
	}

The code written in this way is very long and difficult to read and understand. Considering that there are still very complicated logics in each login channel, there will be many if+esle, and the eyes will be able to see the flowers. At that time, it is estimated that not only others will maintain your code and scold your mother, you What I see is big.

3. Experienced programmers will extract a common interface. After implementing the interface, different login channels maintain and log in logic in their own class. The code is as follows
public interface LoginService {
    
    
	
	void login(LogingRequest logingRequest);

}

public class WeChatLoginService implements LoginService {
    
    

	public void login(LogingRequest logingRequest) {
    
    
		/**一大堆逻辑
		 * $$$$$&^&^&^&%&%&&^&^&^&
		 * erwerewrwrewr
		 * 2343243242
		 */
		System.out.println("从微信渠道登录");
	}

}

public class QQLoginService implements LoginService {
    
    

	public void login(LogingRequest logingRequest) {
    
    
		/**一大堆逻辑
		 * $$$$$&^&^&^&%&%&&^&^&^&
		 * erwerewrwrewr
		 * 2343243242
		 */
		System.out.println("从QQ渠道登录");
	}

}

public class NeteaseLoginService implements LoginService {
    
    

	public void login(LogingRequest logingRequest) {
    
    
		/**一大堆逻辑
		 * $$$$$&^&^&^&%&%&&^&^&^&
		 * erwerewrwrewr
		 * 2343243242
		 */
		System.out.println("从网易渠道登录");
	}

}

public class SinaLoginService implements LoginService {
    
    

	public void login(LogingRequest logingRequest) {
    
    
		/**一大堆逻辑
		 * $$$$$&^&^&^&%&%&&^&^&^&
		 * erwerewrwrewr
		 * 2343243242
		 */
		System.out.println("从新浪微博渠道登录");
	}

}

public class AlipayLoginService implements LoginService {
    
    

	public void login(LogingRequest logingRequest) {
    
    
		/**一大堆逻辑
		 * $$$$$&^&^&^&%&%&&^&^&^&
		 * erwerewrwrewr
		 * 2343243242
		 */
		System.out.println("从支付宝渠道登录");
	}

}


	// 把具体实现分接口实现
	public void loginWithInterface(LogingRequest logingRequest) {
    
    
		if ("wechat".equals(logingRequest.getLoginTypeCode())) {
    
    
			new WeChatLoginService().login(logingRequest);
		} else if ("qq".equals(logingRequest.getLoginTypeCode())) {
    
    
			new QQLoginService().login(logingRequest);
		} else if ("netease".equals(logingRequest.getLoginTypeCode())) {
    
    
			new NeteaseLoginService().login(logingRequest);
		} else if ("sina".equals(logingRequest.getLoginTypeCode())) {
    
    
			new SinaLoginService().login(logingRequest);
		} else if ("alipay".equals(logingRequest.getLoginTypeCode())) {
    
    
			new AlipayLoginService().login(logingRequest);
		}
	}
4. In addition to this, you still don’t like so many if+else, what to do? Next is the strategy mode I want to talk about, establish an enumeration to maintain the implementation of each interface, and implement strategy routing
public enum LoginTypeEnum {
    
    

	WECHAT("wechat", WeChatLoginService::new, "微信登录"), QQ("qq", QQLoginService::new, "qq登录"),
	NETEASE("netease", NeteaseLoginService::new, "网易登录"), SINA("sina", SinaLoginService::new, "微博登录"),
	ALIPAY("alipay", AlipayLoginService::new, "支付宝登录");

	private String code;

	private Supplier<LoginService> loginService;

	private String desc;

	public String getCode() {
    
    
		return code;
	}

	public void setCode(String code) {
    
    
		this.code = code;
	}

	public String getDesc() {
    
    
		return desc;
	}

	public void setDesc(String desc) {
    
    
		this.desc = desc;
	}

	private LoginTypeEnum(String code, Supplier<LoginService> loginService, String desc) {
    
    
		this.code = code;
		this.loginService = loginService;
		this.desc = desc;
	}

	public Supplier<LoginService> getLoginService() {
    
    
		return loginService;
	}

	public void setLoginService(Supplier<LoginService> loginService) {
    
    
		this.loginService = loginService;
	}

	public static LoginTypeEnum getByLoginTypeCode(String loginTypeCode) {
    
    
		return Stream.of(LoginTypeEnum.values()).filter(x -> x.getCode().equals(loginTypeCode)).findAny()
				.orElseThrow(() -> new RuntimeException("登录类型编码错误"));
	}

}

5. The final business category, three ways of writing
public class LoginServiceBiz {
    
    

	// 普通程序员的写法
	public void loginWithIfElse(LogingRequest logingRequest) {
    
    
		if ("wechat".equals(logingRequest.getLoginTypeCode())) {
    
    
			// 微信登录的逻辑,
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从微信渠道登录");
		} else if ("qq".equals(logingRequest.getLoginTypeCode())) {
    
    
			// qq登录的逻辑
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从QQ渠道登录");
		} else if ("netease".equals(logingRequest.getLoginTypeCode())) {
    
    
			// 网易登录的逻辑
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从网易渠道登录");
		} else if ("sina".equals(logingRequest.getLoginTypeCode())) {
    
    
			// 新浪微博登录的逻辑
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从新浪微博渠道登录");
		} else if ("alipay".equals(logingRequest.getLoginTypeCode())) {
    
    
			// 支付宝登录的逻辑
			/**
			 * 一大堆逻辑 $$$$$&^&^&^&%&%&&^&^&^& erwerewrwrewr 2343243242
			 */
			System.out.println("从支付宝渠道登录");
		}
	}

	// 把具体实现分接口实现
	public void loginWithInterface(LogingRequest logingRequest) {
    
    
		if ("wechat".equals(logingRequest.getLoginTypeCode())) {
    
    
			new WeChatLoginService().login(logingRequest);
		} else if ("qq".equals(logingRequest.getLoginTypeCode())) {
    
    
			new QQLoginService().login(logingRequest);
		} else if ("netease".equals(logingRequest.getLoginTypeCode())) {
    
    
			new NeteaseLoginService().login(logingRequest);
		} else if ("sina".equals(logingRequest.getLoginTypeCode())) {
    
    
			new SinaLoginService().login(logingRequest);
		} else if ("alipay".equals(logingRequest.getLoginTypeCode())) {
    
    
			new AlipayLoginService().login(logingRequest);
		}
	}

	// 采用java8配置策略模式实现登录自动路由到具体实现方法上
	public void loginWithJava8(LogingRequest logingRequest) {
    
    
		      		LoginTypeEnum.getByLoginTypeCode(logingRequest.getLoginTypeCode()).getLoginService().get().login(logingRequest);
	}
6. Test results
public // 测试类
class Test {
    
    

	public static void main(String[] args) {
    
    
		LoginServiceBiz loginServiceBiz = new LoginServiceBiz();
		LogingRequest logingRequest = new LogingRequest();
		logingRequest.setLoginTypeCode("wechat");
			System.out.println("=======用最普通的写法开始登录=========");
		loginServiceBiz.loginWithIfElse(logingRequest);
		System.out.println("=======用最普通的写法登录成功=========");
		System.out.println();
		System.out.println("=======用接口分离的写法开始登录=========");
		loginServiceBiz.loginWithInterface(logingRequest);
		System.out.println("=======用接口分离的写法登录成功=========");
		System.out.println();
		System.out.println("=======用策略模式java8的写法开始登录=========");
		loginServiceBiz.loginWithJava8(logingRequest);
		System.out.println("=======用策略模式java8写法登录成功=========");

	}

}

Results of the:

=======用最普通的写法开始登录=========
从微信渠道登录
=======用最普通的写法登录成功=========

=======用接口分离的写法开始登录=========
从微信渠道登录
=======用接口分离的写法登录成功=========

=======用策略模式java8的写法开始登录=========
从微信渠道登录
=======用策略模式java8写法登录成功=========

Guess you like

Origin blog.csdn.net/m0_53085735/article/details/112969996