Skip to content

Commit 306b1f3

Browse files
committed
Reformat rest of the design patterns - Issue iluwatar#224
1 parent 449340b commit 306b1f3

File tree

337 files changed

+6988
-7095
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

337 files changed

+6988
-7095
lines changed

double-checked-locking/src/main/java/com/iluwatar/doublechecked/locking/App.java

+27-28
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,37 @@
66

77
/**
88
*
9-
* Double Checked Locking is a concurrency design pattern used to reduce the overhead
10-
* of acquiring a lock by first testing the locking criterion (the "lock hint") without
11-
* actually acquiring the lock. Only if the locking criterion check indicates that
12-
* locking is required does the actual locking logic proceed.
9+
* Double Checked Locking is a concurrency design pattern used to reduce the overhead of acquiring a
10+
* lock by first testing the locking criterion (the "lock hint") without actually acquiring the
11+
* lock. Only if the locking criterion check indicates that locking is required does the actual
12+
* locking logic proceed.
1313
* <p>
14-
* In {@link Inventory} we store the items with a given size. However, we do not store
15-
* more items than the inventory size. To address concurrent access problems we
16-
* use double checked locking to add item to inventory. In this method, the
17-
* thread which gets the lock first adds the item.
14+
* In {@link Inventory} we store the items with a given size. However, we do not store more items
15+
* than the inventory size. To address concurrent access problems we use double checked locking to
16+
* add item to inventory. In this method, the thread which gets the lock first adds the item.
1817
*
1918
*/
2019
public class App {
2120

22-
/**
23-
* Program entry point
24-
* @param args command line args
25-
*/
26-
public static void main(String[] args) {
27-
final Inventory inventory = new Inventory(1000);
28-
ExecutorService executorService = Executors.newFixedThreadPool(3);
29-
for (int i = 0; i < 3; i++) {
30-
executorService.execute(() -> {
31-
while (inventory.addItem(new Item()))
32-
;
33-
});
34-
}
21+
/**
22+
* Program entry point
23+
*
24+
* @param args command line args
25+
*/
26+
public static void main(String[] args) {
27+
final Inventory inventory = new Inventory(1000);
28+
ExecutorService executorService = Executors.newFixedThreadPool(3);
29+
for (int i = 0; i < 3; i++) {
30+
executorService.execute(() -> {
31+
while (inventory.addItem(new Item()));
32+
});
33+
}
3534

36-
executorService.shutdown();
37-
try {
38-
executorService.awaitTermination(5, TimeUnit.SECONDS);
39-
} catch (InterruptedException e) {
40-
System.out.println("Error waiting for ExecutorService shutdown");
41-
}
42-
}
35+
executorService.shutdown();
36+
try {
37+
executorService.awaitTermination(5, TimeUnit.SECONDS);
38+
} catch (InterruptedException e) {
39+
System.out.println("Error waiting for ExecutorService shutdown");
40+
}
41+
}
4342
}

double-checked-locking/src/main/java/com/iluwatar/doublechecked/locking/Inventory.java

+24-26
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,30 @@
1212
*/
1313
public class Inventory {
1414

15-
private final int inventorySize;
16-
private final List<Item> items;
17-
private final Lock lock;
15+
private final int inventorySize;
16+
private final List<Item> items;
17+
private final Lock lock;
1818

19-
public Inventory(int inventorySize) {
20-
this.inventorySize = inventorySize;
21-
this.items = new ArrayList<>(inventorySize);
22-
this.lock = new ReentrantLock();
23-
}
24-
25-
public boolean addItem(Item item) {
26-
if (items.size() < inventorySize) {
27-
lock.lock();
28-
try {
29-
if (items.size() < inventorySize) {
30-
items.add(item);
31-
System.out.println(Thread.currentThread()
32-
+ ": items.size()=" + items.size()
33-
+ ", inventorySize=" + inventorySize);
34-
return true;
35-
}
36-
} finally {
37-
lock.unlock();
38-
}
39-
}
40-
return false;
41-
}
19+
public Inventory(int inventorySize) {
20+
this.inventorySize = inventorySize;
21+
this.items = new ArrayList<>(inventorySize);
22+
this.lock = new ReentrantLock();
23+
}
4224

25+
public boolean addItem(Item item) {
26+
if (items.size() < inventorySize) {
27+
lock.lock();
28+
try {
29+
if (items.size() < inventorySize) {
30+
items.add(item);
31+
System.out.println(Thread.currentThread() + ": items.size()=" + items.size()
32+
+ ", inventorySize=" + inventorySize);
33+
return true;
34+
}
35+
} finally {
36+
lock.unlock();
37+
}
38+
}
39+
return false;
40+
}
4341
}

double-checked-locking/src/main/java/com/iluwatar/doublechecked/locking/Item.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*/
88
public class Item {
9-
10-
private String name;
11-
private int level;
9+
10+
private String name;
11+
private int level;
1212
}

double-checked-locking/src/test/java/com/iluwatar/doublechecked/locking/AppTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
*/
1212
public class AppTest {
1313

14-
@Test
15-
public void test() {
16-
String[] args = {};
17-
App.main(args);
18-
}
14+
@Test
15+
public void test() {
16+
String[] args = {};
17+
App.main(args);
18+
}
1919
}

double-dispatch/src/main/java/com/iluwatar/doubledispatch/App.java

+36-32
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,50 @@
55

66
/**
77
*
8-
* When a message with a parameter is sent to an object, the resultant behaviour is defined by the
9-
* implementation of that method in the receiver. Sometimes the behaviour must also be determined
10-
* by the type of the parameter.
8+
* When a message with a parameter is sent to an object, the resultant behaviour is defined by the
9+
* implementation of that method in the receiver. Sometimes the behaviour must also be determined by
10+
* the type of the parameter.
1111
* <p>
12-
* One way to implement this would be to create multiple instanceof-checks for the methods parameter.
13-
* However, this creates a maintenance issue. When new types are added we would also need to change
14-
* the method's implementation and add a new instanceof-check. This violates the single responsibility
15-
* principle - a class should have only one reason to change.
12+
* One way to implement this would be to create multiple instanceof-checks for the methods
13+
* parameter. However, this creates a maintenance issue. When new types are added we would also need
14+
* to change the method's implementation and add a new instanceof-check. This violates the single
15+
* responsibility principle - a class should have only one reason to change.
1616
* <p>
1717
* Instead of the instanceof-checks a better way is to make another virtual call on the parameter
1818
* object. This way new functionality can be easily added without the need to modify existing
1919
* implementation (open-closed principle).
2020
* <p>
21-
* In this example we have hierarchy of objects ({@link GameObject}) that can collide to each other. Each
22-
* object has its own coordinates which are checked against the other objects' coordinates. If
21+
* In this example we have hierarchy of objects ({@link GameObject}) that can collide to each other.
22+
* Each object has its own coordinates which are checked against the other objects' coordinates. If
2323
* there is an overlap, then the objects collide utilizing the Double Dispatch pattern.
2424
*
2525
*/
2626
public class App {
27-
28-
/**
29-
* Program entry point
30-
* @param args command line args
31-
*/
32-
public static void main( String[] args ) {
33-
// initialize game objects and print their status
34-
List<GameObject> objects = new ArrayList<>();
35-
objects.add(new FlamingAsteroid(0, 0, 5, 5));
36-
objects.add(new SpaceStationMir(1, 1, 2, 2));
37-
objects.add(new Meteoroid(10, 10, 15, 15));
38-
objects.add(new SpaceStationIss(12, 12, 14, 14));
39-
objects.stream().forEach(o -> System.out.println(o));
40-
System.out.println("");
41-
42-
// collision check
43-
objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> { if (o1 != o2 && o1.intersectsWith(o2)) o1.collision(o2); } ));
44-
System.out.println("");
45-
46-
// output eventual object statuses
47-
objects.stream().forEach(o -> System.out.println(o));
48-
System.out.println("");
49-
}
27+
28+
/**
29+
* Program entry point
30+
*
31+
* @param args command line args
32+
*/
33+
public static void main(String[] args) {
34+
// initialize game objects and print their status
35+
List<GameObject> objects = new ArrayList<>();
36+
objects.add(new FlamingAsteroid(0, 0, 5, 5));
37+
objects.add(new SpaceStationMir(1, 1, 2, 2));
38+
objects.add(new Meteoroid(10, 10, 15, 15));
39+
objects.add(new SpaceStationIss(12, 12, 14, 14));
40+
objects.stream().forEach(o -> System.out.println(o));
41+
System.out.println("");
42+
43+
// collision check
44+
objects.stream().forEach(o1 -> objects.stream().forEach(o2 -> {
45+
if (o1 != o2 && o1.intersectsWith(o2))
46+
o1.collision(o2);
47+
}));
48+
System.out.println("");
49+
50+
// output eventual object statuses
51+
objects.stream().forEach(o -> System.out.println(o));
52+
System.out.println("");
53+
}
5054
}

double-dispatch/src/main/java/com/iluwatar/doubledispatch/FlamingAsteroid.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
*/
88
public class FlamingAsteroid extends Meteoroid {
99

10-
public FlamingAsteroid(int left, int top, int right, int bottom) {
11-
super(left, top, right, bottom);
12-
setOnFire(true);
13-
}
10+
public FlamingAsteroid(int left, int top, int right, int bottom) {
11+
super(left, top, right, bottom);
12+
setOnFire(true);
13+
}
1414

15-
@Override
16-
public void collision(GameObject gameObject) {
17-
gameObject.collisionResolve(this);
18-
}
15+
@Override
16+
public void collision(GameObject gameObject) {
17+
gameObject.collisionResolve(this);
18+
}
1919
}

double-dispatch/src/main/java/com/iluwatar/doubledispatch/GameObject.java

+40-41
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,47 @@
22

33
/**
44
*
5-
* Game objects have coordinates and some
6-
* other status information.
5+
* Game objects have coordinates and some other status information.
76
*
87
*/
98
public abstract class GameObject extends Rectangle {
10-
11-
private boolean damaged;
12-
private boolean onFire;
13-
14-
public GameObject(int left, int top, int right, int bottom) {
15-
super(left, top, right, bottom);
16-
}
17-
18-
@Override
19-
public String toString() {
20-
return String.format("%s at %s damaged=%b onFire=%b", this.getClass().getSimpleName(),
21-
super.toString(), isDamaged(), isOnFire());
22-
}
23-
24-
public boolean isOnFire() {
25-
return onFire;
26-
}
27-
28-
public void setOnFire(boolean onFire) {
29-
this.onFire = onFire;
30-
}
31-
32-
public boolean isDamaged() {
33-
return damaged;
34-
}
35-
36-
public void setDamaged(boolean damaged) {
37-
this.damaged = damaged;
38-
}
39-
40-
public abstract void collision(GameObject gameObject);
41-
42-
public abstract void collisionResolve(FlamingAsteroid asteroid);
43-
44-
public abstract void collisionResolve(Meteoroid meteoroid);
45-
46-
public abstract void collisionResolve(SpaceStationMir mir);
47-
48-
public abstract void collisionResolve(SpaceStationIss iss);
9+
10+
private boolean damaged;
11+
private boolean onFire;
12+
13+
public GameObject(int left, int top, int right, int bottom) {
14+
super(left, top, right, bottom);
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return String.format("%s at %s damaged=%b onFire=%b", this.getClass().getSimpleName(),
20+
super.toString(), isDamaged(), isOnFire());
21+
}
22+
23+
public boolean isOnFire() {
24+
return onFire;
25+
}
26+
27+
public void setOnFire(boolean onFire) {
28+
this.onFire = onFire;
29+
}
30+
31+
public boolean isDamaged() {
32+
return damaged;
33+
}
34+
35+
public void setDamaged(boolean damaged) {
36+
this.damaged = damaged;
37+
}
38+
39+
public abstract void collision(GameObject gameObject);
40+
41+
public abstract void collisionResolve(FlamingAsteroid asteroid);
42+
43+
public abstract void collisionResolve(Meteoroid meteoroid);
44+
45+
public abstract void collisionResolve(SpaceStationMir mir);
46+
47+
public abstract void collisionResolve(SpaceStationIss iss);
4948
}

0 commit comments

Comments
 (0)