Skip to content

Commit a5ab5c7

Browse files
authored
Merge branch 'master' into converter
2 parents 72a0765 + 286d6c3 commit a5ab5c7

File tree

30 files changed

+488
-62
lines changed

30 files changed

+488
-62
lines changed

balking/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
layout: pattern
3+
title: Balking
4+
folder: balking
5+
permalink: /patterns/balking/
6+
categories: Concurrency
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Intent
13+
Balking Pattern is used to prevent an object from executing certain code if it is an
14+
incomplete or inappropriate state
15+
16+
![alt text](./etc/balking.png "Balking")
17+
18+
## Applicability
19+
Use the Balking pattern when
20+
21+
*you want to invoke an action on an object only when it is in a particular state
22+
*objects are generally only in a state that is prone to balking temporarily
23+
but for an unknown amount of time
24+
25+
## Related patterns
26+
* Guarded Suspendion Pattern
27+
* Double Checked Locking Pattern

balking/etc/balking.png

20.9 KB
Loading

balking/etc/balking.ucls

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.13" icons="true" automaticImage="PNG" always-add-relationships="false"
3+
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
4+
router="FAN">
5+
<class id="1" language="java" name="com.iluwatar.balking.App" project="balking"
6+
file="/balking/src/main/java/com/iluwatar/balking/App.java" binary="false" corner="BOTTOM_RIGHT">
7+
<position height="113" width="114" x="135" y="103"/>
8+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
9+
sort-features="false" accessors="true" visibility="true">
10+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
11+
<operations public="true" package="true" protected="true" private="true" static="true"/>
12+
</display>
13+
</class>
14+
<class id="2" language="java" name="com.iluwatar.balking.WashingMachine" project="balking"
15+
file="/balking/src/main/java/com/iluwatar/balking/WashingMachine.java" binary="false" corner="BOTTOM_RIGHT">
16+
<position height="149" width="268" x="289" y="103"/>
17+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
18+
sort-features="false" accessors="true" visibility="true">
19+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
20+
<operations public="true" package="true" protected="true" private="true" static="true"/>
21+
</display>
22+
</class>
23+
<enumeration id="3" language="java" name="com.iluwatar.balking.WashingMachineState" project="balking"
24+
file="/balking/src/main/java/com/iluwatar/balking/WashingMachineState.java" binary="false" corner="BOTTOM_RIGHT">
25+
<position height="113" width="192" x="289" y="292"/>
26+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
27+
sort-features="false" accessors="true" visibility="true">
28+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
29+
<operations public="true" package="true" protected="true" private="true" static="true"/>
30+
</display>
31+
</enumeration>
32+
<association id="4">
33+
<end type="SOURCE" refId="2" navigable="false">
34+
<attribute id="5" name="washingMachineState"/>
35+
<multiplicity id="6" minimum="0" maximum="1"/>
36+
</end>
37+
<end type="TARGET" refId="3" navigable="true"/>
38+
<display labels="true" multiplicity="true"/>
39+
</association>
40+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
41+
sort-features="false" accessors="true" visibility="true">
42+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
43+
<operations public="true" package="true" protected="true" private="true" static="true"/>
44+
</classifier-display>
45+
<association-display labels="true" multiplicity="true"/>
46+
</class-diagram>

balking/etc/balking.urm.puml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@startuml
2+
package com.iluwatar.balking {
3+
class App {
4+
- LOGGER : Logger {static}
5+
+ App()
6+
+ main(args : String[]) {static}
7+
}
8+
class WashingMachine {
9+
- LOGGER : Logger {static}
10+
- washingMachineState : WashingMachineState
11+
+ WashingMachine()
12+
+ endOfWashing()
13+
+ getWashingMachineState() : WashingMachineState
14+
+ wash()
15+
}
16+
enum WashingMachineState {
17+
+ ENABLED {static}
18+
+ WASHING {static}
19+
+ valueOf(name : String) : WashingMachineState {static}
20+
+ values() : WashingMachineState[] {static}
21+
}
22+
}
23+
WashingMachine --> "-washingMachineState" WashingMachineState
24+
@enduml

balking/pom.xml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.15.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>balking</artifactId>
37+
<dependencies>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
46+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.balking;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import java.util.concurrent.ExecutorService;
29+
import java.util.concurrent.Executors;
30+
import java.util.concurrent.TimeUnit;
31+
32+
/**
33+
* In Balking Design Pattern if an object’s method is invoked when it is in an inappropriate state,
34+
* then the method will return without doing anything. Objects that use this pattern are generally only in a
35+
* state that is prone to balking temporarily but for an unknown amount of time
36+
*
37+
* In this example implementation WashingMachine is an object that has two states
38+
* in which it can be: ENABLED and WASHING. If the machine is ENABLED
39+
* the state is changed into WASHING that any other thread can't invoke this action on this and then do the job.
40+
* On the other hand if it have been already washing and any other thread execute wash()
41+
* it can't do that once again and returns doing nothing.
42+
*/
43+
44+
public class App {
45+
46+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
47+
48+
/**
49+
* @param args the command line arguments - not used
50+
*/
51+
public static void main(String... args) {
52+
final WashingMachine washingMachine = new WashingMachine();
53+
ExecutorService executorService = Executors.newFixedThreadPool(3);
54+
for (int i = 0; i < 3; i++) {
55+
executorService.execute(washingMachine::wash);
56+
}
57+
executorService.shutdown();
58+
try {
59+
executorService.awaitTermination(10, TimeUnit.SECONDS);
60+
} catch (InterruptedException ie) {
61+
LOGGER.error("ERROR: Waiting on executor service shutdown!");
62+
}
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.balking;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
public class WashingMachine {
29+
30+
private static final Logger LOGGER = LoggerFactory.getLogger(WashingMachine.class);
31+
32+
private WashingMachineState washingMachineState;
33+
34+
public WashingMachine() {
35+
washingMachineState = WashingMachineState.ENABLED;
36+
}
37+
38+
public WashingMachineState getWashingMachineState() {
39+
return washingMachineState;
40+
}
41+
42+
/**
43+
* Method responsible for washing
44+
* if the object is in appropriate state
45+
*/
46+
public void wash() {
47+
synchronized (this) {
48+
LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), getWashingMachineState());
49+
if (washingMachineState == WashingMachineState.WASHING) {
50+
LOGGER.error("ERROR: Cannot wash if the machine has been already washing!");
51+
return;
52+
}
53+
washingMachineState = WashingMachineState.WASHING;
54+
}
55+
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
56+
try {
57+
Thread.sleep(50);
58+
} catch (InterruptedException ie) {
59+
ie.printStackTrace();
60+
}
61+
endOfWashing();
62+
}
63+
64+
/**
65+
* Method responsible of ending the washing
66+
* by changing machine state
67+
*/
68+
public synchronized void endOfWashing() {
69+
washingMachineState = WashingMachineState.ENABLED;
70+
LOGGER.info("{}: Washing completed.", Thread.currentThread().getId());
71+
}
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.balking;
24+
25+
/**
26+
* WashingMachineState enum describes in which state machine is,
27+
* it can be enabled and ready to work as well as during washing
28+
*/
29+
30+
public enum WashingMachineState {
31+
ENABLED, WASHING
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.balking;
25+
26+
import org.junit.Test;
27+
28+
/**
29+
* Application test
30+
*/
31+
public class AppTest {
32+
33+
@Test
34+
public void main() throws Exception {
35+
String[] args = {};
36+
App.main(args);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)