File tree Expand file tree Collapse file tree 23 files changed +819
-326
lines changed
src/main/java/com/mashibing/dp Expand file tree Collapse file tree 23 files changed +819
-326
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 4
4
import java .awt .event .WindowAdapter ;
5
5
import java .awt .event .WindowEvent ;
6
6
import java .awt .event .WindowListener ;
7
+ import java .util .concurrent .locks .LockSupport ;
7
8
8
9
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" );
16
15
});
16
+ t .start ();
17
+
18
+ Thread .sleep (1000 );
19
+ LockSupport .unpark (t ); //t线程解除wait态
17
20
}
18
21
}
19
22
Original file line number Diff line number Diff line change 1
1
package com .mashibing .dp .command ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
3
6
public class Main {
4
7
public static void main (String [] args ) {
5
8
Content c = new Content ();
@@ -16,6 +19,23 @@ public static void main(String[] args) {
16
19
deleteCommand .doit ();
17
20
deleteCommand .undo ();
18
21
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
+
19
39
System .out .println (c .msg );
20
40
}
21
41
}
Original file line number Diff line number Diff line change
1
+ 脚本语言解释器
Original file line number Diff line number Diff line change
1
+ 记录对象的某个瞬间
2
+ 类似照片
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments