Skip to content

Commit 57403ee

Browse files
committed
设计模式V1.0完结
1 parent 091bb65 commit 57403ee

File tree

23 files changed

+819
-326
lines changed

23 files changed

+819
-326
lines changed

.idea/workspace.xml

Lines changed: 349 additions & 319 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mashibing.dp.TemplateMethod;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
F f = new C1();
6+
f.m();
7+
}
8+
9+
}
10+
11+
abstract class F {
12+
public void m() {
13+
op1();
14+
op2();
15+
}
16+
17+
abstract void op1();
18+
abstract void op2();
19+
}
20+
21+
class C1 extends F {
22+
23+
@Override
24+
void op1() {
25+
System.out.println("op1");
26+
}
27+
28+
@Override
29+
void op2() {
30+
System.out.println("op2");
31+
}
32+
}

src/main/java/com/mashibing/dp/Test.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
import java.awt.event.WindowAdapter;
55
import java.awt.event.WindowEvent;
66
import java.awt.event.WindowListener;
7+
import java.util.concurrent.locks.LockSupport;
78

89
public class Test {
9-
public static void main(String[] args) {
10-
Frame f = new Frame();
11-
f.addWindowListener(new WindowAdapter() {
12-
@Override
13-
public void windowClosing(WindowEvent e) {
14-
super.windowClosing(e);
15-
}
10+
public static void main(String[] args) throws Exception {
11+
Thread t = new Thread(()->{
12+
System.out.println("start");
13+
LockSupport.park(); //一直wait
14+
System.out.println("continue");
1615
});
16+
t.start();
17+
18+
Thread.sleep(1000);
19+
LockSupport.unpark(t); //t线程解除wait态
1720
}
1821
}
1922

src/main/java/com/mashibing/dp/command/Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.mashibing.dp.command;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
public class Main {
47
public static void main(String[] args) {
58
Content c = new Content();
@@ -16,6 +19,23 @@ public static void main(String[] args) {
1619
deleteCommand.doit();
1720
deleteCommand.undo();
1821

22+
List<Command> commands = new ArrayList<>();
23+
commands.add(new InsertCommand(c));
24+
commands.add(new CopyCommand(c));
25+
commands.add(new DeleteCommand(c));
26+
27+
for(Command comm : commands) {
28+
comm.doit();
29+
}
30+
31+
32+
System.out.println(c.msg);
33+
34+
for(int i= commands.size()-1; i>=0; i--) {
35+
commands.get(i).undo();
36+
}
37+
38+
1939
System.out.println(c.msg);
2040
}
2141
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
脚本语言解释器
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
记录对象的某个瞬间
2+
类似照片
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mashibing.dp.prototype.v1;
2+
3+
/**
4+
* 浅克隆
5+
*/
6+
7+
public class Test {
8+
public static void main(String[] args) throws Exception {
9+
Person p1 = new Person();
10+
Person p2 = (Person)p1.clone();
11+
System.out.println(p2.age + " " + p2.score);
12+
System.out.println(p2.loc);
13+
14+
System.out.println(p1.loc == p2.loc);
15+
p1.loc.street = "sh";
16+
System.out.println(p2.loc);
17+
18+
}
19+
}
20+
21+
class Person implements Cloneable {
22+
int age = 8;
23+
int score = 100;
24+
25+
Location loc = new Location("bj", 22);
26+
@Override
27+
public Object clone() throws CloneNotSupportedException {
28+
return super.clone();
29+
}
30+
}
31+
32+
class Location {
33+
String street;
34+
int roomNo;
35+
36+
@Override
37+
public String toString() {
38+
return "Location{" +
39+
"street='" + street + '\'' +
40+
", roomNo=" + roomNo +
41+
'}';
42+
}
43+
44+
public Location(String street, int roomNo) {
45+
this.street = street;
46+
this.roomNo = roomNo;
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.mashibing.dp.prototype.v2;
2+
3+
/**
4+
* 深克隆的处理
5+
*/
6+
public class Test {
7+
public static void main(String[] args) throws Exception {
8+
Person p1 = new Person();
9+
Person p2 = (Person)p1.clone();
10+
System.out.println(p2.age + " " + p2.score);
11+
System.out.println(p2.loc);
12+
13+
System.out.println(p1.loc == p2.loc);
14+
p1.loc.street = "sh";
15+
System.out.println(p2.loc);
16+
17+
18+
19+
}
20+
}
21+
22+
class Person implements Cloneable {
23+
int age = 8;
24+
int score = 100;
25+
26+
Location loc = new Location("bj", 22);
27+
@Override
28+
public Object clone() throws CloneNotSupportedException {
29+
Person p = (Person)super.clone();
30+
p.loc = (Location)loc.clone();
31+
return p;
32+
}
33+
}
34+
35+
class Location implements Cloneable {
36+
String street;
37+
int roomNo;
38+
39+
@Override
40+
public String toString() {
41+
return "Location{" +
42+
"street='" + street + '\'' +
43+
", roomNo=" + roomNo +
44+
'}';
45+
}
46+
47+
public Location(String street, int roomNo) {
48+
this.street = street;
49+
this.roomNo = roomNo;
50+
}
51+
52+
@Override
53+
public Object clone() throws CloneNotSupportedException {
54+
return super.clone();
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.mashibing.dp.prototype.v3;
2+
3+
/**
4+
* String需要进一步深克隆吗?
5+
*/
6+
public class Test {
7+
public static void main(String[] args) throws Exception {
8+
Person p1 = new Person();
9+
Person p2 = (Person)p1.clone();
10+
System.out.println(p2.age + " " + p2.score);
11+
System.out.println(p2.loc);
12+
13+
System.out.println(p1.loc == p2.loc);
14+
p1.loc.street = "sh";
15+
System.out.println(p2.loc);
16+
17+
p1.loc.street.replace("sh", "sz");
18+
System.out.println(p2.loc.street);
19+
}
20+
}
21+
22+
class Person implements Cloneable {
23+
int age = 8;
24+
int score = 100;
25+
26+
Location loc = new Location("bj", 22);
27+
@Override
28+
public Object clone() throws CloneNotSupportedException {
29+
Person p = (Person)super.clone();
30+
p.loc = (Location)loc.clone();
31+
return p;
32+
}
33+
}
34+
35+
class Location implements Cloneable {
36+
String street;
37+
int roomNo;
38+
39+
@Override
40+
public String toString() {
41+
return "Location{" +
42+
"street='" + street + '\'' +
43+
", roomNo=" + roomNo +
44+
'}';
45+
}
46+
47+
public Location(String street, int roomNo) {
48+
this.street = street;
49+
this.roomNo = roomNo;
50+
}
51+
52+
@Override
53+
public Object clone() throws CloneNotSupportedException {
54+
return super.clone();
55+
}
56+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.mashibing.dp.prototype.v4;
2+
3+
/**
4+
* String需要进一步深克隆吗?
5+
*/
6+
public class Test {
7+
public static void main(String[] args) throws Exception {
8+
Person p1 = new Person();
9+
Person p2 = (Person)p1.clone();
10+
System.out.println("p1.loc == p2.loc? " + (p1.loc == p2.loc));
11+
12+
p1.loc.street.reverse();
13+
System.out.println(p2.loc.street);
14+
}
15+
}
16+
17+
class Person implements Cloneable {
18+
int age = 8;
19+
int score = 100;
20+
21+
Location loc = new Location(new StringBuilder("bj"), 22);
22+
@Override
23+
public Object clone() throws CloneNotSupportedException {
24+
Person p = (Person)super.clone();
25+
p.loc = (Location)loc.clone();
26+
return p;
27+
}
28+
}
29+
30+
class Location implements Cloneable {
31+
StringBuilder street;
32+
int roomNo;
33+
34+
@Override
35+
public String toString() {
36+
return "Location{" +
37+
"street='" + street + '\'' +
38+
", roomNo=" + roomNo +
39+
'}';
40+
}
41+
42+
public Location(StringBuilder street, int roomNo) {
43+
this.street = street;
44+
this.roomNo = roomNo;
45+
}
46+
47+
@Override
48+
public Object clone() throws CloneNotSupportedException {
49+
return super.clone();
50+
}
51+
}

0 commit comments

Comments
 (0)