forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolygonFactory.java
32 lines (31 loc) · 1.27 KB
/
PolygonFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.designpatterns.creational.factory;
/**
* In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal
* with the problem of creating objects without having to specify the exact class of the object that will be created.
* This is done by creating objects by calling a factory method—either specified in an interface and implemented by
* child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling
* a constructor.
*
* @see <a href="https://en.wikipedia.org/wiki/Factory_method_pattern">Factory Pattern</a>
*/
public class PolygonFactory {
/**
* Factory pattern implementation for the Polygon Interface to return the correct regular polygon object
* depending on the number of sides it has.
*
* @param numberOfSides in the polygon to initialize.
* @return the object having the respective number of sides
*/
public Polygon getPolygon(int numberOfSides) {
switch (numberOfSides) {
case 3:
return new Triangle();
case 4:
return new Square();
case 5:
return new Pentagon();
default:
return null;
}
}
}