Friday, 17 January 2014

Bridge Design pattern - Implementation [Shape]



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

Click the below Image to Enlarge
Bridge Design pattern - Implementation [Shape]
















Bridge Design pattern - Implementation [Shape] - Class Diagram


















Shape.java


abstract class Shape
{

protected ColorImplementor color;

protected ColorImplementor getColor()
{
return color;
}

protected void setColor( ColorImplementor color )
{
this.color = color;
}

abstract public void colorIt();

abstract public void draw();
}

Circle.java

public class Circle extends Shape
{

public void colorIt()
{
System.out.print("Circle filled with ");
color.fillColor();
}

@Override
public void draw()
{
System.out.println("Circle has been drawn with out any color");
}
}

Rectangle.java

public class Rectangle extends Shape
{

public void colorIt()
{
System.out.print("Rectangle filled with ");
color.fillColor();
}

@Override
public void draw()
{
System.out.println("Rectangale has been drawn with out any color");
}
}

ColorImplementor.java

public interface ColorImplementor
{
public void fillColor();
}

BlueColorImplementor.java

public class BlueColorImplementor implements ColorImplementor
{
public void fillColor()
{
System.out.println("blue color using BlueColorImplementor");
}
}

GreenColorImplementor .java

public class GreenColorImplementor implements ColorImplementor
{
public void fillColor()
{
System.out.println("green color using GreenColorImplementor");
}
}


BridgeDesignPatternClient.java

import java.util.Scanner;

public class BridgeDesignPatternClient
{

public static void main( String[] args )
{

System.out.println("Please enter the color you want to fill  : 'Green' or 'Blue'");
Scanner scanner = new Scanner(System.in);
String color = scanner.next();
System.out.println("color : " + color);

Shape rectangleShape = new Rectangle();
rectangleShape.draw();
Shape circleShape = new Circle();
circleShape.draw();
System.out.println();
if( "blue".equalsIgnoreCase(color) )
{
rectangleShape.setColor(new BlueColorImplementor());
rectangleShape.colorIt();

circleShape.setColor(new BlueColorImplementor());
circleShape.colorIt();

}
else if( "green".equalsIgnoreCase(color) )
{
rectangleShape.setColor(new GreenColorImplementor());
rectangleShape.colorIt();

circleShape.setColor(new GreenColorImplementor());
circleShape.colorIt();
}

}
}

Output

Please enter the color you want to fill  : 'Green' or 'Blue'
Green
color : Green
Rectangale has been drawn with out any color
Circle has been drawn with out any color

Rectangle filled with green color using GreenColorImplementor
Circle filled with green color using GreenColorImplementor


Please enter the color you want to fill  : 'Green' or 'Blue'
Blue
color : Blue
Rectangale has been drawn with out any color
Circle has been drawn with out any color

Rectangle filled with blue color using BlueColorImplementor
Circle filled with blue color using BlueColorImplementor


See also:

  • Bridge Design pattern - Introduction
  • Bridge Design pattern - Real time example [Send Message]
  • Bridge Design pattern - Real time example [Publish Message]
  • Bridge Design pattern - Real time example [Shape]
  • Bridge Design pattern - Real time example [TV]
  • Bridge Design pattern - Class Diagram
  • Bridge Design pattern - Implementation [Send Message]
  • Bridge Design pattern - Implementation [Publish Message]
  • Bridge Design pattern - Implementation [LED TV]
  • Bridge Design pattern - Keypoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge