Merge pull request #281 from ankurkaushal/master

Reformat according to google style guide
This commit is contained in:
Ilkka Seppälä
2015-11-02 21:39:17 +02:00
438 changed files with 8257 additions and 8382 deletions

View File

@ -6,53 +6,55 @@ import com.iluwatar.property.Character.Type;
*
* The Property pattern is also known as Prototype inheritance.
* <p>
* 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.
* 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.
* <p>
* In this example we demonstrate {@link Character} instantiation using the Property pattern.
*
*/
public class App {
/**
* Program entry point
* @param args command line args
*/
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);
/**
* Program entry point
*
* @param args command line args
*/
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 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 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
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);
/* 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 warrior = new Character("Player_2", warProto);
System.out.println(warrior);
Character rogue = new Character("Player_3", rogueProto);
System.out.println(rogue);
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);
}
Character rogueDouble = new Character("Player_4", rogue);
rogueDouble.set(Stats.ATTACK_POWER, 12);
System.out.println(rogueDouble);
}
}

View File

@ -8,110 +8,100 @@ import java.util.Map;
*/
public class Character implements Prototype {
public enum Type {
WARRIOR, MAGE, ROGUE
}
public enum Type {
WARRIOR, MAGE, ROGUE
}
private final Prototype prototype;
private final Map<Stats, Integer> properties = new HashMap<>();
private final Prototype prototype;
private final Map<Stats, Integer> properties = new HashMap<>();
private String name;
private Type type;
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() {
this.prototype = new Prototype() { // Null-value object
@Override
public Integer get(Stats stat) {
return null;
}
public Character(Type type, Prototype prototype) {
this.type = type;
this.prototype = prototype;
}
@Override
public boolean has(Stats stat) {
return false;
}
public Character(String name, Character prototype) {
this.name = name;
this.type = prototype.type;
this.prototype = prototype;
}
@Override
public void set(Stats stat, Integer val) {}
public String name() {
return name;
}
@Override
public void remove(Stats stat) {}
};
}
public Type type() {
return type;
}
public Character(Type type, Prototype prototype) {
this.type = type;
this.prototype = prototype;
}
@Override
public Integer get(Stats stat) {
boolean containsValue = properties.containsKey(stat);
if (containsValue) {
return properties.get(stat);
} else {
return prototype.get(stat);
}
}
public Character(String name, Character prototype) {
this.name = name;
this.type = prototype.type;
this.prototype = prototype;
}
@Override
public boolean has(Stats stat) {
return get(stat) != null;
}
public String name() {
return name;
}
@Override
public void set(Stats stat, Integer val) {
properties.put(stat, val);
}
public Type type() {
return type;
}
@Override
public void remove(Stats stat) {
properties.put(stat, null);
}
@Override
public Integer get(Stats stat) {
boolean containsValue = properties.containsKey(stat);
if (containsValue) {
return properties.get(stat);
} else {
return prototype.get(stat);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (name != null) {
builder
.append("Player: ")
.append(name)
.append("\n");
}
@Override
public boolean has(Stats stat) {
return get(stat) != null;
}
if (type != null) {
builder
.append("Character type: ")
.append(type.name())
.append("\n");
}
@Override
public void set(Stats stat, Integer val) {
properties.put(stat, val);
}
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();
}
@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();
}
}

View File

@ -5,8 +5,11 @@ package com.iluwatar.property;
*/
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);
public Integer get(Stats stat);
public boolean has(Stats stat);
public void set(Stats stat, Integer val);
public void remove(Stats stat);
}

View File

@ -5,5 +5,5 @@ package com.iluwatar.property;
*/
public enum Stats {
AGILITY, STRENGTH, ATTACK_POWER, ARMOR, INTELLECT, SPIRIT, ENERGY, RAGE
AGILITY, STRENGTH, ATTACK_POWER, ARMOR, INTELLECT, SPIRIT, ENERGY, RAGE
}

View File

@ -11,9 +11,9 @@ import com.iluwatar.property.App;
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
@Test
public void test() {
String[] args = {};
App.main(args);
}
}