forked from bjmashibing/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
091bb65
commit 57403ee
Showing
23 changed files
with
819 additions
and
326 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.mashibing.dp.TemplateMethod; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
F f = new C1(); | ||
f.m(); | ||
} | ||
|
||
} | ||
|
||
abstract class F { | ||
public void m() { | ||
op1(); | ||
op2(); | ||
} | ||
|
||
abstract void op1(); | ||
abstract void op2(); | ||
} | ||
|
||
class C1 extends F { | ||
|
||
@Override | ||
void op1() { | ||
System.out.println("op1"); | ||
} | ||
|
||
@Override | ||
void op2() { | ||
System.out.println("op2"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
脚本语言解释器 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
记录对象的某个瞬间 | ||
类似照片 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.mashibing.dp.prototype.v1; | ||
|
||
/** | ||
* 浅克隆 | ||
*/ | ||
|
||
public class Test { | ||
public static void main(String[] args) throws Exception { | ||
Person p1 = new Person(); | ||
Person p2 = (Person)p1.clone(); | ||
System.out.println(p2.age + " " + p2.score); | ||
System.out.println(p2.loc); | ||
|
||
System.out.println(p1.loc == p2.loc); | ||
p1.loc.street = "sh"; | ||
System.out.println(p2.loc); | ||
|
||
} | ||
} | ||
|
||
class Person implements Cloneable { | ||
int age = 8; | ||
int score = 100; | ||
|
||
Location loc = new Location("bj", 22); | ||
@Override | ||
public Object clone() throws CloneNotSupportedException { | ||
return super.clone(); | ||
} | ||
} | ||
|
||
class Location { | ||
String street; | ||
int roomNo; | ||
|
||
@Override | ||
public String toString() { | ||
return "Location{" + | ||
"street='" + street + '\'' + | ||
", roomNo=" + roomNo + | ||
'}'; | ||
} | ||
|
||
public Location(String street, int roomNo) { | ||
this.street = street; | ||
this.roomNo = roomNo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.mashibing.dp.prototype.v2; | ||
|
||
/** | ||
* 深克隆的处理 | ||
*/ | ||
public class Test { | ||
public static void main(String[] args) throws Exception { | ||
Person p1 = new Person(); | ||
Person p2 = (Person)p1.clone(); | ||
System.out.println(p2.age + " " + p2.score); | ||
System.out.println(p2.loc); | ||
|
||
System.out.println(p1.loc == p2.loc); | ||
p1.loc.street = "sh"; | ||
System.out.println(p2.loc); | ||
|
||
|
||
|
||
} | ||
} | ||
|
||
class Person implements Cloneable { | ||
int age = 8; | ||
int score = 100; | ||
|
||
Location loc = new Location("bj", 22); | ||
@Override | ||
public Object clone() throws CloneNotSupportedException { | ||
Person p = (Person)super.clone(); | ||
p.loc = (Location)loc.clone(); | ||
return p; | ||
} | ||
} | ||
|
||
class Location implements Cloneable { | ||
String street; | ||
int roomNo; | ||
|
||
@Override | ||
public String toString() { | ||
return "Location{" + | ||
"street='" + street + '\'' + | ||
", roomNo=" + roomNo + | ||
'}'; | ||
} | ||
|
||
public Location(String street, int roomNo) { | ||
this.street = street; | ||
this.roomNo = roomNo; | ||
} | ||
|
||
@Override | ||
public Object clone() throws CloneNotSupportedException { | ||
return super.clone(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.mashibing.dp.prototype.v3; | ||
|
||
/** | ||
* String需要进一步深克隆吗? | ||
*/ | ||
public class Test { | ||
public static void main(String[] args) throws Exception { | ||
Person p1 = new Person(); | ||
Person p2 = (Person)p1.clone(); | ||
System.out.println(p2.age + " " + p2.score); | ||
System.out.println(p2.loc); | ||
|
||
System.out.println(p1.loc == p2.loc); | ||
p1.loc.street = "sh"; | ||
System.out.println(p2.loc); | ||
|
||
p1.loc.street.replace("sh", "sz"); | ||
System.out.println(p2.loc.street); | ||
} | ||
} | ||
|
||
class Person implements Cloneable { | ||
int age = 8; | ||
int score = 100; | ||
|
||
Location loc = new Location("bj", 22); | ||
@Override | ||
public Object clone() throws CloneNotSupportedException { | ||
Person p = (Person)super.clone(); | ||
p.loc = (Location)loc.clone(); | ||
return p; | ||
} | ||
} | ||
|
||
class Location implements Cloneable { | ||
String street; | ||
int roomNo; | ||
|
||
@Override | ||
public String toString() { | ||
return "Location{" + | ||
"street='" + street + '\'' + | ||
", roomNo=" + roomNo + | ||
'}'; | ||
} | ||
|
||
public Location(String street, int roomNo) { | ||
this.street = street; | ||
this.roomNo = roomNo; | ||
} | ||
|
||
@Override | ||
public Object clone() throws CloneNotSupportedException { | ||
return super.clone(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.mashibing.dp.prototype.v4; | ||
|
||
/** | ||
* String需要进一步深克隆吗? | ||
*/ | ||
public class Test { | ||
public static void main(String[] args) throws Exception { | ||
Person p1 = new Person(); | ||
Person p2 = (Person)p1.clone(); | ||
System.out.println("p1.loc == p2.loc? " + (p1.loc == p2.loc)); | ||
|
||
p1.loc.street.reverse(); | ||
System.out.println(p2.loc.street); | ||
} | ||
} | ||
|
||
class Person implements Cloneable { | ||
int age = 8; | ||
int score = 100; | ||
|
||
Location loc = new Location(new StringBuilder("bj"), 22); | ||
@Override | ||
public Object clone() throws CloneNotSupportedException { | ||
Person p = (Person)super.clone(); | ||
p.loc = (Location)loc.clone(); | ||
return p; | ||
} | ||
} | ||
|
||
class Location implements Cloneable { | ||
StringBuilder street; | ||
int roomNo; | ||
|
||
@Override | ||
public String toString() { | ||
return "Location{" + | ||
"street='" + street + '\'' + | ||
", roomNo=" + roomNo + | ||
'}'; | ||
} | ||
|
||
public Location(StringBuilder street, int roomNo) { | ||
this.street = street; | ||
this.roomNo = roomNo; | ||
} | ||
|
||
@Override | ||
public Object clone() throws CloneNotSupportedException { | ||
return super.clone(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
根据状态决定动作 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.mashibing.dp.state.thread; | ||
|
||
public class Action { | ||
String msg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mashibing.dp.state.thread; | ||
|
||
public class NewState extends ThreadState_ { | ||
private Thread_ t; | ||
|
||
public NewState(Thread_ t) { | ||
this.t = t; | ||
} | ||
|
||
@Override | ||
void move(Action input) { | ||
if(input.msg == "start") | ||
t.state = new RunningState(t); | ||
} | ||
|
||
@Override | ||
void run() { | ||
|
||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/mashibing/dp/state/thread/RunningState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mashibing.dp.state.thread; | ||
|
||
public class RunningState extends ThreadState_ { | ||
private Thread_ t; | ||
|
||
public RunningState(Thread_ t) { | ||
this.t = t; | ||
} | ||
|
||
@Override | ||
void move(Action input) { | ||
|
||
} | ||
|
||
@Override | ||
void run() { | ||
|
||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/mashibing/dp/state/thread/TerminatedState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mashibing.dp.state.thread; | ||
|
||
public class TerminatedState extends ThreadState_ { | ||
private Thread_ t; | ||
|
||
public TerminatedState(Thread_ t) { | ||
this.t = t; | ||
} | ||
|
||
@Override | ||
void move(Action input) { | ||
|
||
} | ||
|
||
@Override | ||
void run() { | ||
|
||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/mashibing/dp/state/thread/ThreadState_.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.mashibing.dp.state.thread; | ||
|
||
public abstract class ThreadState_ { | ||
abstract void move(Action input); | ||
abstract void run(); | ||
} |
Oops, something went wrong.