mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-13 14:16:02 +00:00
docs: update subclass sandbox
This commit is contained in:
1 parent
201675d7fc
commit
d4f80630aa
1 file changed
+17
-19
+17
-19
@@ -37,9 +37,9 @@ In plain words
|
|||||||
|
|
||||||
Suppose you want to create various superpowers in a game, where each superpower needs to move with a sound effect and spawn particles. Should you create many classes with similar methods or derive them from a base class? The Subclass Sandbox pattern enables you to handle this efficiently by deriving these classes from a common base class.
|
Suppose you want to create various superpowers in a game, where each superpower needs to move with a sound effect and spawn particles. Should you create many classes with similar methods or derive them from a base class? The Subclass Sandbox pattern enables you to handle this efficiently by deriving these classes from a common base class.
|
||||||
|
|
||||||
We start with the base class `Superpower`. It contains an abstract sandbox method `active()` and some provided operations.
|
We start with the base class `Superpower`. It contains an abstract sandbox method `active` and some provided operations.
|
||||||
|
|
||||||
```
|
```java
|
||||||
public abstract class Superpower {
|
public abstract class Superpower {
|
||||||
|
|
||||||
protected Logger logger;
|
protected Logger logger;
|
||||||
@@ -62,7 +62,7 @@ public abstract class Superpower {
|
|||||||
|
|
||||||
Next, we are able to create derived sandboxed subclass that implements the sandbox method using the provided operations. Here is the first power:
|
Next, we are able to create derived sandboxed subclass that implements the sandbox method using the provided operations. Here is the first power:
|
||||||
|
|
||||||
```
|
```java
|
||||||
public class SkyLaunch extends Superpower {
|
public class SkyLaunch extends Superpower {
|
||||||
|
|
||||||
public SkyLaunch() {
|
public SkyLaunch() {
|
||||||
@@ -81,7 +81,7 @@ public class SkyLaunch extends Superpower {
|
|||||||
|
|
||||||
Here is the second power.
|
Here is the second power.
|
||||||
|
|
||||||
```
|
```java
|
||||||
public class GroundDive extends Superpower {
|
public class GroundDive extends Superpower {
|
||||||
|
|
||||||
public GroundDive() {
|
public GroundDive() {
|
||||||
@@ -100,32 +100,30 @@ public class GroundDive extends Superpower {
|
|||||||
|
|
||||||
Finally, here are the superpowers in active.
|
Finally, here are the superpowers in active.
|
||||||
|
|
||||||
```
|
```java
|
||||||
|
public static void main(String[] args) {
|
||||||
LOGGER.info("Use superpower: sky launch");
|
LOGGER.info("Use superpower: sky launch");
|
||||||
var skyLaunch = new SkyLaunch();
|
var skyLaunch = new SkyLaunch();
|
||||||
skyLaunch.activate();
|
skyLaunch.activate();
|
||||||
LOGGER.info("Use superpower: ground dive");
|
LOGGER.info("Use superpower: ground dive");
|
||||||
var groundDive = new GroundDive();
|
var groundDive = new GroundDive();
|
||||||
groundDive.activate();
|
groundDive.activate();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Program output:
|
Program output:
|
||||||
|
|
||||||
```
|
```
|
||||||
// Use superpower: sky launch
|
13:10:23.177 [main] INFO com.iluwatar.subclasssandbox.App -- Use superpower: sky launch
|
||||||
// Move to ( 0.0, 0.0, 20.0 )
|
13:10:23.179 [main] INFO com.iluwatar.subclasssandbox.SkyLaunch -- Move to ( 0.0, 0.0, 20.0 )
|
||||||
// Play SKYLAUNCH_SOUND with volume 1
|
13:10:23.180 [main] INFO com.iluwatar.subclasssandbox.SkyLaunch -- Play SKYLAUNCH_SOUND with volume 1
|
||||||
// Spawn 100 particle with type SKYLAUNCH_PARTICLE
|
13:10:23.180 [main] INFO com.iluwatar.subclasssandbox.SkyLaunch -- Spawn 100 particle with type SKYLAUNCH_PARTICLE
|
||||||
// Use superpower: ground dive
|
13:10:23.180 [main] INFO com.iluwatar.subclasssandbox.App -- Use superpower: ground dive
|
||||||
// Move to ( 0.0, 0.0, -20.0 )
|
13:10:23.180 [main] INFO com.iluwatar.subclasssandbox.GroundDive -- Move to ( 0.0, 0.0, -20.0 )
|
||||||
// Play GROUNDDIVE_SOUND with volume 5
|
13:10:23.180 [main] INFO com.iluwatar.subclasssandbox.GroundDive -- Play GROUNDDIVE_SOUND with volume 5
|
||||||
// Spawn 20 particle with type GROUNDDIVE_PARTICLE
|
13:10:23.180 [main] INFO com.iluwatar.subclasssandbox.GroundDive -- Spawn 20 particle with type GROUNDDIVE_PARTICLE
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
* Use when you want to create a framework that allows users to define their own behaviors by extending classes.
|
* Use when you want to create a framework that allows users to define their own behaviors by extending classes.
|
||||||
@@ -153,12 +151,12 @@ Trade-offs:
|
|||||||
|
|
||||||
## Related Patterns
|
## Related Patterns
|
||||||
|
|
||||||
* [Template Method](https://java-design-patterns.com/patterns/template-method/): Similar in enforcing a structure where certain steps can be overridden by subclasses.
|
|
||||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Both involve interchangeable behaviors, but Strategy pattern uses composition over inheritance.
|
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Both involve interchangeable behaviors, but Strategy pattern uses composition over inheritance.
|
||||||
|
* [Template Method](https://java-design-patterns.com/patterns/template-method/): Similar in enforcing a structure where certain steps can be overridden by subclasses.
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||||
* [Effective Java](https://amzn.to/4cGk2Jz)
|
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||||
* [Game Programming Patterns](https://amzn.to/3K96fOn)
|
* [Game Programming Patterns](https://amzn.to/3K96fOn)
|
||||||
* [Subclass Sandbox - Game Programming Patterns](https://gameprogrammingpatterns.com/subclass-sandbox.html)
|
* [Subclass Sandbox (Game Programming Patterns)](https://gameprogrammingpatterns.com/subclass-sandbox.html)
|
||||||
Reference in new issue
Block a user