Skip to content

Commit 247fc64

Browse files
factory pattern
1 parent d1a24bf commit 247fc64

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package designPatterns.factory_pattern;
2+
3+
public class Circle implements Shape {
4+
5+
@Override
6+
public void draw() {
7+
System.out.println("Inside Circle::draw() method.");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package designPatterns.factory_pattern;
2+
3+
public class MainApp {
4+
5+
public static void main(String... args){
6+
ShapeFactory shapeFactory = new ShapeFactory();
7+
8+
Shape shape = shapeFactory.getShapeType("circle");
9+
shape.draw();
10+
11+
shape = shapeFactory.getShapeType("Rectangle");
12+
shape.draw();
13+
14+
shape = shapeFactory.getShapeType("Square");
15+
shape.draw();
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package designPatterns.factory_pattern;
2+
3+
4+
public class Rectangle implements Shape {
5+
6+
@Override
7+
public void draw() {
8+
System.out.println("Inside Rectangle::draw() method.");
9+
}
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package designPatterns.factory_pattern;
2+
3+
public interface Shape {
4+
void draw();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package designPatterns.factory_pattern;
2+
3+
public class ShapeFactory {
4+
public Shape getShapeType(String shapeType){
5+
if(shapeType == null){
6+
return null;
7+
}
8+
if(shapeType.equalsIgnoreCase("Circle")){
9+
return new Circle();
10+
} else if(shapeType.equalsIgnoreCase("Square")){
11+
return new Square();
12+
} else if (shapeType.equalsIgnoreCase("Rectangle")){
13+
return new Rectangle();
14+
}
15+
return null;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package designPatterns.factory_pattern;
2+
3+
4+
public class Square implements Shape {
5+
6+
@Override
7+
public void draw() {
8+
System.out.println("Inside Square::draw() method.");
9+
}
10+
11+
}

0 commit comments

Comments
 (0)