Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active July 11, 2016 15:46
Show Gist options
  • Save rishi93/2b810c1f4e4a4fcbfc0d to your computer and use it in GitHub Desktop.
Save rishi93/2b810c1f4e4a4fcbfc0d to your computer and use it in GitHub Desktop.
Design Patterns - Factory Pattern
import java.io.*;
interface Shape
{
public void draw();
}
class Square implements Shape
{
@Override
public void draw()
{
System.out.println("Square Drawn");
}
}
class Circle implements Shape
{
@Override
public void draw()
{
System.out.println("Circle Drawn");
}
}
class ShapeFactory
{
public static Shape getShape(String shape)
{
if(shape == "square")
return new Square();
else if(shape == "circle")
return new Circle();
else
return null;
}
}
public class factory
{
public static void main(String args[])
{
Shape shape1 = ShapeFactory.getShape("circle");
shape1.draw();
shape1 = ShapeFactory.getShape("square");
shape1.draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment