Cvičenie 3
Návrhové vzory - Strategy. Observer. Singleton.
Cvičenie 3 - Návrhové vzory
Pred cvičením
- Príďte pripravení. V nedeľu sa odovzdáva 1. zadanie. Ak máte otázky alebo si nieste niečim istí, toto cvičenie je ideálnou príležitosťou na diskusiu a získanie spätnej väzby.
- Oboznámte sa s prednáškou 3. Cvičenie 3 je o návrhových vzoroch. Oboznámte sa aj s katalógom návrhových vzorov, ktorý nájdete v materiáloch.
Organizačné
- Uistite sa, že ste so svojím cvičiacim skonzultovali vašu hru, žáner a rozsah. V rámci cvičenia nemôžete mať rovnakú hru ako kolega z cvičenia. Prípadne sa podľa pokynov cvičiaceho dohodnite ako budete riešiť tento problém.
- Od budúceho cvičenia začíname s implementáciou. Od 09.03.2026 sa začínajú počítať commity do github repozitára. Uistite sa, že máte založený repozitár a že viete, ako s ním pracovať. Komunikujte problémy s commitovaním s cvičiacim včas.
Termín
1. zadanie (špecifikácia) - 08.03.2026 23:59
Úlohy Java
Download the skeleton files
Task 7 — Design Patterns: Strategy, Observer & Singleton
Extending the RPG Character System from Task 6
In this task you will extend the RPG character system with three fundamental design patterns.
The base classes (GameCharacter, Weapon, Warrior, Mage, Archer) are provided fully
implemented — your job is to add the patterns on top.
| Pattern | What you will learn |
|---|---|
| Strategy | Encapsulate interchangeable algorithms behind a common interface; switch behaviour at runtime without modifying the client code |
| Observer | Establish a one-to-many dependency so that when an event occurs, all interested parties are notified automatically |
| Singleton | Guarantee a class has exactly one instance and provide a global access point to it |
Provided files
All files live in package org.jikvict.tasks.exposed:
| File | Status | Description |
|---|---|---|
GameCharacter.java | Implemented + TODOs | Base class — strategy integration methods marked TODO |
Weapon.java | Fully implemented | Weapon class from Task 6 |
Warrior.java | Fully implemented | Subclass from Task 6 |
Mage.java | Fully implemented | Subclass from Task 6 |
Archer.java | Fully implemented | Subclass from Task 6 |
AttackStrategy.java | Complete interface | Strategy pattern — no changes needed |
MeleeAttackStrategy.java | Skeleton — TODO | Concrete strategy |
RangedAttackStrategy.java | Skeleton — TODO | Concrete strategy |
MagicAttackStrategy.java | Skeleton — TODO | Concrete strategy |
CombatEventListener.java | Complete interface | Observer pattern — no changes needed |
CombatEventManager.java | Skeleton — TODO | Subject (manages observers) |
BattleLogger.java | Skeleton — TODO | Concrete observer |
GameWorld.java | Skeleton — TODO | Singleton |
Do not change method signatures or access modifiers that are already present. You may add private helper methods if you need them.
What you must implement
Quick example
// ── Strategy ──
GameCharacter warrior = new Warrior("Arthur", 100, 10, 6);
warrior.setAttackStrategy(new MeleeAttackStrategy());
System.out.println(warrior.executeStrategy()); // 15 ((int)(10 * 1.5))
warrior.setAttackStrategy(new MagicAttackStrategy());
System.out.println(warrior.executeStrategy()); // 30 (10 * 3)
// ── Observer ──
CombatEventManager manager = new CombatEventManager();
BattleLogger logger = new BattleLogger();
manager.addListener(logger);
manager.notifyDamageDealt("Arthur", "Goblin", 15);
System.out.println(logger.getLog().get(0));
// "DAMAGE: Arthur dealt 15 damage to Goblin"
// ── Singleton ──
GameWorld world = GameWorld.getInstance();
world.addCharacter(warrior);
System.out.println(world.getParty().size()); // 1
System.out.println(GameWorld.getInstance() == world); // trueSubmission
Take your whole project, remove unnecessary files (like .idea folder or build folder) and zip it.
The final zip file should contain the default-structure folder at the root.