What is Factory Pattern and how to implement Factory Pattern in Java?

Factory pattern is one of the most used design patterns in Java. In Factory pattern, we create objects without exposing the creation logic to the client and the client uses the same common interface to create a new type of object.

Note: You do not have to declare a factory pattern.
PortfolioManagerFactory has been declared for you.
PortfolioManagerImpl is the Implementation class.
PortfolioManager is your Interface.

Implementing a Factory Pattern in Java :

Step 1:
Interface :
Interface is used to declare various methods and variables. Interface does not contain definition for any.

Example :

Interface CircularShape {
String Shape;
void base(String Shape);

An Interface unlike a Class in Java, can be utilized(implemented) more than once.

Step 2:
Implementation Class :

A Concrete Class that is used to implement your Interface.

public class Circle implements CircularShape {

String Shape;
   public void base(String Shape) {

Step 3:
Factory Class:

Making use of the Factory class.

Public Class Factory1{

 public Shape getShape(String shapeType){
     Return shape;

Note: the Factory class already has a method, you only have to call that method in the AnnualizedReturnsRefactor Function in the PortfolioManagerApplication Class.

Note: Refer to the TODO’s , The TODO’s give away a hint, in order to connect the PortfolioManagerApplication with the PortfolioManagerImpl.