Skip to content

Instantly share code, notes, and snippets.

@deda9
Forked from rishi93/factory.java
Created July 11, 2016 15:46
Show Gist options
  • Save deda9/dd2105190a2841af411140403e2d7009 to your computer and use it in GitHub Desktop.
Save deda9/dd2105190a2841af411140403e2d7009 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