Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78df613

Browse files
committed
add chain gof example
1 parent 7fbacb2 commit 78df613

File tree

9 files changed

+295
-10
lines changed

9 files changed

+295
-10
lines changed

‎GOF/gof-example/pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
<artifactId>spring-boot-starter-test</artifactId>
2828
<scope>test</scope>
2929
</dependency>
30+
31+
<dependency>
32+
<groupId>com.alibaba</groupId>
33+
<artifactId>fastjson</artifactId>
34+
<version>1.2.70</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<optional>true</optional>
41+
<version>1.18.12</version>
42+
<scope>provided</scope>
43+
</dependency>
3044
</dependencies>
3145

3246
<build>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.ipipman.gof.example.chain;
2+
3+
/**
4+
* Created by ipipman on 2021年4月22日.
5+
*
6+
* @version V1.0
7+
* @Package com.ipipman.gof.example.chain
8+
* @Description: (用一句话描述该文件做什么)
9+
* @date 2021年4月22日 10:11 上午
10+
*/
11+
public class AuthInfo {
12+
13+
private String code;
14+
private String info = "";
15+
16+
public AuthInfo(String code, String... infos) {
17+
this.code = code;
18+
for (String str : infos) {
19+
this.info = this.info.concat(str);
20+
}
21+
}
22+
23+
public String getCode() {
24+
return code;
25+
}
26+
27+
public void setCode(String code) {
28+
this.code = code;
29+
}
30+
31+
public String getInfo() {
32+
return info;
33+
}
34+
35+
public void setInfo(String info) {
36+
this.info = info;
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.ipipman.gof.example.chain;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
6+
/**
7+
* Created by ipipman on 2021年4月22日.
8+
*
9+
* @version V1.0
10+
* @Package com.ipipman.gof.example.chain
11+
* @Description: (用一句话描述该文件做什么)
12+
* @date 2021年4月22日 10:03 上午
13+
*/
14+
public abstract class AuthLink {
15+
16+
// 时间格式化
17+
protected SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
18+
// 级别人员ID
19+
protected String levelUserId;
20+
// 级别人员姓名
21+
protected String levelUserName;
22+
// 下一个责任链
23+
private AuthLink next;
24+
25+
// 构造方法
26+
public AuthLink(String levelUserId, String levelUserName) {
27+
this.levelUserId = levelUserId;
28+
this.levelUserName = levelUserName;
29+
}
30+
31+
// 返回下一个责任链条
32+
public AuthLink next() {
33+
return this.next;
34+
}
35+
36+
// 加入下一个责任链
37+
public AuthLink appendNext(AuthLink next) {
38+
this.next = next;
39+
return this;
40+
}
41+
42+
// 审核
43+
public abstract AuthInfo doAuth(String uId, String orderId, Date authDate);
44+
45+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.ipipman.gof.example.chain;
2+
3+
import java.util.Date;
4+
import java.util.Map;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
/**
8+
* Created by ipipman on 2021年4月22日.
9+
*
10+
* @version V1.0
11+
* @Package com.ipipman.gof.example.chain
12+
* @Description: (用一句话描述该文件做什么)
13+
* @date 2021年4月22日 10:18 上午
14+
*/
15+
public class AuthService {
16+
17+
private static final Map<String, Date> authMap = new ConcurrentHashMap<>();
18+
19+
public static Date queryAuthInfo(String uId, String orderId) {
20+
return authMap.get(uId.concat(orderId));
21+
}
22+
23+
public static void auth(String uId, String orderId) {
24+
authMap.put(uId.concat(orderId), new Date());
25+
}
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.ipipman.gof.example.chain;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.text.ParseException;
8+
import java.util.Date;
9+
import java.util.logging.Logger;
10+
11+
/**
12+
* Created by ipipman on 2021年4月22日.
13+
*
14+
* @version V1.0
15+
* @Package com.ipipman.gof.example.chain
16+
* @Description: (用一句话描述该文件做什么)
17+
* @date 2021年4月22日 10:34 上午
18+
*/
19+
@Slf4j
20+
public class ChainTest {
21+
22+
23+
public static void main(String[] args) throws ParseException {
24+
AuthLink authLink = new Level3AuthLink("1000013", "王工")
25+
.appendNext(new Level2AuthLink("1000012", "张经理")
26+
.appendNext(new Level1AuthLink("1000011", "段总")));
27+
28+
log.info("测试结果:{}", JSON.toJSONString(authLink.doAuth("小傅哥", "1000998004813441", new Date())));
29+
30+
// 模拟三级负责人审批
31+
AuthService.auth("1000013", "1000998004813441");
32+
log.info("测试结果:{}", "模拟三级负责人审批,王工");
33+
log.info("测试结果:{}", JSON.toJSONString(authLink.doAuth("小傅哥", "1000998004813441", new Date())));
34+
35+
// 模拟二级负责人审批
36+
AuthService.auth("1000012", "1000998004813441");
37+
log.info("测试结果:{}", "模拟二级负责人审批,张经理");
38+
log.info("测试结果:{}", JSON.toJSONString(authLink.doAuth("小傅哥", "1000998004813441", new Date())));
39+
40+
// 模拟一级负责人审批
41+
AuthService.auth("1000011", "1000998004813441");
42+
log.info("测试结果:{}", "模拟一级负责人审批,段总");
43+
log.info("测试结果:{}", JSON.toJSONString(authLink.doAuth("小傅哥", "1000998004813441", new Date())));
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.ipipman.gof.example.chain;
2+
3+
import java.util.Date;
4+
5+
/**
6+
* Created by ipipman on 2021年4月22日.
7+
*
8+
* @version V1.0
9+
* @Package com.ipipman.gof.example.chain
10+
* @Description: (用一句话描述该文件做什么)
11+
* @date 2021年4月22日 10:15 上午
12+
*/
13+
public class Level1AuthLink extends AuthLink {
14+
15+
// 初始化父类
16+
public Level1AuthLink(String levelUserId, String levelUserName) {
17+
super(levelUserId, levelUserName);
18+
}
19+
20+
@Override
21+
public AuthInfo doAuth(String uId, String orderId, Date authDate) {
22+
Date date = AuthService.queryAuthInfo(levelUserId, orderId);
23+
if (date == null) {
24+
return new AuthInfo("0001", "单号:", orderId, " 状态:待一级审批负责人 ", levelUserName);
25+
}
26+
AuthLink next = super.next();
27+
if (null == next) {
28+
return new AuthInfo("0000", "单号:", orderId, " 状态:一级审批完成负责人", " 时间:", sdf.format(date), " 审批人:", levelUserName);
29+
}
30+
return next.doAuth(uId, orderId, authDate);
31+
}
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ipipman.gof.example.chain;
2+
3+
import java.text.ParseException;
4+
import java.util.Date;
5+
6+
/**
7+
* Created by ipipman on 2021年4月22日.
8+
*
9+
* @version V1.0
10+
* @Package com.ipipman.gof.example.chain
11+
* @Description: (用一句话描述该文件做什么)
12+
* @date 2021年4月22日 10:15 上午
13+
*/
14+
public class Level2AuthLink extends AuthLink {
15+
16+
private final Date beginDate = sdf.parse("2020年06月11日 00:00:00");
17+
private final Date endDate = sdf.parse("2020年06月20日 23:59:59");
18+
19+
// 初始化父类
20+
public Level2AuthLink(String levelUserId, String levelUserName) throws ParseException {
21+
super(levelUserId, levelUserName);
22+
}
23+
24+
@Override
25+
public AuthInfo doAuth(String uId, String orderId, Date authDate) {
26+
Date date = AuthService.queryAuthInfo(levelUserId, orderId);
27+
if (date == null) {
28+
return new AuthInfo("0001", "单号:", orderId, " 状态:待二级审批负责人 ", levelUserName);
29+
}
30+
AuthLink next = super.next();
31+
if (null == next) {
32+
return new AuthInfo("0000", "单号:", orderId, " 状态:二级审批完成负责人", " 时间:", sdf.format(date), " 审批人:", levelUserName);
33+
}
34+
// if (authDate.before(beginDate) || authDate.after(endDate)){
35+
// return new AuthInfo("0000", "单号:", orderId, " 状态:二级审批完成负责人", " 时间:", sdf.format(date), " 审批人:", levelUserName);
36+
// }
37+
return next.doAuth(uId, orderId, authDate);
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ipipman.gof.example.chain;
2+
3+
import java.text.ParseException;
4+
import java.util.Date;
5+
6+
/**
7+
* Created by ipipman on 2021年4月22日.
8+
*
9+
* @version V1.0
10+
* @Package com.ipipman.gof.example.chain
11+
* @Description: (用一句话描述该文件做什么)
12+
* @date 2021年4月22日 10:15 上午
13+
*/
14+
public class Level3AuthLink extends AuthLink {
15+
16+
private final Date beginDate = sdf.parse("2020年06月01日 00:00:00");
17+
private final Date endDate = sdf.parse("2020年06月25日 23:59:59");
18+
19+
// 初始化父类
20+
public Level3AuthLink(String levelUserId, String levelUserName) throws ParseException {
21+
super(levelUserId, levelUserName);
22+
}
23+
24+
@Override
25+
public AuthInfo doAuth(String uId, String orderId, Date authDate) {
26+
Date date = AuthService.queryAuthInfo(levelUserId, orderId);
27+
if (date == null) {
28+
return new AuthInfo("0001", "单号:", orderId, " 状态:待三级审批负责人 ", levelUserName);
29+
}
30+
AuthLink next = super.next();
31+
if (null == next) {
32+
return new AuthInfo("0000", "单号:", orderId, " 状态:三级审批完成负责人", " 时间:", sdf.format(date), " 审批人:", levelUserName);
33+
}
34+
// if (authDate.before(beginDate) || authDate.after(endDate)){
35+
// return new AuthInfo("0000", "单号:", orderId, " 状态:三级审批完成负责人", " 时间:", sdf.format(date), " 审批人:", levelUserName);
36+
// }
37+
return next.doAuth(uId, orderId, authDate);
38+
}
39+
}

‎GOF/gof-example/src/main/java/com/ipipman/gof/example/solution/ArraySearchSolution.java‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class ArraySearchSolution {
1414

1515
// 统计一个数字在排序数组中出现的次数
1616
public static int search(int[] nums, int target) {
17+
1718
int l = 0, r = nums.length - 1;
18-
int count = 0;
1919
while (l < r) {
2020
int mid = l + (r - l) / 2;
2121
if (nums[mid] >= target) {
@@ -24,6 +24,7 @@ public static int search(int[] nums, int target) {
2424
l = mid + 1;
2525
}
2626
}
27+
int count = 0;
2728
while (l < nums.length && nums[l++] == target) {
2829
count++;
2930
}
@@ -34,26 +35,32 @@ public static void main(String[] args) {
3435
int[] nums = new int[]{5, 7, 7, 8, 8, 10};
3536
Arrays.sort(nums);
3637
System.out.println(search(nums, 5));
38+
39+
40+
System.out.println(longestPalindrome("abcbc"));
3741
}
3842

39-
public String longestPalindrome(String s) {
40-
if (s == null || s.length() < 1) return "";
43+
public static String longestPalindrome(String s) {
44+
if(s == null || s.length() < 1){
45+
return "";
46+
}
4147
int start = 0, end = 0;
4248
for(int i = 0; i < s.length(); i++){
43-
int len1 = toLongestPalindromeLen(s, i, i);
44-
int len2 = toLongestPalindromeLen(s, i, i+1);
45-
int len = Math.max(len1, len2);
46-
if(len > (end - start)){
47-
start = i - (len - 1) / 2;
48-
end = i + len / 2;
49+
int lLen = toLongestPalindromeLen(s, i, i);
50+
int rLen = toLongestPalindromeLen(s, i, i+1);
51+
int maxLen = Math.max(lLen, rLen);
52+
if (maxLen > (end - start)){
53+
start = i - (maxLen - 1) / 2;
54+
end = i + maxLen / 2;
4955
}
5056
}
5157
return s.substring(start, end + 1);
5258
}
5359

54-
public int toLongestPalindromeLen(String s, int left, int right){
60+
public staticint toLongestPalindromeLen(String s, int left, int right){
5561
int l = left, r = right;
5662
while((l >= 0 && r < s.length()) && (s.charAt(l) == s.charAt(r))){
63+
5764
l--;
5865
r++;
5966
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /