Skip to content

Commit 3eb27d1

Browse files
authored
Implement the Structural Design Patterns (#5)
* Implement the Adapter Design Pattern * Implement the Bridge Design Pattern * Implement the Composite Design Pattern * Implement the Decorator Design Pattern * Implement the Facade Design Pattern * Implement the Flyweight Design Pattern * Implement the Proxy Design Pattern
1 parent 017e04d commit 3eb27d1

Some content is hidden

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

41 files changed

+857
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package practice.structural.adapter;
2+
3+
/**
4+
* Example of Adapter Design Pattern
5+
*/
6+
public final class AdapterDemo {
7+
8+
public static void main(final String... arguments) {
9+
10+
new Olympian(new BoatAdapter()).rowBoat(); // Play
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package practice.structural.adapter;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.ToString;
5+
6+
@ToString
7+
@EqualsAndHashCode
8+
public final class BoatAdapter implements RowingBoat {
9+
10+
private final FishingBoat boat = new FishingBoat();
11+
12+
@Override
13+
public void row() {
14+
boat.sail();
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package practice.structural.adapter;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.extern.java.Log;
5+
6+
@Log
7+
@EqualsAndHashCode
8+
public final class FishingBoat {
9+
10+
public void sail() {
11+
log.info("Sailing BOAT => [" + this + "]");
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package practice.structural.adapter;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.NonNull;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.java.Log;
7+
8+
@Log
9+
@EqualsAndHashCode
10+
@RequiredArgsConstructor
11+
public final class Olympian {
12+
13+
@NonNull
14+
private final RowingBoat boat;
15+
16+
public void rowBoat() {
17+
log.info("ATHLETE in Rowing => [" + this + "]");
18+
boat.row(); // ROW currently AVAILABLE type of BOAT
19+
}
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package practice.structural.adapter;
2+
3+
public sealed interface RowingBoat permits BoatAdapter {
4+
5+
void row(); // Implementation of TECHNIQUES of ROWING
6+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package practice.structural.bridge;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.ToString;
5+
import lombok.extern.java.Log;
6+
7+
@Log
8+
@EqualsAndHashCode
9+
@ToString(callSuper = true)
10+
public final class BleedEffect implements Enchantment {
11+
12+
@Override
13+
public void activate() {
14+
log.info("ACTIVATED Enchantment => [" + this + "]");
15+
}
16+
17+
@Override
18+
public void apply() {
19+
log.info("APPLIED Enchantment => [" + this + "]");
20+
}
21+
22+
@Override
23+
public void deactivate() {
24+
log.info("DEACTIVATED Enchantment => [" + this + "]");
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package practice.structural.bridge;
2+
3+
/**
4+
* Example of Bridge Design Pattern
5+
*/
6+
public final class BridgeDemo {
7+
8+
public static void main(final String... arguments) {
9+
10+
final var specialDagger = new Dagger(new PoisonEffect());
11+
12+
specialDagger.wield(); // WIELD `Dagger` with `PoisonEffect`
13+
specialDagger.use(); // USING `Dagger` with `PoisonEffect`
14+
specialDagger.sheath(); // SHEATH `Dagger` with `PoisonEffect`
15+
16+
final var specialHammer = new Hammer(new BleedEffect());
17+
18+
specialHammer.wield(); // WIELD `Hammer` with `BleedEffect`
19+
specialHammer.use(); // USING `Hammer` with `BleedEffect`
20+
specialHammer.sheath(); // SHEATH `Hammer` with `BleedEffect`
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package practice.structural.bridge;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.NonNull;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.ToString;
7+
import lombok.extern.java.Log;
8+
9+
@Log
10+
@ToString
11+
@EqualsAndHashCode
12+
@RequiredArgsConstructor
13+
public final class Dagger implements Weaponry {
14+
15+
@NonNull
16+
private final Enchantment enchantment;
17+
18+
@Override
19+
public void wield() {
20+
log.info("WIELDED Weapon => [" + this + "]");
21+
enchantment.activate(); // ACTIVATE current Spell
22+
}
23+
24+
@Override
25+
public void use() {
26+
log.info("USING Weapon => [" + this + "]");
27+
enchantment.apply(); // CAST this current Spell
28+
}
29+
30+
@Override
31+
public void sheath() {
32+
log.info("SHEATHED Weapon => [" + this + "]");
33+
enchantment.deactivate(); // DEACTIVATE this Spell
34+
}
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package practice.structural.bridge;
2+
3+
public sealed interface Enchantment permits BleedEffect, PoisonEffect {
4+
5+
void activate(); // Abstraction of ACTIVATING Enchantment
6+
7+
void apply(); // Abstraction of APPLYING Enchantment
8+
9+
void deactivate(); // Abstraction of DEACTIVATING Enchantment
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package practice.structural.bridge;
2+
3+
import lombok.EqualsAndHashCode;
4+
import lombok.NonNull;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.ToString;
7+
import lombok.extern.java.Log;
8+
9+
@Log
10+
@ToString
11+
@EqualsAndHashCode
12+
@RequiredArgsConstructor
13+
public final class Hammer implements Weaponry {
14+
15+
@NonNull
16+
private final Enchantment enchantment;
17+
18+
@Override
19+
public void wield() {
20+
log.info("WIELDED Weapon => [" + this + "]");
21+
enchantment.activate(); // ACTIVATE current Spell
22+
}
23+
24+
@Override
25+
public void use() {
26+
log.info("USING Weapon => [" + this + "]");
27+
enchantment.apply(); // CAST this current Spell
28+
}
29+
30+
@Override
31+
public void sheath() {
32+
log.info("SHEATHED Weapon => [" + this + "]");
33+
enchantment.deactivate(); // DEACTIVATE this Spell
34+
}
35+
}

0 commit comments

Comments
 (0)