Skip to content

Commit 76b1764

Browse files
committed
Flyweight design pattern - AFTER CODE with with reusable cached object to reduce memory foot print
1 parent 4539373 commit 76b1764

File tree

3 files changed

+115
-4
lines changed

3 files changed

+115
-4
lines changed

pattern/src/com/premaseem/Client.java

+23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ public class Client {
99
public static void main (String[] args) {
1010
System.out.println("Flyweight design pattern using ** STAR WARS ** example");
1111

12+
// Create Landscape object which is responsible to display stars
13+
Landscape landscape = new Landscape();
14+
15+
// Factory will provide star based on param
16+
// Note: Factory will cache objects for reuse
17+
// and will create new object only when it does not exist in cache.
18+
Star star = StarFactory.getStar("dull");
19+
20+
landscape.displayStar(star,65,87);
21+
22+
// No new object needs to be created, Factory provides reusable object
23+
// from cache which landscape displays
24+
landscape.displayStar(StarFactory.getStar("bright"),34,43);
25+
landscape.displayStar(StarFactory.getStar("bright"),36,47);
26+
landscape.displayStar(StarFactory.getStar("dull"),34,43);
27+
landscape.displayStar(StarFactory.getStar("dim"),34,43);
28+
landscape.displayStar(StarFactory.getStar("dim"),34,43);
29+
30+
31+
32+
33+
/* OLD Scrapped code
1234
// Created 1000 stars with brightness level bright
1335
Star brightStar1 = new Star("bright");
1436
Star brightStar2 = new Star("bright");
@@ -51,5 +73,6 @@ public static void main (String[] args) {
5173
landscape.displayStar(dullStar2,34,45);
5274
landscape.displayStar(dullStar3,23,55);
5375
landscape.displayStar(dullStar1000,34,45);
76+
*/
5477
}
5578
}

pattern/src/com/premaseem/Star.java

+51-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,59 @@
55
@title: Design Patterns with Java 9
66
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
77
*/
8-
public class Star {
8+
public interface Star {
9+
void displayBrightness ();
10+
}
11+
12+
class BrightStar implements Star {
13+
14+
private String brightness;
15+
16+
public BrightStar () {
17+
this.brightness = "bright";
18+
}
19+
20+
@Override
21+
public void displayBrightness () {
22+
System.out.println("Star with brightness :" + brightness);
23+
}
924

10-
String brightness;
25+
@Override
26+
public String toString () {
27+
return "Star with brightness :" + brightness;
28+
}
29+
}
30+
31+
class DimStar implements Star {
1132

12-
public Star (String brightness) {
13-
this.brightness = brightness;
33+
private String brightness;
34+
35+
public DimStar () {
36+
this.brightness = "dim";
37+
}
38+
39+
@Override
40+
public void displayBrightness () {
41+
System.out.println("Star with brightness :" + brightness);
42+
}
43+
44+
@Override
45+
public String toString () {
46+
return "Star with brightness :" + brightness;
47+
}
48+
}
49+
50+
class DullStar implements Star {
51+
52+
private String brightness;
53+
54+
public DullStar () {
55+
this.brightness = "dull";
56+
}
57+
58+
@Override
59+
public void displayBrightness () {
60+
System.out.println("Star with brightness :" + brightness);
1461
}
1562

1663
@Override
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.premaseem;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/*
7+
@author: Aseem Jain
8+
@title: Design Patterns with Java 9
9+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
10+
*/
11+
public class StarFactory {
12+
static Map<String,Star> starCache = new HashMap<>();
13+
14+
public static Star getStar (String brightnessLevel) {
15+
16+
// First try to retrieve object from cache
17+
Star star = starCache.get(brightnessLevel);
18+
19+
// if star does not exist in cache then factory will create one and store it in cache
20+
if (star == null) {
21+
if (brightnessLevel.equalsIgnoreCase("bright")) {
22+
star = new BrightStar();
23+
starCache.put("bright",star);
24+
}
25+
if (brightnessLevel.equalsIgnoreCase("dim")) {
26+
star = new DimStar();
27+
starCache.put("dim",star);
28+
}
29+
if (brightnessLevel.equalsIgnoreCase("dull")) {
30+
star = new DullStar();
31+
starCache.put("dull",star);
32+
}
33+
34+
}
35+
// return star object for reuse
36+
return star;
37+
}
38+
39+
40+
}
41+

0 commit comments

Comments
 (0)