Skip to content

Commit ba6eb4c

Browse files
committed
提交策略模式代码
1 parent b0c6131 commit ba6eb4c

File tree

9 files changed

+245
-4
lines changed

9 files changed

+245
-4
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<modules>
1212
<module>singleton</module>
13+
<module>strategy</module>
1314
</modules>
1415

1516
<dependencies>

singleton/src/main/java/os/dt/design/patterns/singleton/EnumPresident.java

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

33
/**
44
* 枚举式
5-
* 不仅线程安全,还可以反序列化
5+
* 不仅线程安全,还可以反序列化 (原因是枚举类没有构造方法)
66
* (有点别扭)
77
* Created by songgr on 2019/10/17.
88
*/

singleton/src/main/java/os/dt/design/patterns/singleton/InitializingOnDemandHolderPresident.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ private InitializingOnDemandHolderPresident(){
1313
}
1414

1515
// 可以做到延迟加载
16-
private static class HelperHolder {
16+
private static class SingletonHolder {
1717
private static final InitializingOnDemandHolderPresident INSTANCE =
1818
new InitializingOnDemandHolderPresident();
1919
}
2020

2121
public static InitializingOnDemandHolderPresident getInstance() {
22-
return HelperHolder.INSTANCE;
22+
return SingletonHolder.INSTANCE;
2323
}
2424

2525
}

singleton/src/test/java/os/dt/design/patterns/singleton/UnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void enumPresident() throws InterruptedException {
133133

134134
@After
135135
public void check() {
136-
assert objs.size() > 1;
136+
assert objs.size() == 4;
137137

138138
for (int i=0; i<objs.size()-1; i++){
139139
for (int j=i+1; j<objs.size(); j++){

strategy/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-design-patterns</artifactId>
7+
<groupId>os.dt.design.patterns</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>strategy</artifactId>
13+
14+
15+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package os.dt.design.patterns.strategy;
2+
3+
/**
4+
* Created by songgr on 2020/05/27.
5+
*/
6+
public class Class implements Comparable<Class> {
7+
// 班级编号
8+
int cid;
9+
// 语文平均成绩
10+
int chinese;
11+
// 数学平成绩
12+
int math;
13+
14+
public Class(int cid, int chinese, int math) {
15+
this.cid = cid;
16+
this.chinese = chinese;
17+
this.math = math;
18+
}
19+
20+
public int compareTo(Class o) {
21+
if (this.chinese > o.chinese) return 1;
22+
else if (this.chinese < o.chinese) return -1;
23+
else return 0;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "{cid:"+cid+",chinese:"+chinese+",math:"+math+"}";
29+
}
30+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package os.dt.design.patterns.strategy;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Created by songgr on 2020/05/27.
7+
*/
8+
public class SortApp {
9+
10+
public static void main(String[] args) {
11+
12+
Student[] arr = {new Student(1, 90, 100),
13+
new Student(2, 80, 89),
14+
new Student(3, 91, 90)
15+
};
16+
17+
// Class[] arr = {new Class(1, 90, 100),
18+
// new Class(2, 80, 89),
19+
// new Class(3, 91, 90)
20+
// };
21+
22+
23+
new Sorters<Student>().sort(arr, StudentStrategyEnum.MATH.strategy());
24+
System.out.println(Arrays.toString(arr));
25+
26+
Sorter sorter = new Sorter();
27+
sorter.sort_v1(arr);
28+
System.out.println(Arrays.toString(arr));
29+
30+
31+
32+
33+
// new Sorters<Class>().sort(arr, new Comparator<Class>() {
34+
// public int compare(Class o1, Class o2) {
35+
// if (o1.chinese > o2.chinese) return 1;
36+
// else if (o1.chinese < o2.chinese) return -1;
37+
// else return 0;
38+
// }
39+
// });
40+
//
41+
// System.out.println(Arrays.toString(arr));
42+
43+
44+
// new Sorters<Class>().sort(arr, new Comparator<Class>() {
45+
// public int compare(Class o1, Class o2) {
46+
// if (o1.math > o2.math) return 1;
47+
// else if (o1.math < o2.math) return -1;
48+
// else return 0;
49+
// }
50+
// });
51+
//
52+
// System.out.println(Arrays.toString(arr));
53+
54+
55+
List<Integer> list = new ArrayList<Integer>();
56+
list.add(3);list.add(1);list.add(2);
57+
Collections.sort(list, new Comparator<Integer>() {
58+
public int compare(Integer o1, Integer o2) {
59+
if (o1 > o2) return 1;
60+
else if (o1 < o2) return -1;
61+
else return 0;
62+
}
63+
});
64+
System.out.println(list);
65+
}
66+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package os.dt.design.patterns.strategy;
2+
3+
import java.util.Comparator;
4+
5+
/**
6+
* 排序者
7+
* Created by songgr on 2019/10/22.
8+
*/
9+
public class Sorter {
10+
11+
// 只能对student进行排序
12+
public void sort_v1(Student[] arr) {
13+
for (int i=0; i<arr.length-1; i++){
14+
int minPos = i;
15+
for (int j=i+1; j<arr.length; j++){
16+
minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
17+
}
18+
swap(arr, i, minPos);
19+
}
20+
}
21+
22+
void swap(Student[] arr, int i, int j) {
23+
Student temp = arr[i];
24+
arr[i] = arr[j];
25+
arr[j] = temp;
26+
}
27+
28+
// 能对所有的Comparable对象进行排序, 但是比较方法不能修改
29+
public void sort_v2(Comparable[] arr) {
30+
for (int i=0; i<arr.length-1; i++){
31+
int minPos = i;
32+
for (int j=i+1; j<arr.length; j++){
33+
minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
34+
}
35+
swap(arr, i, minPos);
36+
}
37+
}
38+
39+
40+
void swap(Comparable[] arr, int i, int j) {
41+
Comparable temp = arr[i];
42+
arr[i] = arr[j];
43+
arr[j] = temp;
44+
}
45+
46+
}
47+
48+
49+
class Sorters<T> {
50+
public void sort(T[] arr, Comparator<T> comparator) {
51+
for (int i=0; i<arr.length-1; i++){
52+
int minPos = i;
53+
for (int j=i+1; j<arr.length; j++){
54+
minPos = comparator.compare(arr[j],arr[minPos]) == -1 ? j : minPos;
55+
}
56+
swap(arr, i, minPos);
57+
}
58+
}
59+
60+
void swap(T[] arr, int i, int j) {
61+
T temp = arr[i];
62+
arr[i] = arr[j];
63+
arr[j] = temp;
64+
}
65+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package os.dt.design.patterns.strategy;
2+
3+
import java.util.Comparator;
4+
5+
/**
6+
* Created by songgr on 2020/05/27.
7+
*/
8+
public class Student implements Comparable<Student> {
9+
10+
// 学号
11+
int sid;
12+
// 语文成绩
13+
int chinese;
14+
// 数学成绩
15+
int math;
16+
17+
public Student(int sid, int chinese, int math) {
18+
this.sid = sid;
19+
this.chinese = chinese;
20+
this.math = math;
21+
}
22+
23+
public int compareTo(Student o) {
24+
if (this.chinese > o.chinese) return 1;
25+
else if (this.chinese < o.chinese) return -1;
26+
else return 0;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "{sid:"+sid+",chinese:"+chinese+",math:"+math+"}";
32+
}
33+
}
34+
35+
enum StudentStrategyEnum {
36+
CHINESE {
37+
@Override
38+
public Comparator strategy(){
39+
return new Comparator<Student>(){
40+
public int compare (Student o1, Student o2){
41+
if (o1.chinese > o2.chinese) return 1;
42+
else if (o1.chinese < o2.chinese) return -1;
43+
else return 0;
44+
}
45+
};
46+
}
47+
},
48+
49+
MATH {
50+
@Override
51+
public Comparator strategy(){
52+
return new Comparator<Student>(){
53+
public int compare (Student o1, Student o2){
54+
if (o1.math > o2.math) return 1;
55+
else if (o1.math < o2.math) return -1;
56+
else return 0;
57+
}
58+
};
59+
}
60+
};
61+
62+
public abstract Comparator strategy();
63+
}
64+

0 commit comments

Comments
 (0)