Java Factory Pattern HackerRank

 Problem Link:

https://www.hackerrank.com/challenges/java-factory/problem


Solution:


public Food getFood(String order) {

            if(order.equals("pizza")){
               // System.out.println("The factory returned class pizza");
                Pizza obj=new Pizza();
             return obj;   
            }
            else{
                //System.out.println("The factory returned class cake");
                Cake obj=new Cake();
                return obj;
            }
            

}

Time Complexity: O(1)

Space Complexity: O(1)

Auxiliary Space: O(1)

Total Test Cases:2


Approach Used:

Here simply we will see whether the order value is pizza or cake. 

If it is pizza we will create obj of pizza class and return it else we will return the object of cake class.


"Thanks For Reading.😇"

"Share Further To Increase Knowledge Treasure.😊"



Comments

Popular posts from this blog

Perfect Sum Problem Geeks for Geeks

Array Formation HackerEarth

Recursive Sequence Geeks For Geeks Problem Of The Day 12-02-2024