There are cases in programming where it seems that you need to generate a very large number of small class instances to represent data. Sometimes you can greatly reduce the number of different classes that you need to instantiate if you can recognize that the instances are fundamentally the same except for a few parameters.
Use sharing to support large numbers of fine-grained objects efficiently.
Use flyweight pattern when
Note: Flyweights are not frequently used at the application level in Java. They are more of a system resource management technique, used at a lower level than Java.
public abstract class TeaOrder {
public abstract void serveTea(TeaOrderContext teaOrderContext);
}
Concrete Flyweight
public class TeaFlavor extends TeaOrder {
String teaFlavor;
TeaFlavor(String teaFlavor) {
this.teaFlavor = teaFlavor;
}
public String getFlavor() {
return this.teaFlavor;
}
public void serveTea(TeaOrderContext teaOrderContext) {
System.out.println("Serving tea flavor " + teaFlavor +
" to table number " +
teaOrderContext.getTable());
}
}
Context // This class is only in the context of this example
public class TeaOrderContext {
int tableNumber;
TeaOrderContext(int tableNumber) {
this.tableNumber = tableNumber;
}
public int getTable() {
return this.tableNumber;
}
}
The Factory
public class TeaFlavorFactory {
TeaFlavor[] flavors = new TeaFlavor[10];
//no more than 10 flavors can be made
int teasMade = 0;
public TeaFlavor getTeaFlavor(String flavorToGet) {
if (teasMade > 0) {
for (int i = 0; i < teasMade; i++) {
if (flavorToGet.equals((flavors[i]).getFlavor())) {
return flavors[i];
}
}
}
flavors[teasMade] = new TeaFlavor(flavorToGet);
return flavors[teasMade++];
}
public int getTotalTeaFlavorsMade() {return teasMade;}
}
The Client
class TestFlyweight {
static TeaFlavor[] flavors =
new TeaFlavor[100];
//the flavors ordered
static TeaOrderContext[] tables =
new TeaOrderContext[100];
//the tables for the orders
static int ordersMade = 0;
static TeaFlavorFactory teaFlavorFactory;
static void takeOrders(String flavorIn, int table) {
flavors[ordersMade] =
teaFlavorFactory.getTeaFlavor(flavorIn);
tables[ordersMade++] =
new TeaOrderContext(table);
}
public static void main(String[] args) {
teaFlavorFactory = new TeaFlavorFactory();
takeOrders("chai", 2);
takeOrders("chai", 2);
takeOrders("camomile", 1);
takeOrders("camomile", 1);
takeOrders("earl grey", 1);
takeOrders("camomile", 897);
takeOrders("chai", 97);
takeOrders("chai", 97);
takeOrders("camomile", 3);
takeOrders("earl grey", 3);
takeOrders("chai", 3);
takeOrders("earl grey", 96);
takeOrders("camomile", 552);
takeOrders("chai", 121);
takeOrders("earl grey", 121);
for (int i = 0; i < ordersMade; i++) {
flavors[i].serveTea(tables[i]);
}
System.out.println(" ");
System.out.println("total teaFlavor objects made: " +
teaFlavorFactory.getTotalTeaFlavorsMade());
}
}
Comments are closed.
nice post. thanks.
Useful blog website, keep me personally through searching it, I am seriously interested to find out another recommendation of it.