Skip to content

Commit

Permalink
设计模式V1.0完结
Browse files Browse the repository at this point in the history
  • Loading branch information
bjmashibing committed May 28, 2019
1 parent 091bb65 commit 57403ee
Show file tree
Hide file tree
Showing 23 changed files with 819 additions and 326 deletions.
668 changes: 349 additions & 319 deletions .idea/workspace.xml

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/main/java/com/mashibing/dp/TemplateMethod/Main.java
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");
}
}
17 changes: 10 additions & 7 deletions src/main/java/com/mashibing/dp/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.concurrent.locks.LockSupport;

public class Test {
public static void main(String[] args) {
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
public static void main(String[] args) throws Exception {
Thread t = new Thread(()->{
System.out.println("start");
LockSupport.park(); //一直wait
System.out.println("continue");
});
t.start();

Thread.sleep(1000);
LockSupport.unpark(t); //t线程解除wait态
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/mashibing/dp/command/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.mashibing.dp.command;

import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) {
Content c = new Content();
Expand All @@ -16,6 +19,23 @@ public static void main(String[] args) {
deleteCommand.doit();
deleteCommand.undo();

List<Command> commands = new ArrayList<>();
commands.add(new InsertCommand(c));
commands.add(new CopyCommand(c));
commands.add(new DeleteCommand(c));

for(Command comm : commands) {
comm.doit();
}


System.out.println(c.msg);

for(int i= commands.size()-1; i>=0; i--) {
commands.get(i).undo();
}


System.out.println(c.msg);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/mashibing/dp/intepreter/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
脚本语言解释器
2 changes: 2 additions & 0 deletions src/main/java/com/mashibing/dp/memento/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
记录对象的某个瞬间
类似照片
48 changes: 48 additions & 0 deletions src/main/java/com/mashibing/dp/prototype/v1/Test.java
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;
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/mashibing/dp/prototype/v2/Test.java
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();
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/mashibing/dp/prototype/v3/Test.java
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();
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/mashibing/dp/prototype/v4/Test.java
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();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/mashibing/dp/state/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
根据状态决定动作

5 changes: 5 additions & 0 deletions src/main/java/com/mashibing/dp/state/thread/Action.java
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;
}
20 changes: 20 additions & 0 deletions src/main/java/com/mashibing/dp/state/thread/NewState.java
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 src/main/java/com/mashibing/dp/state/thread/RunningState.java
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 src/main/java/com/mashibing/dp/state/thread/TerminatedState.java
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 src/main/java/com/mashibing/dp/state/thread/ThreadState_.java
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();
}
Loading

0 comments on commit 57403ee

Please sign in to comment.