Java Factory Pattern

Sort by

recency

|

164 Discussions

|

  • + 0 comments

    Why is this one considered Advanced? What is so advanced about the Factory pattern?

  • + 0 comments

    Ugh! I had to take a guess that returning null is expected behavior when the given input is neither "cake" nor "pizza". What is the point of hiding the failing test cases, especially when the English description is not complete?

  • + 0 comments

    import java.util.Scanner;

    interface Food { public String getType(); }

    class Pizza implements Food { public String getType() { return "Someone ordered a Fast Food!"; } }

    class Cake implements Food { public String getType() { return "Someone ordered a Dessert!"; } }

    class FoodFactory { public Food getFood(String order) { if (order.equalsIgnoreCase("pizza")) { return new Pizza(); } else { return new Cake(); } } }

    public class Solution { public static void main(String args[]) { Scanner sc = new Scanner(System.in); // Read user input String order = sc.nextLine();

        FoodFactory foodFactory = new FoodFactory();
        Food food = foodFactory.getFood(order);
    
        System.out.println("The factory returned class " + food.getClass().getSimpleName());
        System.out.println(food.getType());
    
        sc.close();
    }
    

    }

  • + 0 comments

    This looks like a good example of the factory design pattern. Instead of creating objects directly, the factory handles the logic of returning the right type based on input. cbtfturbo

  • + 1 comment

    This FoodFactory problem is a cool way to see polymorphism in action! Just like how Pizza and Cake implement the Food interface, real-life food spots like Wheelock Place offer a mix of options—whether you're craving fast food (pizza, anyone?) or something sweet (dessert time!).

    Speaking of which, Wheelock Place isn’t just another mall—it’s a go-to for quick bites, healthy meals, and everything in between. Maybe the FoodFactory should add a 'Wheelock Place food' option next—instant access to all those delicious choices!