|
| 1 | +/* |
| 2 | +Reference: https://www.geeksforgeeks.org/flyweight-design-pattern/ |
| 3 | + */ |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +// A Java program to demonstrate working of FlyWeight Pattern with example of VAlorant Game |
| 8 | +import java.util.Random; |
| 9 | +import java.util.HashMap; |
| 10 | + |
| 11 | +// A common interface for all Agents |
| 12 | +interface Agent |
| 13 | +{ |
| 14 | + public void assignWeapon(String weapon); |
| 15 | + public void mission(); |
| 16 | +} |
| 17 | + |
| 18 | +// An Attacker must have weapon and mission |
| 19 | +class Attacker implements Agent |
| 20 | +{ |
| 21 | + // Intrinsic Attribute |
| 22 | + private final String TASK; |
| 23 | + |
| 24 | + // Extrinsic Attribute |
| 25 | + private String weapon; |
| 26 | + |
| 27 | + public Attacker() |
| 28 | + { |
| 29 | + TASK = "PLANT A Spike"; |
| 30 | + } |
| 31 | + public void assignWeapon(String weapon) |
| 32 | + { |
| 33 | + // Assign a weapon |
| 34 | + this.weapon = weapon; |
| 35 | + } |
| 36 | + public void mission() |
| 37 | + { |
| 38 | + //Work on the Mission |
| 39 | + System.out.println("Attackers with weapon " |
| 40 | + + weapon + "|" + " Task is " + TASK); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// A Defender must have weapon and mission just like an Attacker |
| 45 | +class Defender implements Agent |
| 46 | +{ |
| 47 | + // Intrinsic Attribute |
| 48 | + private final String TASK; |
| 49 | + |
| 50 | + // Extrinsic Attribute |
| 51 | + private String weapon; |
| 52 | + |
| 53 | + public Defender() |
| 54 | + { |
| 55 | + TASK = "DIFFUSE Spike"; |
| 56 | + } |
| 57 | + public void assignWeapon(String weapon) |
| 58 | + { |
| 59 | + this.weapon = weapon; |
| 60 | + } |
| 61 | + public void mission() |
| 62 | + { |
| 63 | + System.out.println("Defenders with weapon " |
| 64 | + + weapon + "|" + " Task is " + TASK); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Class used to get an Agent using HashMap (Returns) an existing Agent if an Agent of given type exists. |
| 69 | +// Else creates a new Agent and returns it. |
| 70 | +class HavenMap |
| 71 | +{ |
| 72 | + /* HashMap stores the reference to the object |
| 73 | + of Attackers or Defender */ |
| 74 | + private static HashMap <String, Agent> hm = |
| 75 | + new HashMap<String, Agent>(); |
| 76 | + |
| 77 | + // Method to get an Agent |
| 78 | + public static Agent getAgent(String type) |
| 79 | + { |
| 80 | + Agent p = null; |
| 81 | + |
| 82 | + /* If an object for Attackers or Defender has already been |
| 83 | + created simply return its reference */ |
| 84 | + if (hm.containsKey(type)) |
| 85 | + p = hm.get(type); |
| 86 | + else |
| 87 | + { |
| 88 | + /* create an object of Attackers or Defender */ |
| 89 | + switch(type) |
| 90 | + { |
| 91 | + case "Attacker": |
| 92 | + System.out.println("Attackers Created"); |
| 93 | + p = new Attacker(); |
| 94 | + break; |
| 95 | + case "Defender": |
| 96 | + System.out.println("Defenders Created"); |
| 97 | + p = new Defender(); |
| 98 | + break; |
| 99 | + default : |
| 100 | + System.out.println("Unreachable code!"); |
| 101 | + } |
| 102 | + |
| 103 | + // Once created insert it into the HashMap |
| 104 | + hm.put(type, p); |
| 105 | + } |
| 106 | + return p; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Driver class |
| 111 | +public class Valorant |
| 112 | +{ |
| 113 | + // All Agent types and weapon (used by getRandAgentType() |
| 114 | + // and getRandWeapon() |
| 115 | + private static String[] AgentType = |
| 116 | + {"Attacker", "Defender"}; |
| 117 | + private static String[] weapons = |
| 118 | + {"Phantom", "Vandal", "Sheriff","Operator", "Spectre","Ares","Odin"}; |
| 119 | + |
| 120 | + |
| 121 | + // Driver code |
| 122 | + public static void main(String args[]) |
| 123 | + { |
| 124 | + /* Assume that we have a total of 10 Agents |
| 125 | + in the game. */ |
| 126 | + for (int i = 0; i < 10; i++) |
| 127 | + { |
| 128 | + /* getAgent() is called simply using the class |
| 129 | + name since the method is a static one */ |
| 130 | + Agent p = HavenMap.getAgent(getRandAgentType()); |
| 131 | + |
| 132 | + /* Assign a weapon chosen randomly uniformly |
| 133 | + from the weapon array */ |
| 134 | + p.assignWeapon(getRandWeapon()); |
| 135 | + |
| 136 | + // Send this Agent on a mission |
| 137 | + p.mission(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // Utility methods to get a random Agent type and |
| 142 | + // weapon |
| 143 | + public static String getRandAgentType() |
| 144 | + { |
| 145 | + Random r = new Random(); |
| 146 | + |
| 147 | + // Will return an integer between [0,2) |
| 148 | + int randInt = r.nextInt(AgentType.length); |
| 149 | + |
| 150 | + // return the Agent stored at index 'randInt' |
| 151 | + return AgentType[randInt]; |
| 152 | + } |
| 153 | + public static String getRandWeapon() |
| 154 | + { |
| 155 | + Random r = new Random(); |
| 156 | + |
| 157 | + // Will return an integer between [0,5) |
| 158 | + int randInt = r.nextInt(weapons.length); |
| 159 | + |
| 160 | + // Return the weapon stored at index 'randInt' |
| 161 | + return weapons[randInt]; |
| 162 | + } |
| 163 | +} |
| 164 | + |
0 commit comments