Skip to content

Commit 0b70a7a

Browse files
committed
flyweight added
1 parent 201cef9 commit 0b70a7a

14 files changed

+216
-0
lines changed

flyweight/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# flyweight
2+
This is repository of http://androidcode.pl blog. It shows uses of Flyweight pattern in Android. It is a part of Design Patterns - Flyweight post in the blog.

flyweight/example/DetailsFactory.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class DetailsFactory {
2+
3+
private static Map<Type, Details> types = new HashMap<>();
4+
5+
public static Details getPlantDetails(Type type) {
6+
Details item = types.get(type);
7+
if(item == null) {
8+
item = new Details(type);
9+
types.put(type, item);
10+
}
11+
return item;
12+
}
13+
}

flyweight/example/Main.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//user start new game so the board is creating
5+
//a lot of trees are in the board, so create all types of the tree and reuse them
6+
7+
for(int i=0; i<1000; i++) {
8+
//random type of the plant and get it from cache factory
9+
Type type = randomType();
10+
Details details = DetailsFactory.getTreeType(type);
11+
12+
//random position of the tree on the board
13+
Position position = randomPosition();
14+
15+
//create plant based on the type and position
16+
//Factory method can be used, but for simplify just if-else
17+
Plant plant;
18+
if(type == Type.OAK || type == Type.PINE || Type.CHESTNUT) {
19+
plant = new Tree(point, data);
20+
}
21+
else if(type == Type.ROSE || Type.TULIP || Type.HEATHER) {
22+
plant = new Flower(point, data);
23+
}
24+
25+
//put the plant on the map
26+
plant.draw();
27+
}
28+
}
29+
30+
public static Position randomPosition(int min, int max) {
31+
int x = randomValue(100, 100);
32+
int y = randomValue(100, 100);
33+
int z = randomValue(20, 20);
34+
return new Position(x, y, z);
35+
}
36+
37+
public static int randomValue(int min, int max) {
38+
return min + (int) (Math.random() * ((max - min) + 1));
39+
}
40+
}

flyweight/example/dummy/Position.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Position {
2+
3+
private int x, y, z;
4+
5+
public Position() {
6+
x = 0;
7+
y = 0;
8+
z = 0;
9+
}
10+
11+
public Position(int x, int y, int z) {
12+
this.x = x;
13+
this.y = y;
14+
this.z = z;
15+
}
16+
17+
//getter, setter and other methods
18+
}

flyweight/example/dummy/Texture.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Texture {
2+
3+
//graphics texture for map items
4+
//heavy object
5+
6+
//some fields, constructors and methods
7+
}

flyweight/example/dummy/Type.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public enum Type {
2+
3+
OAK(0),
4+
PINE(1),
5+
CHESTNUT(2),
6+
ROSE(3),
7+
TULIP(4),
8+
HEATHER(5);
9+
//etc
10+
11+
private int type;
12+
13+
public Type(int type) {
14+
this.type = type;
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Details {
2+
3+
private Type type;
4+
private Texture texture;
5+
private String data;
6+
7+
public Details(Type type) {
8+
this.type = type;
9+
loadTexture();
10+
loadData();
11+
this.texture = texture;
12+
this.data = data;
13+
}
14+
15+
private void loadTexture() {
16+
//load texture based on the type
17+
}
18+
19+
private void loadData() {
20+
//load additional data based on the type
21+
}
22+
23+
//some methods
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Flower extends Plant {
2+
3+
@Override
4+
public void draw() {
5+
//draw concrete type of the flower in the point in specific way
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public abstract class Plant {
2+
3+
private Point point;
4+
private Data data; //consume a lot of memory
5+
6+
public Plant(Point point, Data data) {
7+
this.point = point;
8+
this.data = data;
9+
}
10+
11+
public abstract void draw();
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Tree extends Plant {
2+
3+
@Override
4+
public void draw() {
5+
//draw concrete type of the tree in the point in specific way
6+
}
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class FlyweightFactor {
2+
3+
//if there are more types of flyweight classes than use Factory method
4+
5+
public static Map<String, Flyweight> flyweightTypes = new HashMap<>();
6+
7+
public static Flyweight getFlyweight(String type, int value) {
8+
Flyweight flyweight = flyweightTypes.get(type);
9+
if(result == null) {
10+
result = new ConcreteFlyweight(name, value);
11+
flyweightTypes.put(name, result);
12+
}
13+
else {
14+
result.setValue(value);
15+
}
16+
return result;
17+
}
18+
}

flyweight/pattern/Main.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//3 types of items can be created so reuse those items instead of creation new one
5+
String types[] = { "Type1", "Type2", "Type3" };
6+
7+
//generate a lot of items for example in loop with random values
8+
for(int i=0; i<100; i++) {
9+
//get random type and value
10+
String type = randomType(types);
11+
value = randomValue(0, 100);
12+
//get flyweight item from factory and do operation
13+
Flyweight flyweight = FlyweightFactory.getFlyweight(type, value);
14+
flyweight.operation();
15+
}
16+
//factory created only 3 items: Type1, Type2, Type3 and rest of 97 have been reused
17+
}
18+
19+
public static int randomValue(int min, int max) {
20+
return min + (int) (Math.random() * ((max - min) + 1));
21+
}
22+
23+
public static int randomType(String[] type) {
24+
return types[(int)(Math.random() * types.length)];
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class ConcreteFlyweight implements Flyweight {
2+
3+
private String type;
4+
private int value;
5+
6+
public ConcreteFlyweight(String type, int value) {
7+
//creation of object is consuming resources
8+
this.type = type;
9+
this.value = value;
10+
}
11+
12+
@Override
13+
public void operation() {
14+
//do specific operation for ConcreteFlyweight
15+
}
16+
17+
public void setValue(int value) {
18+
this.value = value;
19+
}
20+
21+
//other setters and getters
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface Flyweight {
2+
3+
void operation();
4+
}

0 commit comments

Comments
 (0)