Skip to content

Commit 06a8bb9

Browse files
committed
Factory Design Pattern
1 parent 8e3cb22 commit 06a8bb9

11 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.Random;
2+
import com.javahelps.shapes.*;
3+
4+
public class ShapeClient {
5+
public static void main(String[] args) {
6+
Random random = new Random();
7+
Shape circle = new Circle(random.nextInt());
8+
Shape square = new Square(random.nextInt());
9+
Shape rectangle = new Rectangle(random.nextInt(), random.nextInt());
10+
circle.draw();
11+
square.draw();
12+
rectangle.draw();
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.javahelps.shapes;
2+
3+
public class Circle implements Shape {
4+
private double radius;
5+
6+
public Circle(double radius) {
7+
this.radius = radius;
8+
}
9+
10+
@Override
11+
public void draw() {
12+
System.out.println("Circle");
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javahelps.shapes;
2+
3+
public class Rectangle implements Shape {
4+
private int width;
5+
private int height;
6+
7+
public Rectangle(int width, int height) {
8+
this.width = width;
9+
this.height = height;
10+
}
11+
12+
@Override
13+
public void draw() {
14+
System.out.println("Rectangle");
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.javahelps.shapes;
2+
3+
public interface Shape {
4+
public void draw();
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.javahelps.shapes;
2+
3+
public class Square implements Shape {
4+
private int width;
5+
6+
public Square(int width) {
7+
this.width = width;
8+
}
9+
10+
@Override
11+
public void draw() {
12+
System.out.println("Square");
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import com.javahelps.shapes.*;
2+
3+
public class ShapeClient {
4+
public static void main(String[] args) {
5+
ShapeFactory factory = new ShapeFactory();
6+
Shape circle = factory.createShape(ShapeFactory.Type.CIRCLE);
7+
Shape square = factory.createShape(ShapeFactory.Type.SQUARE);
8+
Shape rectangle = factory.createShape(ShapeFactory.Type.RECTANGLE);
9+
circle.draw();
10+
square.draw();
11+
rectangle.draw();
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.javahelps.shapes;
2+
3+
class Circle implements Shape {
4+
private double radius;
5+
6+
public Circle(double radius) {
7+
this.radius = radius;
8+
}
9+
10+
@Override
11+
public void draw() {
12+
System.out.println("Circle");
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javahelps.shapes;
2+
3+
class Rectangle implements Shape {
4+
private int width;
5+
private int height;
6+
7+
public Rectangle(int width, int height) {
8+
this.width = width;
9+
this.height = height;
10+
}
11+
12+
@Override
13+
public void draw() {
14+
System.out.println("Rectangle");
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.javahelps.shapes;
2+
3+
public interface Shape {
4+
public void draw();
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.javahelps.shapes;
2+
3+
import java.util.Random;
4+
5+
public class ShapeFactory {
6+
private Random random = new Random();
7+
public enum Type {
8+
CIRCLE, SQUARE, RECTANGLE;
9+
}
10+
11+
public Shape createShape(Type type) {
12+
Shape shape = null;
13+
switch (type) {
14+
case CIRCLE:
15+
shape = new Circle(random.nextInt());
16+
break;
17+
case SQUARE:
18+
shape = new Square(random.nextInt());
19+
break;
20+
case RECTANGLE:
21+
shape = new Rectangle(random.nextInt(), random.nextInt());
22+
}
23+
return shape;
24+
}
25+
}

0 commit comments

Comments
 (0)