Last active
July 11, 2016 15:46
-
-
Save rishi93/2b810c1f4e4a4fcbfc0d to your computer and use it in GitHub Desktop.
Design Patterns - Factory Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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