-
-
Save deda9/dd2105190a2841af411140403e2d7009 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