Friday, 29 November 2013

Builder Design pattern - Real Time Example [Meal Package]

Builder Design pattern - Introduction

Builder Design pattern - Sequence Diagram

Builder Design pattern - Class Diagram

Builder Design pattern - keypoints

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
  • Sunday, 24 November 2013

    Memento Design pattern - Implementation


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

    Click the below Image to Enlarge
    Memento Design pattern - Implementation
    Memento Design pattern - Class Diagram








































    LedTV.java

    public class LedTV
    {
    private String  size;
    private String  price;
    private boolean usbSupport;

    public LedTV( String size, String price, boolean usbSupport )
    {
    super();
    this.size = size;
    this.price = price;
    this.usbSupport = usbSupport;
    }

    public String getSize()
    {
    return size;
    }

    public void setSize( String size )
    {
    this.size = size;
    }

    public String getPrice()
    {
    return price;
    }

    public void setPrice( String price )
    {
    this.price = price;
    }

    public boolean isUsbSupport()
    {
    return usbSupport;
    }

    public void setUsbSupport( boolean usbSupport )
    {
    this.usbSupport = usbSupport;
    }

    @Override
    public String toString()
    {
    return "LedTV [size=" + size + ", price=" + price + ", usbSupport=" + usbSupport + "]";
    }

    }


    Memento.java

    public class Memento
    {
    private LedTV ledTV;

    public Memento( LedTV ledTV )
    {
    super();
    this.ledTV = ledTV;
    }

    public LedTV getLedTV()
    {
    return ledTV;
    }

    public void setLedTV( LedTV ledTV )
    {
    this.ledTV = ledTV;
    }

    @Override
            public String toString()
            {
           return "Memento [ledTV=" + ledTV + "]";
            }

    }

    Caretaker.java

    import java.util.ArrayList;
    import java.util.List;
    /*
     * Store Room
     */
    public class Caretaker
    {
    private List<Memento> ledTvList = new ArrayList<Memento>();

    public void addMemento( Memento m )
    {
    ledTvList.add(m);
    System.out.println("LED TV's snapshots Maintained by CareTaker :" + ledTvList);
    }

    public Memento getMemento( int index )
    {
    return ledTvList.get(index);
    }
    }


    Originator.java

    /*
     * Hall
     */
    public class Originator
    {
    LedTV ledTV;

    public LedTV getLedTV()
    {
    return ledTV;
    }

    public void setLedTV( LedTV ledTV )
    {
    this.ledTV = ledTV;
    }

    public Memento createMemento()
    {
    return new Memento(ledTV);
    }

    public void setMemento( Memento memento )
    {
    ledTV = memento.getLedTV();
    }

    @Override
            public String toString()
            {
           return "Originator [ledTV=" + ledTV + "]";
            }

    }

    MementoClient.java

    public class MementoClient
    {

    public static void main( String[] args )
    {
    Originator originator = new Originator();
    originator.setLedTV(new LedTV("42 inch", "60000Rs", false));

    Caretaker caretaker = new Caretaker();
    caretaker.addMemento(originator.createMemento());

    originator.setLedTV(new LedTV("46 inch", "80000Rs", true));
    caretaker.addMemento(originator.createMemento());

    originator.setLedTV(new LedTV("50 inch", "100000Rs", true));
    System.out.println("\nOrignator current state : " + originator);

    System.out.println("\nOriginator restoring to 42 inch LED TV...");

    originator.setMemento(caretaker.getMemento(0));

    System.out.println("\nOrignator current state : " + originator);

    }

    }

    Output

    LED TV's snapshots Maintained by CareTaker :[Memento [ledTV=LedTV [size=42 inch, price=60000Rs, usbSupport=false]]]
    LED TV's snapshots Maintained by CareTaker :[Memento [ledTV=LedTV [size=42 inch, price=60000Rs, usbSupport=false]], Memento [ledTV=LedTV [size=46 inch, price=80000Rs, usbSupport=true]]]

    Orignator current state : Originator [ledTV=LedTV [size=50 inch, price=100000Rs, usbSupport=true]]

    Originator restoring to 42 inch LED TV...

    Orignator current state : Originator [ledTV=LedTV [size=42 inch, price=60000Rs, usbSupport=false]]

    See also:

  • Momento Design pattern - Introduction
  • Momento Design pattern - Real time Example
  • Memento Design pattern - Class Diagram
  • Memento Design pattern - Sequence Diagram
  • Momento Design pattern - KeyPoints
  • All Design Patterns Links
  • Proxy Design pattern - Implementation[Virtual Proxy]


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

    Click the below Image to Enlarge
    Virtual Proxy
    Virtual Proxy - Class Diagram













































    Image.java


    interface Image
    {
    public void displayImage();
    }

    RealImage.java


    //on System A 
    class RealImage implements Image
    {

    private String filename = null;

    /**
    * Constructor
    * @param FILENAME
    */
    public RealImage( final String FILENAME )
    {
    filename = FILENAME; 
    loadImageFromDisk();
    }

    /**
    * Loads the image from the disk
    */
    private void loadImageFromDisk()
    {
    System.out.println("   Loading   " + filename);
    }

    /**
    * Displays the image
    */
    public void displayImage()
    {
    System.out.println("   Displaying " + filename+"\n");
    }

    }

    ProxyImage.java


    //on System B 
    class ProxyImage implements Image
    {

    private RealImage image    = null;
    private String    filename = null;

    /**
    * Constructor
    * @param FILENAME
    */
    public ProxyImage( final String FILENAME )
    {
    filename = FILENAME;
    }

    /**
    * Displays the image
    */
    public void displayImage()
    {
    if( image == null )
    {
    image = new RealImage(filename);
    }
    image.displayImage();
    }

    }


    Client.java


    class Client
    {

    /**
    * Test method
    */
    public static void main( String[] args )
    {
    final Image IMAGE1 = new ProxyImage("HiResolution_100MB_Dog Photo");
    final Image IMAGE2 = new ProxyImage("HiResolution_100MB_Lion photo");

    System.out.println("IMAGE1["+IMAGE1+"] calling displayImage first time :");
    IMAGE1.displayImage(); // loading necessary
    System.out.println("IMAGE1["+IMAGE1+"] calling displayImage second time :");
    IMAGE1.displayImage(); // loading unnecessary
    System.out.println("IMAGE1["+IMAGE1+"] calling displayImage third time :");
    IMAGE1.displayImage(); // loading unnecessary
    System.out.println("###############################################################\n");

    System.out.println("IMAGE2["+IMAGE2+"] calling displayImage first time :");
    IMAGE2.displayImage(); // loading necessary
    System.out.println("IMAGE2["+IMAGE2+"] calling displayImage second time :");
    IMAGE2.displayImage(); // loading unnecessary

    }

    }

    Output


    IMAGE1[ProxyImage@1ad086a] calling displayImage first time :
       Loading   HiResolution_100MB_Dog Photo
       Displaying HiResolution_100MB_Dog Photo

    IMAGE1[ProxyImage@1ad086a] calling displayImage second time :
       Displaying HiResolution_100MB_Dog Photo

    IMAGE1[ProxyImage@1ad086a] calling displayImage third time :
       Displaying HiResolution_100MB_Dog Photo

    ###############################################################

    IMAGE2[ProxyImage@42719c] calling displayImage first time :
       Loading   HiResolution_100MB_Lion photo
       Displaying HiResolution_100MB_Lion photo

    IMAGE2[ProxyImage@42719c] calling displayImage second time :
       Displaying HiResolution_100MB_Lion photo


    See also:

  • Proxy Design Pattern - Introduction
  • Proxy Design pattern - Real Time Example[ATM]
  • Proxy Design pattern - Real Time Example [Proxy Server]
  • Proxy Design pattern - Class Diagram
  • Proxy Design pattern - Sequence Diagram
  • Proxy Design pattern - Implementation [Protection Proxy]
  • Proxy Design pattern - Implementation [Remote Proxy]
  • Proxy Design Pattern - Key Points
  • All Design Patterns Links
  • Friday, 22 November 2013

    Flyweight Design pattern - Sequence Diagram

    Flyweight Design pattern - Class Diagram

    Flyweight Design pattern - Keypoints

    Flyweight Design pattern - Real Time Example

    Flyweight Design pattern - Introduction

    Proxy Design pattern - Implementation [Remote Proxy]