Skip to content

Commit a5646b6

Browse files
Azureyjtiluwatar
authored andcommitted
iluwatar#554: Subclass Sandbox pattern (iluwatar#998)
* Fix issue iluwatar#761: ThreadSafeDoubleCheckLocking.java: Instantiating by Reflection call will be successful if you do that firstly * Create leader election module * Create Interface of Instance and MessageManager * Create implementations with token ring algorithm * Change package structure. Create basic message system. * Implement heartbeat and heartbeat invoking message system * Implement election message handler * Add leader message handler * Add main entry point * Add comments * Update README.md * Fix checkstyle issue * Add Unit Tests * Add Unit Tests * Add bully leader selection * Change System.out to log print. Add MIT license in each file. * Add More java doc comments * Add unit test * Add unit tests * Add subclass-sandbox * Add Unit Test * Add Unit Test * Fix Typo * Move dependency into parent pom.xml * Change local valuable reference to be var
1 parent 27c131c commit a5646b6

File tree

10 files changed

+517
-1
lines changed

10 files changed

+517
-1
lines changed

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<jaxb-api.version>2.3.1</jaxb-api.version>
6161
<jaxb-impl.version>2.3.2</jaxb-impl.version>
6262
<annotation-api.version>1.3.2</annotation-api.version>
63+
<system-rules.version>1.19.0</system-rules.version>
6364
</properties>
6465
<modules>
6566
<module>abstract-factory</module>
@@ -180,8 +181,8 @@
180181
<module>bytecode</module>
181182
<module>leader-election</module>
182183
<module>data-locality</module>
184+
<module>subclass-sandbox</module>
183185
<module>circuit-breaker</module>
184-
185186
</modules>
186187

187188
<repositories>
@@ -317,6 +318,12 @@
317318
<artifactId>javassist</artifactId>
318319
<version>3.25.0-GA</version>
319320
</dependency>
321+
<dependency>
322+
<groupId>com.github.stefanbirkner</groupId>
323+
<artifactId>system-rules</artifactId>
324+
<version>${system-rules.version}</version>
325+
<scope>test</scope>
326+
</dependency>
320327
</dependencies>
321328
</dependencyManagement>
322329

subclass-sandbox/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
---
3+
layout: pattern
4+
title: Subclass Sandbox
5+
folder: subclass-sandbox
6+
permalink: /patterns/subclass-sandbox/
7+
categories: Other
8+
tags:
9+
- Java
10+
- Difficulty-Beginner
11+
---
12+
13+
## Intent
14+
The subclass sandbox pattern describes a basic idea, while not having a lot of detailed mechanics. You will need the pattern when you have several similar subclasses. If you have to make a tiny change, then change the base class, while all subclasses shouldn't have to be touched. So the base class has to be able to provide all of the operations a derived class needs to perform.
15+
16+
## Applicability
17+
The Subclass Sandbox pattern is a very simple, common pattern lurking in lots of codebases, even outside of games. If you have a non-virtual protected method laying around, you’re probably already using something like this. Subclass Sandbox is a good fit when:
18+
19+
- You have a base class with a number of derived classes.
20+
21+
- The base class is able to provide all of the operations that a derived class may need to perform.
22+
23+
- There is behavioral overlap in the subclasses and you want to make it easier to share code between them.
24+
25+
- You want to minimize coupling between those derived classes and the rest of the program.
26+
27+
## Credits
28+
29+
* [Game Programming Patterns - Subclass Sandbox]([http://gameprogrammingpatterns.com/subclass-sandbox.html](http://gameprogrammingpatterns.com/subclass-sandbox.html))

subclass-sandbox/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright ? 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.22.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>subclass-sandbox</artifactId>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.github.stefanbirkner</groupId>
45+
<artifactId>system-rules</artifactId>
46+
</dependency>
47+
</dependencies>
48+
49+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 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.subclasssandbox;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* The subclass sandbox pattern describes a basic idea, while not having a lot
31+
* of detailed mechanics. You will need the pattern when you have several similar
32+
* subclasses. If you have to make a tiny change, then change the base class,
33+
* while all subclasses shouldn't have to be touched. So the base class has to be
34+
* able to provide all of the operations a derived class needs to perform.
35+
*/
36+
public class App {
37+
38+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
39+
40+
/**
41+
* Entry point of the main program.
42+
* @param args Program runtime arguments.
43+
*/
44+
public static void main(String[] args) {
45+
LOGGER.info("Use superpower: sky launch");
46+
var skyLaunch = new SkyLaunch();
47+
skyLaunch.activate();
48+
LOGGER.info("Use superpower: ground dive");
49+
var groundDive = new GroundDive();
50+
groundDive.activate();
51+
}
52+
53+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 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.subclasssandbox;
25+
26+
import org.slf4j.LoggerFactory;
27+
28+
/**
29+
* GroundDive superpower.
30+
*/
31+
public class GroundDive extends Superpower {
32+
33+
public GroundDive() {
34+
super();
35+
logger = LoggerFactory.getLogger(GroundDive.class);
36+
}
37+
38+
@Override
39+
protected void activate() {
40+
move(0, 0, -20);
41+
playSound("GROUNDDIVE_SOUND", 5);
42+
spawnParticles("GROUNDDIVE_PARTICLE", 20);
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 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.subclasssandbox;
25+
26+
import org.slf4j.LoggerFactory;
27+
28+
/**
29+
* SkyLaunch superpower.
30+
*/
31+
public class SkyLaunch extends Superpower {
32+
33+
public SkyLaunch() {
34+
super();
35+
logger = LoggerFactory.getLogger(SkyLaunch.class);
36+
}
37+
38+
@Override
39+
protected void activate() {
40+
move(0, 0, 20);
41+
playSound("SKYLAUNCH_SOUND", 1);
42+
spawnParticles("SKYLAUNCH_PARTICLE", 100);
43+
}
44+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 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.subclasssandbox;
25+
26+
import org.slf4j.Logger;
27+
28+
/**
29+
* Superpower abstract class. In this class the basic operations of all types of
30+
* superpowers are provided as protected methods.
31+
*/
32+
public abstract class Superpower {
33+
34+
protected Logger logger;
35+
36+
/**
37+
* Subclass of superpower should implement this sandbox method by calling the
38+
* methods provided in this super class.
39+
*/
40+
protected abstract void activate();
41+
42+
/**
43+
* Move to (x, y, z).
44+
* @param x X coordinate.
45+
* @param y Y coordinate.
46+
* @param z Z coordinate.
47+
*/
48+
protected void move(double x, double y, double z) {
49+
logger.info("Move to ( " + x + ", " + y + ", " + z + " )");
50+
}
51+
52+
/**
53+
* Play sound effect for the superpower.
54+
* @param soundName Sound name.
55+
* @param volumn Value of volumn.
56+
*/
57+
protected void playSound(String soundName, int volumn) {
58+
logger.info("Play " + soundName + " with volumn " + volumn);
59+
}
60+
61+
/**
62+
* Spawn particles for the superpower.
63+
* @param particleType Particle type.
64+
* @param count Count of particles to be spawned.
65+
*/
66+
protected void spawnParticles(String particleType, int count) {
67+
logger.info("Spawn " + count + " particle with type " + particleType);
68+
}
69+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 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.subclasssandbox;
25+
26+
import org.junit.Test;
27+
28+
/**
29+
* App unit tests.
30+
*/
31+
public class AppTest {
32+
33+
@Test
34+
public void testMain() {
35+
String[] args = {};
36+
App.main(args);
37+
}
38+
}

0 commit comments

Comments
 (0)