Factory Design Pattern

The Factory Design Pattern is a type of creational pattern. It lets you create objects without directly instantiating a specific class. Instead, it uses a factory to decide which object to create.

This pattern is useful for promoting loose coupling by…


This content originally appeared on DEV Community and was authored by Tommy

The Factory Design Pattern is a type of creational pattern. It lets you create objects without directly instantiating a specific class. Instead, it uses a factory to decide which object to create.

This pattern is useful for promoting loose coupling by delegating the responsibility of instantiating objects to the concrete factory class.

It is commonly used for creating objects based on specific types and conditions.

class Main {
    public static void main(String[] args) {
        ToyFactory toyFactory = new ToyFactory();

        // Create a Car toy
        Toy car = toyFactory.createToy("CAR");
        car.play();  // Output: Vroom! Car toy is moving.

        // Create a Doll toy
        Toy doll = toyFactory.createToy("DOLL");
        doll.play();  // Output: Hello! Doll toy is talking
    }
}

// Create a Toy interface
interface Toy {
    void play();
}

// Implement concrete Toy classes
class CarToy implements Toy {
    @Override
    public void play() {
        System.out.println("Vroom! Car toy is moving.");
    }
}

class DollToy implements Toy {
    @Override
    public void play() {
        System.out.println("Hello! Doll toy is talking.");
    }
}

// Create the ToyFactory class
class ToyFactory {
    // Factory method to create toys based on type
    public Toy createToy(String toyType) {
        if (toyType == null) {
            return null;
        }
        if (toyType.equalsIgnoreCase("CAR")) {
            return new CarToy();
        } else if (toyType.equalsIgnoreCase("DOLL")) {
            return new DollToy();
        }
        return null;
    }
}


This content originally appeared on DEV Community and was authored by Tommy


Print Share Comment Cite Upload Translate Updates
APA

Tommy | Sciencx (2024-09-22T20:38:23+00:00) Factory Design Pattern. Retrieved from https://www.scien.cx/2024/09/22/factory-design-pattern/

MLA
" » Factory Design Pattern." Tommy | Sciencx - Sunday September 22, 2024, https://www.scien.cx/2024/09/22/factory-design-pattern/
HARVARD
Tommy | Sciencx Sunday September 22, 2024 » Factory Design Pattern., viewed ,<https://www.scien.cx/2024/09/22/factory-design-pattern/>
VANCOUVER
Tommy | Sciencx - » Factory Design Pattern. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/22/factory-design-pattern/
CHICAGO
" » Factory Design Pattern." Tommy | Sciencx - Accessed . https://www.scien.cx/2024/09/22/factory-design-pattern/
IEEE
" » Factory Design Pattern." Tommy | Sciencx [Online]. Available: https://www.scien.cx/2024/09/22/factory-design-pattern/. [Accessed: ]
rf:citation
» Factory Design Pattern | Tommy | Sciencx | https://www.scien.cx/2024/09/22/factory-design-pattern/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.