Skip to content

Commit 26bcea1

Browse files
committed
threaded mediator and memento
1 parent 6f477eb commit 26bcea1

File tree

9 files changed

+222
-0
lines changed

9 files changed

+222
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package comportamentali.mediator;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.TimeUnit;
5+
6+
public class Airplane extends Colleague implements Runnable {
7+
8+
private static int ID;
9+
private int id;
10+
11+
public Airplane(Mediator mediator) {
12+
super(mediator);
13+
this.id = ++ID;
14+
}
15+
16+
public boolean landingRequest() {
17+
notify("Requesting landing...");
18+
return mediator.landingRequest();
19+
}
20+
21+
private void land() {
22+
notify("Landing now");
23+
((ControlTower) mediator).landed();
24+
}
25+
26+
@Override
27+
public void run() {
28+
Random random = new Random();
29+
wait(random.nextInt(3, 20));
30+
while (!landingRequest()) {
31+
notify("Request rejected, not allowed to land now");
32+
wait(4);
33+
}
34+
wait(random.nextInt(4, 7));
35+
land();
36+
37+
}
38+
39+
void wait(int seconds) {
40+
try {
41+
TimeUnit.SECONDS.sleep(seconds);
42+
} catch (InterruptedException e) {}
43+
}
44+
45+
void notify(String message) {
46+
System.out.println(id + ": " + message);
47+
}
48+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package comportamentali.mediator;
2+
3+
public class Client {
4+
5+
public static void main(String[] args) {
6+
7+
Mediator controlTower = new ControlTower(5);
8+
for (int i = 0; i < 10; i++) {
9+
Runnable runnable = new Airplane(controlTower);
10+
Thread thread = new Thread(runnable);
11+
thread.start();
12+
}
13+
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package comportamentali.mediator;
2+
3+
public abstract class Colleague {
4+
5+
protected Mediator mediator;
6+
7+
public Colleague(Mediator mediator) {
8+
this.mediator = mediator;
9+
}
10+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package comportamentali.mediator;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
public class ControlTower implements Mediator {
6+
7+
private int landingRequests;
8+
private final int maxLandingRequests;
9+
private Semaphore mutex = new Semaphore(1);
10+
11+
public ControlTower(int maxLandingRequests) {
12+
this.landingRequests = 0;
13+
this.maxLandingRequests = maxLandingRequests;
14+
}
15+
16+
public void landed() {
17+
try {
18+
mutex.acquire();
19+
} catch (InterruptedException e) {}
20+
if (landingRequests > 0) landingRequests--;
21+
mutex.release();
22+
}
23+
24+
@Override
25+
public boolean landingRequest() {
26+
try {
27+
mutex.acquire();
28+
} catch (InterruptedException e) {}
29+
if (landingRequests >= maxLandingRequests) {
30+
mutex.release();
31+
return false;
32+
}
33+
landingRequests++;
34+
mutex.release();
35+
return true;
36+
}
37+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package comportamentali.mediator;
2+
3+
public interface Mediator {
4+
5+
boolean landingRequest();
6+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package comportamentali.memento;
2+
3+
import java.util.Random;
4+
5+
public class Client {
6+
7+
public static void main(String[] args) {
8+
Database database = new Database("people");
9+
10+
newTransaction(database, new String[]{"john", "doe", "johndoe@gmail.com"});
11+
newTransaction(database, new String[]{"Cohen", "Oneal", "CohenOneal@gmail.com"});
12+
13+
System.out.println("\n" + database);
14+
}
15+
16+
static void newTransaction(Database db, String[] row) {
17+
MementoNarrow temp = db.save();
18+
System.out.println("Transaction started");
19+
20+
Random random = new Random();
21+
if (random.nextInt(0, 10) < 2) {
22+
System.out.println("Transaction failed, restoring automatically");
23+
db.restore(temp);
24+
} else {
25+
db.add(row);
26+
System.out.println("Transaction finished");
27+
}
28+
}
29+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package comportamentali.memento;
2+
3+
import java.util.*;
4+
5+
public class Database {
6+
7+
private String name;
8+
private List<String[]> rows;
9+
10+
public Database(String name) {
11+
this.name = name;
12+
rows = new ArrayList<>();
13+
14+
}
15+
16+
private class Memento implements MementoWide, MementoNarrow {
17+
18+
String name;
19+
List<String[]> rows;
20+
Date date;
21+
22+
public Memento(String name, List<String[]> rows) {
23+
this.name = name;
24+
this.rows = new ArrayList<>(rows);
25+
this.date = new Date();
26+
}
27+
28+
@Override
29+
public List<String[]> getRows() {
30+
return new ArrayList<>(rows);
31+
}
32+
33+
@Override
34+
public void setRows(List<String[]> rows) {
35+
rows = new ArrayList<>(rows);
36+
}
37+
}
38+
39+
void add(String[] row) {
40+
rows.add(row);
41+
}
42+
43+
MementoNarrow save() {
44+
MementoWide memento = new Memento(this.name, this.rows);
45+
return (MementoNarrow) memento;
46+
}
47+
48+
void restore(MementoNarrow memento) {
49+
this.rows = new ArrayList<>(((MementoWide) memento).getRows());
50+
};
51+
52+
@Override
53+
public String toString() {
54+
StringBuilder result = new StringBuilder();
55+
result.append(name);
56+
result.append("\n");
57+
for (String[] row : rows) {
58+
result.append(Arrays.toString(row));
59+
result.append("\n");
60+
}
61+
return result.toString();
62+
}
63+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package comportamentali.memento;
2+
3+
public interface MementoNarrow {
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package comportamentali.memento;
2+
3+
import java.util.List;
4+
5+
public interface MementoWide {
6+
7+
List<String[]> getRows();
8+
void setRows(List<String[]> rows);
9+
10+
}

0 commit comments

Comments
 (0)