Thursday 28 November 2013

Flyweight Design pattern - Implementation


Click here to watch in Youtube : https://www.youtube.com/watch?v=SKZQx1DdA9U

Click the below Image to Enlarge
Flyweight Design pattern - Implementation

Flyweight Design pattern - Class Diagram



Shape.java

public interface Shape
{
void draw();
}

Circle.java

public class Circle implements Shape
{
private String    color;
private final int x      = 10;
private final int y      = 20;
private final int radius = 30;

public Circle()
{
}

public Circle( String color )
{
this.color = color;
}

public String getColor()
{
return color;
}

public void setColor( String color )
{
this.color = color;
}

@Override
public void draw()
{
System.out.println(this+" : Circle: Draw() [Color : " + color + ", x : " + x + ", y :" + y + ", radius :"
               + radius);
}
}

ShapeFactory.java

import java.util.HashMap;

public class ShapeFactory
{
private static final HashMap<String, Shape> shapeMap = new HashMap<String, Shape>();

public static Shape getShape( String shapeType )
{
Shape shape=null;
if( shapeType.equalsIgnoreCase("circle") )
{
shape = (Circle) shapeMap.get("circle");

if( shape == null )
{
shape = new Circle();
shapeMap.put("circle", shape);
System.out.println("Creating circle object with out any color in shapefactory : " + shape
               + "\n");
}
}
return shape;
}
}

FlyweightPatternDemo.java

public class FlyweightPatternDemo
{
private static final String colors[] = { "Red", "Green", "Blue", "Orange", "Black" };

public static void main( String[] args )
{

System.out.println("\n################ Red color Circles ####################");
for( int i = 0; i < 10; ++i )
{
Circle circle = (Circle) ShapeFactory.getShape("circle");
circle.setColor(colors[0]);
circle.draw();
}
System.out.println("\n############### Green color Circles ####################");
for( int i = 0; i < 10; ++i )
{
Circle circle = (Circle) ShapeFactory.getShape("circle");
circle.setColor(colors[1]);
circle.draw();
}
System.out.println("\n################ Blue color Circles ####################");
for( int i = 0; i < 10; ++i )
{
Circle circle = (Circle) ShapeFactory.getShape("circle");
circle.setColor(colors[2]);
circle.draw();
}
System.out.println("\n################ Orange color Circles ####################");
for( int i = 0; i < 10; ++i )
{
Circle circle = (Circle) ShapeFactory.getShape("circle");
circle.setColor(colors[3]);
circle.draw();
}
System.out.println("\n################ Black color Circles ####################");
for( int i = 0; i < 10; ++i )
{
Circle circle = (Circle) ShapeFactory.getShape("circle");
circle.setColor(colors[4]);
circle.draw();
}
}
}

Output

################ Red color Circles ####################
Creating circle object with out any color in shapefactory : Circle@10385c1

Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Red, x : 10, y :20, radius :30

############### Green color Circles ####################
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Green, x : 10, y :20, radius :30

################ Blue color Circles ####################
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Blue, x : 10, y :20, radius :30

################ Orange color Circles ####################
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Orange, x : 10, y :20, radius :30

################ Black color Circles ####################
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30
Circle@10385c1 : Circle: Draw() [Color : Black, x : 10, y :20, radius :30

See also:

  • Flyweight Design pattern - Introduction
  • Flyweight Design pattern - Real Time Example
  • Flyweight Design pattern - Class Diagram
  • Flyweight Design pattern - Sequence Diagram
  • Flyweight Design pattern - Key Points
  • All Design Patterns Links
  • No comments:

    Post a Comment