added Property pattern
This commit is contained in:
49
property/src/main/java/com/iluwatar/App.java
Normal file
49
property/src/main/java/com/iluwatar/App.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import com.iluwatar.Character.Type;
|
||||
|
||||
/**
|
||||
* Example of Character instantiation using Property pattern (as concept also known like Prototype inheritance).
|
||||
* In prototype inheritance instead of classes, as opposite to Java class inheritance,
|
||||
* objects are used to create another objects and object hierarchies.
|
||||
* Hierarchies are created using prototype chain through delegation: every object has link to parent object.
|
||||
* Any base (parent) object can be amended at runtime (by adding or removal of some property), and all child objects will be affected as result.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* set up */
|
||||
Prototype charProto = new Character();
|
||||
charProto.set(Stats.STRENGTH, 10);
|
||||
charProto.set(Stats.AGILITY, 10);
|
||||
charProto.set(Stats.ARMOR, 10);
|
||||
charProto.set(Stats.ATTACK_POWER, 10);
|
||||
|
||||
Character mageProto = new Character(Type.MAGE, charProto);
|
||||
mageProto.set(Stats.INTELLECT, 15);
|
||||
mageProto.set(Stats.SPIRIT, 10);
|
||||
|
||||
Character warProto = new Character(Type.WARRIOR, charProto);
|
||||
warProto.set(Stats.RAGE, 15);
|
||||
warProto.set(Stats.ARMOR, 15); // boost default armor for warrior
|
||||
|
||||
Character rogueProto = new Character(Type.ROGUE, charProto);
|
||||
rogueProto.set(Stats.ENERGY, 15);
|
||||
rogueProto.set(Stats.AGILITY, 15); // boost default agility for rogue
|
||||
|
||||
/* usage */
|
||||
Character mag = new Character("Player_1", mageProto);
|
||||
mag.set(Stats.ARMOR, 8);
|
||||
System.out.println(mag);
|
||||
|
||||
Character warrior = new Character("Player_2", warProto);
|
||||
System.out.println(warrior);
|
||||
|
||||
Character rogue = new Character("Player_3", rogueProto);
|
||||
System.out.println(rogue);
|
||||
|
||||
Character rogueDouble = new Character("Player_4", rogue);
|
||||
rogueDouble.set(Stats.ATTACK_POWER, 12);
|
||||
System.out.println(rogueDouble);
|
||||
}
|
||||
}
|
117
property/src/main/java/com/iluwatar/Character.java
Normal file
117
property/src/main/java/com/iluwatar/Character.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Represents Character in game and his abilities (base stats).
|
||||
*/
|
||||
public class Character implements Prototype {
|
||||
|
||||
public enum Type {
|
||||
WARRIOR, MAGE, ROGUE
|
||||
}
|
||||
|
||||
private final Prototype prototype;
|
||||
private final Map<Stats, Integer> properties = new HashMap<>();
|
||||
|
||||
private String name;
|
||||
private Type type;
|
||||
|
||||
public Character() {
|
||||
this.prototype = new Prototype() { // Null-value object
|
||||
@Override
|
||||
public Integer get(Stats stat) {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public boolean has(Stats stat) {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public void set(Stats stat, Integer val) {
|
||||
}
|
||||
@Override
|
||||
public void remove(Stats stat) {
|
||||
}}
|
||||
;
|
||||
}
|
||||
|
||||
public Character(Type type, Prototype prototype) {
|
||||
this.type = type;
|
||||
this.prototype = prototype;
|
||||
}
|
||||
|
||||
public Character(String name, Character prototype) {
|
||||
this.name = name;
|
||||
this.type = prototype.type;
|
||||
this.prototype = prototype;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Type type() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer get(Stats stat) {
|
||||
boolean containsValue = properties.containsKey(stat);
|
||||
if (containsValue) {
|
||||
return properties.get(stat);
|
||||
} else {
|
||||
return prototype.get(stat);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(Stats stat) {
|
||||
return get(stat) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(Stats stat, Integer val) {
|
||||
properties.put(stat, val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Stats stat) {
|
||||
properties.put(stat, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
if (name != null) {
|
||||
builder
|
||||
.append("Player: ")
|
||||
.append(name)
|
||||
.append("\n");
|
||||
}
|
||||
|
||||
if (type != null) {
|
||||
builder
|
||||
.append("Character type: ")
|
||||
.append(type.name())
|
||||
.append("\n");
|
||||
}
|
||||
|
||||
builder.append("Stats:\n");
|
||||
for (Stats stat : Stats.values()) {
|
||||
Integer value = this.get(stat);
|
||||
if (value == null) {
|
||||
continue;
|
||||
}
|
||||
builder
|
||||
.append(" - ")
|
||||
.append(stat.name())
|
||||
.append(":")
|
||||
.append(value)
|
||||
.append("\n");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
12
property/src/main/java/com/iluwatar/Prototype.java
Normal file
12
property/src/main/java/com/iluwatar/Prototype.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
* Interface for prototype inheritance
|
||||
*/
|
||||
public interface Prototype {
|
||||
|
||||
public Integer get(Stats stat);
|
||||
public boolean has(Stats stat);
|
||||
public void set(Stats stat, Integer val);
|
||||
public void remove(Stats stat);
|
||||
}
|
9
property/src/main/java/com/iluwatar/Stats.java
Normal file
9
property/src/main/java/com/iluwatar/Stats.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
* All possible attributes that Character can have
|
||||
*/
|
||||
public enum Stats {
|
||||
|
||||
AGILITY, STRENGTH, ATTACK_POWER, ARMOR, INTELLECT, SPIRIT, ENERGY, RAGE
|
||||
}
|
12
property/src/test/java/com/iluwatar/AppTest.java
Normal file
12
property/src/test/java/com/iluwatar/AppTest.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.iluwatar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user