Skip to content

Commit c74c6c4

Browse files
committed
add pattern chain
1 parent 06f987b commit c74c6c4

26 files changed

+583
-37
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.duwei.annotation;
2+
3+
4+
import java.lang.annotation.*;
5+
6+
/**
7+
* 定义注解的关键字
8+
*/
9+
10+
@Documented
11+
@Retention(RetentionPolicy.CLASS)
12+
@Target(ElementType.METHOD)
13+
public @interface Define {
14+
15+
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.duwei.annotation;
2+
3+
import javax.annotation.processing.AbstractProcessor;
4+
import javax.annotation.processing.RoundEnvironment;
5+
import javax.annotation.processing.SupportedAnnotationTypes;
6+
import javax.annotation.processing.SupportedSourceVersion;
7+
import javax.lang.model.SourceVersion;
8+
import javax.lang.model.element.TypeElement;
9+
import java.util.Set;
10+
11+
//@SupportedAnnotationTypes("注解的名字")
12+
//@SupportedSourceVersion(SourceVersion.RELEASE_6)
13+
public class Processor extends AbstractProcessor {
14+
15+
16+
//唯一需要复写的抽象方法
17+
@Override
18+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
19+
return false;
20+
}
21+
22+
//使用上面的注解代替,推荐不用注解,因为混淆的问题
23+
@Override
24+
public Set<String> getSupportedAnnotationTypes() {
25+
return super.getSupportedAnnotationTypes();
26+
}
27+
28+
//使用上面的注解代替,推荐不用注解,因为混淆的问题
29+
@Override
30+
public SourceVersion getSupportedSourceVersion() {
31+
return super.getSupportedSourceVersion();
32+
33+
}
34+
35+
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.duwei.annotation;
2+
3+
4+
/**
5+
* 注解的使用
6+
*
7+
* 实现了自己的注解处理器,需要对其进行注册:
8+
*
9+
* 建立:resources/META-INFO/service/javax.annotation.processing.Processor文件
10+
* 在里面写上我们注解处理器的完整路径
11+
*
12+
* 或者,使用google的autoService,记得加依赖
13+
*
14+
*/
15+
public class Usage {
16+
}

src/com/duwei/designpattern/absfactory/AbstractFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public abstract class AbstractFactory {
44
public abstract Color getColor(String color);
5-
public abstract Shape getShape(String shape) ;
5+
public abstract Shape getShape(String shape);
66
}

src/com/duwei/designpattern/adapter/BinarySearch.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.duwei.designpattern.adapter;
22

3-
//二分查找类:适配者
3+
//二分查找类:适配者
44
class BinarySearch {
55
public int binarySearch(int array[], int key) {
66
int low = 0;
@@ -13,9 +13,9 @@ public int binarySearch(int array[], int key) {
1313
} else if (midVal > key) {
1414
high = mid - 1;
1515
} else {
16-
return 1; // 找到元素返回1
16+
return 1; // 找到元素返回1
1717
}
1818
}
19-
return -1; // 未找到元素返回-1
19+
return -1; // 未找到元素返回-1
2020
}
2121
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.duwei.designpattern.adapter;
22

3-
//操作适配器:适配器
3+
//操作适配器:适配器
44
class OperationAdapter implements ScoreOperation {
5-
private QuickSort sortObj; // 定义适配者QuickSort对象
6-
private BinarySearch searchObj; // 定义适配者BinarySearch对象
5+
private QuickSort sortObj; // 定义适配者QuickSort对象
6+
private BinarySearch searchObj; // 定义适配者BinarySearch对象
77

88
public OperationAdapter() {
99
sortObj = new QuickSort();
1010
searchObj = new BinarySearch();
1111
}
1212

1313
public int[] sort(int array[]) {
14-
return sortObj.quickSort(array); // 调用适配者类QuickSort的排序方法
14+
return sortObj.quickSort(array); // 调用适配者类QuickSort的排序方法
1515
}
1616

1717
public int search(int array[], int key) {
18-
return searchObj.binarySearch(array, key); // 调用适配者类BinarySearch的查找方法
18+
return searchObj.binarySearch(array, key); // 调用适配者类BinarySearch的查找方法
1919
}
2020
}

src/com/duwei/designpattern/adapter/QuickSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.duwei.designpattern.adapter;
22

3-
//¿ìËÙÅÅÐòÀࣺÊÊÅäÕß
3+
//快速排序类:适配者
44
class QuickSort {
55
public int[] quickSort(int array[]) {
66
sort(array, 0, array.length - 1);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.duwei.designpattern.adapter;
22

3-
//抽象成绩操作类:目标接口
3+
//抽象成绩操作类:目标接口
44
interface ScoreOperation {
5-
public int[] sort(int array[]); //成绩排序
5+
public int[] sort(int array[]); //成绩排序
66

7-
public int search(int array[],int key); //成绩查找
7+
public int search(int array[],int key); //成绩查找
88
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.duwei.designpattern.chain.chain1;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Client {
7+
8+
public static void main(String[] args) {
9+
//1.构建责任链
10+
List<Ratify> ratifies = new ArrayList<>();
11+
ratifies.add(new Leader());
12+
ratifies.add(new Manager());
13+
ratifies.add(new Header());
14+
//构建请求对象
15+
Request request = new Request.Builder().setDays(7).setName("aaa").setReason("生病").build();
16+
//组装处理
17+
RealChain realChain = new RealChain(ratifies, request, 0);
18+
realChain.proceed(request);
19+
20+
}
21+
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.duwei.designpattern.chain.chain1;
2+
3+
public class Header implements Ratify {
4+
5+
@Override
6+
public Result deal(Chain chain) {
7+
Request request = chain.request();
8+
System.out.println("DepartmentHeader=====>request:" + request.toString());
9+
if (request.getDays() > 7) {
10+
return new Result(false, "你这个完全没必要");
11+
}
12+
return new Result(true, "DepartmentHeader:不要着急,把事情处理完再回来!");
13+
}
14+
}

0 commit comments

Comments
 (0)