https://github.com/piamacalanda/softeng2_strategy-pattern-gameapp
Simple GameApp assigning character's skillset🧙🦸♀️🥷 using Strategy Design Pattern in java.
https://github.com/piamacalanda/softeng2_strategy-pattern-gameapp
strategy-design-pattern
Last synced: 4 months ago
JSON representation
Simple GameApp assigning character's skillset🧙🦸♀️🥷 using Strategy Design Pattern in java.
- Host: GitHub
- URL: https://github.com/piamacalanda/softeng2_strategy-pattern-gameapp
- Owner: PiaMacalanda
- Created: 2025-02-05T05:57:52.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-06T13:44:26.000Z (11 months ago)
- Last Synced: 2025-02-06T14:34:12.719Z (11 months ago)
- Topics: strategy-design-pattern
- Homepage:
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GameApp characters and its skills set (Strategy Pattern)
### Problem scenario:
Suppose we have three types of characters in a GameApp:
1. **Knight**: Attacks with a sword; uses 3 strategies to defend (shield, dodge, magic barrier)
2. **Wizard**: Casts spells; uses magic barrier to defend
3. **Archer**: Shoots arrows; uses dodge to to defend
### Implement two types of Strategy:
A. **DefenseStrategy**
1. Shield
2. Dodge
3. CreateMagic
B. **AttackStrategy**
1. CastSpell
2. ShootArrow
3. SwingSword
### Refactor the existing codes:
```java
public class Character {
private String type;
public Character(String type) {
this.type = type;
}
public void attack() {
if (type.equals("Knight")) {
System.out.println("Knight attacks with a sword!");
} else if (type.equals("Wizard")) {
System.out.println("Wizard casts a spell!");
} else if (type.equals("Archer")) {
System.out.println("Archer shoots an arrow!");
}
}
public void defend() {
if (type.equals("Knight")) {
System.out.println("Using a shield to defend!");
System.out.println("Dodgin to avoid attack!");
System.out.println("Creating a magic barrier for defense!"");
} else if (type.equals("Wizard")) {
System.out.println("Creating a magic barrier for defense!"");
} else if (type.equals("Archer")) {
System.out.println("Using a shield to defend!"");
}
}
}
```
### UML Diagram:
