Skip to content

Commit dddccf9

Browse files
authored
Merge pull request dubesar#591 from cdhiraj40/FlyweightPattern
added Flyweight Pattern
2 parents 31404a2 + 1983e5e commit dddccf9

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Flyweight Design Pattern
2+
3+
### Introduction
4+
5+
Flyweight pattern is one of the structural design patterns as this pattern provides ways to decrease object count thus improving application required objects structure. Flyweight pattern is used when we need to create a large number of similar objects (say 105). One important feature of flyweight objects is that they are immutable. This means that they cannot be modified once they have been constructed.
6+
7+
In Flyweight pattern we use a HashMap that stores reference to the object which have already been created, every object is associated with a key. Now when a client wants to create an object, he simply has to pass a key associated with it and if the object has already been created we simply get the reference to that object else it creates a new object and then returns it reference to the client.
8+
9+
### Advantages
10+
11+
- It reduces the number of objects.
12+
- It reduces the amount of memory and storage devices required if the objects are persisted.
13+
14+
### Disadvantages
15+
16+
- Reduce memory usage by sharing heavy objects.
17+
- Improved data caching for higher response time.
18+
- Increased performance due to a lesser number of heavy objects.
19+
20+
### Implementation
21+
22+
We implement the creation of Attackers and Defenders In the game of Valorant. So we have 2 classes one for Attackers and other for Defenders. Whenever an Agent asks for a weapon we assign him/her/them the asked weapon. In the mission, Attacker's task is to plant a Spike while the Defenders have to diffuse the Spike.

0 commit comments

Comments
 (0)