mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-18 17:26:44 +00:00
* Added Lockable-Object pattern. Closes #1282. * Refactor method name. * Refactor sonar lint bugs. * Added tests and enum Constants. * Increase coverage. * Changed @Data to Getters and Setters. * Iluwatar's comment on pull request #1702. * Fixed codes mells. * Incremented wait time to 3 seconds. * Reduced wait time to 2 seconds. * Cleaned Code Smells. * Incremented wait time, removed cool down. * Refactored README.md file. Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package com.iluwatar.lockableobject.domain;
|
||||
|
||||
import com.iluwatar.lockableobject.Lockable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* An abstract class of a creature that wanders across the wasteland. It can attack, get hit and
|
||||
* acquire a Lockable object.
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Slf4j
|
||||
public abstract class Creature {
|
||||
|
||||
private String name;
|
||||
private CreatureType type;
|
||||
private int health;
|
||||
private int damage;
|
||||
Set<Lockable> instruments;
|
||||
|
||||
protected Creature(@NonNull String name) {
|
||||
this.name = name;
|
||||
this.instruments = new HashSet<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reaches for the Lockable and tried to hold it.
|
||||
*
|
||||
* @param lockable as the Lockable to lock.
|
||||
* @return true of Lockable was locked by this creature.
|
||||
*/
|
||||
public boolean acquire(@NonNull Lockable lockable) {
|
||||
if (lockable.lock(this)) {
|
||||
instruments.add(lockable);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Terminates the Creature and unlocks all of the Lockable that it posses. */
|
||||
public synchronized void kill() {
|
||||
LOGGER.info("{} {} has been slayed!", type, name);
|
||||
for (Lockable lockable : instruments) {
|
||||
lockable.unlock(this);
|
||||
}
|
||||
this.instruments.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attacks a foe.
|
||||
*
|
||||
* @param creature as the foe to be attacked.
|
||||
*/
|
||||
public synchronized void attack(@NonNull Creature creature) {
|
||||
creature.hit(getDamage());
|
||||
}
|
||||
|
||||
/**
|
||||
* When a creature gets hit it removed the amount of damage from the creature's life.
|
||||
*
|
||||
* @param damage as the damage that was taken.
|
||||
*/
|
||||
public synchronized void hit(int damage) {
|
||||
if (damage < 0) {
|
||||
throw new IllegalArgumentException("Damage cannot be a negative number");
|
||||
}
|
||||
if (isAlive()) {
|
||||
setHealth(getHealth() - damage);
|
||||
if (!isAlive()) {
|
||||
kill();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the creature is still alive.
|
||||
*
|
||||
* @return true of creature is alive.
|
||||
*/
|
||||
public synchronized boolean isAlive() {
|
||||
return getHealth() > 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.iluwatar.lockableobject.domain;
|
||||
|
||||
/** Attribute constants of each Creature implementation. */
|
||||
public enum CreatureStats {
|
||||
ELF_HEALTH(90),
|
||||
ELF_DAMAGE(40),
|
||||
ORC_HEALTH(70),
|
||||
ORC_DAMAGE(50),
|
||||
HUMAN_HEALTH(60),
|
||||
HUMAN_DAMAGE(60);
|
||||
|
||||
int value;
|
||||
|
||||
private CreatureStats(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.iluwatar.lockableobject.domain;
|
||||
|
||||
/** Constants of supported creatures. */
|
||||
public enum CreatureType {
|
||||
ORC,
|
||||
HUMAN,
|
||||
ELF
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.iluwatar.lockableobject.domain;
|
||||
|
||||
/** An Elf implementation of a Creature. */
|
||||
public class Elf extends Creature {
|
||||
|
||||
/**
|
||||
* A constructor that initializes the attributes of an elf.
|
||||
*
|
||||
* @param name as the name of the creature.
|
||||
*/
|
||||
public Elf(String name) {
|
||||
super(name);
|
||||
setType(CreatureType.ELF);
|
||||
setDamage(CreatureStats.ELF_DAMAGE.getValue());
|
||||
setHealth(CreatureStats.ELF_HEALTH.getValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.iluwatar.lockableobject.domain;
|
||||
|
||||
import com.iluwatar.lockableobject.Lockable;
|
||||
import java.security.SecureRandom;
|
||||
import lombok.NonNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/** A Feind is a creature that all it wants is to posses a Lockable object. */
|
||||
public class Feind implements Runnable {
|
||||
|
||||
private final Creature creature;
|
||||
private final Lockable target;
|
||||
private final SecureRandom random;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Feind.class.getName());
|
||||
|
||||
/**
|
||||
* public constructor.
|
||||
*
|
||||
* @param feind as the creature to lock to he lockable.
|
||||
* @param target as the target object.
|
||||
*/
|
||||
public Feind(@NonNull Creature feind, @NonNull Lockable target) {
|
||||
this.creature = feind;
|
||||
this.target = target;
|
||||
this.random = new SecureRandom();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!creature.acquire(target)) {
|
||||
try {
|
||||
fightForTheSword(creature, target.getLocker(), target);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
} else {
|
||||
LOGGER.info("{} has acquired the sword!", target.getLocker().getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps on fighting until the Lockable is possessed.
|
||||
*
|
||||
* @param reacher as the source creature.
|
||||
* @param holder as the foe.
|
||||
* @param sword as the Lockable to posses.
|
||||
* @throws InterruptedException in case of interruption.
|
||||
*/
|
||||
private void fightForTheSword(Creature reacher, @NonNull Creature holder, Lockable sword)
|
||||
throws InterruptedException {
|
||||
LOGGER.info("A duel between {} and {} has been started!", reacher.getName(), holder.getName());
|
||||
boolean randBool;
|
||||
while (this.target.isLocked() && reacher.isAlive() && holder.isAlive()) {
|
||||
randBool = random.nextBoolean();
|
||||
if (randBool) {
|
||||
reacher.attack(holder);
|
||||
} else {
|
||||
holder.attack(reacher);
|
||||
}
|
||||
}
|
||||
if (reacher.isAlive()) {
|
||||
if (!reacher.acquire(sword)) {
|
||||
fightForTheSword(reacher, sword.getLocker(), sword);
|
||||
} else {
|
||||
LOGGER.info("{} has acquired the sword!", reacher.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.iluwatar.lockableobject.domain;
|
||||
|
||||
/** A human implementation of a Creature. */
|
||||
public class Human extends Creature {
|
||||
|
||||
/**
|
||||
* A constructor that initializes the attributes of an human.
|
||||
*
|
||||
* @param name as the name of the creature.
|
||||
*/
|
||||
public Human(String name) {
|
||||
super(name);
|
||||
setType(CreatureType.HUMAN);
|
||||
setDamage(CreatureStats.HUMAN_DAMAGE.getValue());
|
||||
setHealth(CreatureStats.HUMAN_HEALTH.getValue());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.iluwatar.lockableobject.domain;
|
||||
|
||||
/** A Orc implementation of a Creature. */
|
||||
public class Orc extends Creature {
|
||||
/**
|
||||
* A constructor that initializes the attributes of an orc.
|
||||
*
|
||||
* @param name as the name of the creature.
|
||||
*/
|
||||
public Orc(String name) {
|
||||
super(name);
|
||||
setType(CreatureType.ORC);
|
||||
setDamage(CreatureStats.ORC_DAMAGE.getValue());
|
||||
setHealth(CreatureStats.ORC_HEALTH.getValue());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user