Skip to content

Commit

Permalink
design pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzaiplus committed May 24, 2020
1 parent 5165e42 commit 5971209
Show file tree
Hide file tree
Showing 41 changed files with 12,457 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/wangzaiplus/test/config/JedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
//@Configuration
public class JedisConfig {

@Value("${spring.redis.host}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//@Configuration
@Slf4j
public class RabbitConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//@Configuration
public class RedissonConfig {

@Value("${spring.redis.host}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@RequestMapping("/redisson")
public class RedissonController {

@Autowired
@Autowired(required = false)
private RedissonClient redissonClient;

@RequestMapping("testRedisson")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.wangzaiplus.test.controller;

import com.google.common.collect.Lists;
import com.wangzaiplus.test.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/search")
public class SearchController {

@GetMapping("users")
public List<User> users() {
User user = new User(1, "beigua", "beigua888");
User user2 = new User(2, "beigua2", "beigua888");
User user3 = new User(3, "beigua3", "beigua888");

return Lists.newArrayList(user, user2, user3);
}

}
14 changes: 14 additions & 0 deletions src/main/java/com/wangzaiplus/test/controller/ViewController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wangzaiplus.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class ViewController {

@GetMapping("home")
public String index() {
return "index";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wangzaiplus.test.designpattern.factory;

public class ALiPay implements IPay{

@Override
public boolean pay() {
// 支付相关逻辑...
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wangzaiplus.test.designpattern.factory;

public interface IPay {

boolean pay();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.wangzaiplus.test.designpattern.factory;

/**
* 支付方式
*/
public enum PayTypeEnum {

ALI_PAY("1", "支付宝支付"),
WE_CHAT_PAY("2", "微信支付"),
UNION_PAY("3", "银联支付"),
;

private String code;
private String msg;

PayTypeEnum(String code, String msg) {
this.code = code;
this.msg = msg;
}

public String getCode() {
return code;
}

public String getMsg() {
return msg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wangzaiplus.test.designpattern.factory;

public class UnionPay implements IPay{

@Override
public boolean pay() {
// 支付相关逻辑...
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.wangzaiplus.test.designpattern.factory;

public class WeChatPay implements IPay{

@Override
public boolean pay() {
// 支付相关逻辑...
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wangzaiplus.test.designpattern.factory.factorymethod;

import com.wangzaiplus.test.designpattern.factory.IPay;

public class AFactory implements FactoryMethod {

@Override
public IPay createPay(String payType) {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wangzaiplus.test.designpattern.factory.factorymethod;

import com.wangzaiplus.test.designpattern.factory.IPay;

public class BFactory implements FactoryMethod {

@Override
public IPay createPay(String payType) {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.wangzaiplus.test.designpattern.factory.factorymethod;

import com.wangzaiplus.test.designpattern.factory.IPay;

public interface FactoryMethod {

IPay createPay(String payType);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.wangzaiplus.test.designpattern.factory.factorymethod;

import com.wangzaiplus.test.designpattern.factory.IPay;

public class PayService {

public void pay(String payType) {
FactoryMethod aFactory = new AFactory();
IPay pay = aFactory.createPay(payType);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.wangzaiplus.test.designpattern.factory.simplefactory;

import com.wangzaiplus.test.designpattern.factory.*;

public class PayFactory {

public static IPay createPay(String payType) {
if (PayTypeEnum.ALI_PAY.getCode().equals(payType)) {
return new ALiPay();
}

if (PayTypeEnum.WE_CHAT_PAY.getCode().equals(payType)) {
return new WeChatPay();
}

if (PayTypeEnum.UNION_PAY.getCode().equals(payType)) {
return new UnionPay();
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.wangzaiplus.test.designpattern.factory.simplefactory;

import com.wangzaiplus.test.designpattern.factory.IPay;
import com.wangzaiplus.test.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;

public class PayService {

public void pay(String payType) {
if (StringUtils.isBlank(payType)) {
throw new ServiceException("支付方式异常");
}

boolean isPaySuccess = isPaySuccess(payType);
if (isPaySuccess) {
// 巴拉巴拉
} else {
// 巴拉巴拉
}
}

private boolean isPaySuccess(String payType) {
IPay iPay = PayFactory.createPay(payType);
if (null == iPay) {
throw new ServiceException("支付方式异常");
}

return iPay.pay();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.wangzaiplus.test.designpattern.strategy;

import org.springframework.stereotype.Component;

@Component
public class AliPayStrategy implements PayStrategy {

@Override
public void pay(String userId) {
System.out.println("AliPayStrategy userId: " + userId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.wangzaiplus.test.designpattern.strategy;

import com.wangzaiplus.test.designpattern.factory.PayTypeEnum;

public class Main {

public static void main(String[] args) {
PayContext payContext = new PayContext();
payContext.pay("3", "aaa");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.wangzaiplus.test.designpattern.strategy;

import com.wangzaiplus.test.designpattern.factory.PayTypeEnum;
import com.wangzaiplus.test.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//@Component
public class PayContext {

// @Autowired
// private Map<String, PayStrategy> map;

private static Map<String, PayStrategy> map2 = new ConcurrentHashMap<>();
static {
map2.put(PayTypeEnum.ALI_PAY.getCode(), new AliPayStrategy());
map2.put(PayTypeEnum.WE_CHAT_PAY.getCode(), new WxPayStrategy());
map2.put(PayTypeEnum.UNION_PAY.getCode(), new UnionPayStrategy());
}

public void pay(String payType, String userId) {
PayStrategy payStrategy = map2.get(payType);
if (null == payStrategy) {
throw new ServiceException("payType: " + payType + " not supported");
}

payStrategy.pay(userId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wangzaiplus.test.designpattern.strategy;

public interface PayStrategy {

void pay(String userId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.wangzaiplus.test.designpattern.strategy;

public class UnionPayStrategy implements PayStrategy {

@Override
public void pay(String userId) {
System.out.println("UnionPayStrategy userId: " + userId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.wangzaiplus.test.designpattern.strategy;

public class WxPayStrategy implements PayStrategy {

@Override
public void pay(String userId) {
System.out.println("WxPayStrategy userId: " + userId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.io.IOException;

@Component
//@Component
@Slf4j
public class SimpleMailConsumer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.io.IOException;

@Component
//@Component
public class LoginLogListener {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.io.IOException;

@Component
//@Component
public class MailListener {

@Autowired
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/wangzaiplus/test/task/ResendMsg.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import java.util.List;

@Component
//@Component
@Slf4j
public class ResendMsg {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/wangzaiplus/test/util/JedisUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Slf4j
public class JedisUtil {

@Autowired
@Autowired(required = false)
private JedisPool jedisPool;

private Jedis getJedis() {
Expand Down
Loading

0 comments on commit 5971209

Please sign in to comment.