OOP - FIIT STU

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é

  1. 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.
  2. 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.

PatternWhat you will learn
StrategyEncapsulate interchangeable algorithms behind a common interface; switch behaviour at runtime without modifying the client code
ObserverEstablish a one-to-many dependency so that when an event occurs, all interested parties are notified automatically
SingletonGuarantee 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:

FileStatusDescription
GameCharacter.javaImplemented + TODOsBase class — strategy integration methods marked TODO
Weapon.javaFully implementedWeapon class from Task 6
Warrior.javaFully implementedSubclass from Task 6
Mage.javaFully implementedSubclass from Task 6
Archer.javaFully implementedSubclass from Task 6
AttackStrategy.javaComplete interfaceStrategy pattern — no changes needed
MeleeAttackStrategy.javaSkeleton — TODOConcrete strategy
RangedAttackStrategy.javaSkeleton — TODOConcrete strategy
MagicAttackStrategy.javaSkeleton — TODOConcrete strategy
CombatEventListener.javaComplete interfaceObserver pattern — no changes needed
CombatEventManager.javaSkeleton — TODOSubject (manages observers)
BattleLogger.javaSkeleton — TODOConcrete observer
GameWorld.javaSkeleton — TODOSingleton

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); // true

Submission

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.

On this page