Flyweight Design Patterns
This commit is contained in:
10
com/javadevjournal/design/structural/flyweight/Brush.java
Normal file
10
com/javadevjournal/design/structural/flyweight/Brush.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package javadevjournal.design.structural.flyweight;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface Brush {
|
||||
public void setColor(String color);
|
||||
|
||||
public void draw(String content);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package javadevjournal.design.structural.flyweight;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class BrushFactory {
|
||||
|
||||
private static final HashMap<String, Brush> brushMap = new HashMap<>();
|
||||
|
||||
public static Brush getThickBrush(String color) {
|
||||
String key = color + "-THICK";
|
||||
Brush brush = brushMap.get(key);
|
||||
|
||||
if (brush != null) {
|
||||
return brush;
|
||||
} else {
|
||||
brush = new ThickBrush();
|
||||
brush.setColor(color);
|
||||
brushMap.put(key, brush);
|
||||
}
|
||||
return brush;
|
||||
}
|
||||
|
||||
public static Brush getThinBrush(String color) {
|
||||
String key = color + "-THIN";
|
||||
Brush brush = brushMap.get(key);
|
||||
|
||||
if (brush != null) {
|
||||
return brush;
|
||||
} else {
|
||||
brush = new ThinBrush();
|
||||
brush.setColor(color);
|
||||
brushMap.put(key, brush);
|
||||
}
|
||||
|
||||
return brush;
|
||||
}
|
||||
|
||||
public static Brush getMediumBrush(String color) {
|
||||
String key = color + "-MEDIUM";
|
||||
Brush brush = brushMap.get(key);
|
||||
|
||||
if (brush != null) {
|
||||
return brush;
|
||||
} else {
|
||||
brush = new MediumBrush();
|
||||
brush.setColor(color);
|
||||
brushMap.put(key, brush);
|
||||
}
|
||||
|
||||
return brush;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package javadevjournal.design.structural.flyweight;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public enum BrushSize {
|
||||
THIN, MEDIUM, THICK
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package javadevjournal.design.structural.flyweight;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class FlyweightPatternDemo {
|
||||
public static void main(String[] args) {
|
||||
// New thick Red Brush
|
||||
Brush redThickBrush1 = BrushFactory.getThickBrush("RED");
|
||||
redThickBrush1.draw("Hello There !!");
|
||||
|
||||
// Red Brush is shared
|
||||
Brush redThickBrush2 = BrushFactory.getThickBrush("RED");
|
||||
redThickBrush2.draw("Hello There Again !!");
|
||||
|
||||
System.out.println("Hashcode: " + redThickBrush1.hashCode());
|
||||
System.out.println("Hashcode: " + redThickBrush2.hashCode());
|
||||
|
||||
// New thin Blue Brush
|
||||
Brush blueThinBrush1 = BrushFactory.getThinBrush("BLUE"); //created new pen
|
||||
blueThinBrush1.draw("Hello There !!");
|
||||
|
||||
// Blue Brush is shared
|
||||
Brush blueThinBrush2 = BrushFactory.getThinBrush("BLUE"); //created new pen
|
||||
blueThinBrush2.draw("Hello There Again!!");
|
||||
|
||||
System.out.println("Hashcode: " + blueThinBrush1.hashCode());
|
||||
System.out.println("Hashcode: " + blueThinBrush2.hashCode());
|
||||
|
||||
// New MEDIUM Yellow Brush
|
||||
Brush yellowThinBrush1 = BrushFactory.getMediumBrush("YELLOW"); //created new pen
|
||||
yellowThinBrush1.draw("Hello There !!");
|
||||
|
||||
// Yellow brush is shared
|
||||
Brush yellowThinBrush2 = BrushFactory.getMediumBrush("YELLOW"); //created new pen
|
||||
yellowThinBrush2.draw("Hello There Again!!");
|
||||
|
||||
System.out.println("Hashcode: " + yellowThinBrush1.hashCode());
|
||||
System.out.println("Hashcode: " + yellowThinBrush2.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package javadevjournal.design.structural.flyweight;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class MediumBrush implements Brush {
|
||||
|
||||
/*
|
||||
intrinsic state - shareable
|
||||
*/
|
||||
final BrushSize brushSize = BrushSize.MEDIUM;
|
||||
|
||||
/*
|
||||
extrinsic state - supplied by client
|
||||
*/
|
||||
private String color = null;
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(String content) {
|
||||
System.out.println("Drawing MEDIUM '" + content + "' in color : " + color);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package javadevjournal.design.structural.flyweight;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class ThickBrush implements Brush {
|
||||
/*
|
||||
intrinsic state - shareable
|
||||
*/
|
||||
final BrushSize brushSize = BrushSize.THICK;
|
||||
|
||||
/*
|
||||
extrinsic state - supplied by client
|
||||
*/
|
||||
private String color = null;
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(String content) {
|
||||
System.out.println("Drawing THICK '" + content + "' in color : " + color);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package javadevjournal.design.structural.flyweight;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class ThinBrush implements Brush {
|
||||
|
||||
/*
|
||||
intrinsic state - shareable
|
||||
*/
|
||||
final BrushSize brushSize = BrushSize.THIN;
|
||||
|
||||
/*
|
||||
extrinsic state - supplied by client
|
||||
*/
|
||||
private String color = null;
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(String content) {
|
||||
System.out.println("Drawing THIN '" + content + "' in color : " + color);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user