From 803a2624f6249cbf7ae4322e5d7b0a05dea52084 Mon Sep 17 00:00:00 2001 From: jnniu-n <141746282+jnniu-n@users.noreply.github.com> Date: Sat, 10 Feb 2024 19:54:02 +1100 Subject: [PATCH] docs: Update property pattern README.md (#2745) Finish Readme.md for Property --- property/README.md | 108 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/property/README.md b/property/README.md index 624c1e7d7..19fe9c90e 100644 --- a/property/README.md +++ b/property/README.md @@ -10,6 +10,114 @@ tag: Create hierarchy of objects and new objects using already existing objects as parents. +## Explanation + +Real-world example + +> In the mystical land of "Elandria", adventurers can harness the power of ancient relics to customize their abilities. Each relic represents a unique property or skill. As adventurers explore, they discover and integrate new relics, dynamically enhancing their skills based on the relics they possess. + +> Consider a modern software used in designing and customizing smartphones. Designers can choose from a variety of components such as processor type, camera specs, battery capacity, and more. Each component represents a property of the smartphone. As technology evolves and new components become available, designers can seamlessly add or replace properties to create a unique smartphone configuration without redefining the core design structure. + +In plain words + +> Define and manage a dynamic set of properties for an object, allowing customization without altering its structure. + +**Programmatic Example** +```java +import java.util.HashMap; +import java.util.Map; + +// Enumeration for possible properties or statistics a character can have +enum Stats { + AGILITY, ATTACK_POWER, ARMOR, INTELLECT, SPIRIT, FURY, RAGE; +} + +// Enumeration for different types or classes of characters +enum Type { + WARRIOR, MAGE, ROGUE; +} + +// Interface defining prototype operations on a character +interface Prototype { + Integer get(Stats stat); + boolean has(Stats stat); + void set(Stats stat, Integer value); + void remove(Stats stat); +} + +// Implementation of the Character class +class Character implements Prototype { + private String name; + private Type type; + private Map properties = new HashMap<>(); + + public Character() {} + + public Character(Type type, Prototype prototype) { + this.type = type; + for (Stats stat : Stats.values()) { + if (prototype.has(stat)) { + this.set(stat, prototype.get(stat)); + } + } + } + + public Character(String name, Type type) { + this.name = name; + this.type = type; + } + + @Override + public Integer get(Stats stat) { + return properties.get(stat); + } + + @Override + public boolean has(Stats stat) { + return properties.containsKey(stat); + } + + @Override + public void set(Stats stat, Integer value) { + properties.put(stat, value); + } + + @Override + public void remove(Stats stat) { + properties.remove(stat); + } + + @Override + public String toString() { + return "Character{name='" + name + "', type=" + type + ", properties=" + properties + '}'; + } +} + +// Main class to demonstrate the pattern +public class PropertyPatternDemo { + public static void main(String[] args) { + // Create a prototype character + Character prototypeWarrior = new Character("Proto Warrior", Type.WARRIOR); + prototypeWarrior.set(Stats.ATTACK_POWER, 10); + prototypeWarrior.set(Stats.ARMOR, 15); + + // Create a new character using the prototype + Character newWarrior = new Character(Type.WARRIOR, prototypeWarrior); + newWarrior.set(Stats.AGILITY, 5); + + System.out.println(prototypeWarrior); + System.out.println(newWarrior); + } +} +``` + +Program output: + +``` +Character{name='Proto Warrior', type=WARRIOR, properties={ARMOR=15, ATTACK_POWER=10}} +Character{name='null', type=WARRIOR, properties={ARMOR=15, AGILITY=5, ATTACK_POWER=10}} +``` + ## Class diagram ![alt text](./etc/property.png "Property")