noscript

The Factory Method Pattern is a creational design pattern used in object-oriented programming for creating objects to define an interface, but allow subclasses to alter the type of objects that will be created. Encapsulating object creation logic can be achieved without specifying the exact object class. A loose connection between the creator and the product is promoted by this pattern.

In Java, you can implement the Factory Method Pattern using a method in a class that creates and returns instances of a class, and subclasses can override this method to change the type of the created object.

 Here’s a simple example to illustrate the Factory Method Pattern in Java:

 // Product interface

interface Product {

    void doSomething();

}

 // Concrete Product 1

class ConcreteProduct1 implements Product {

    @Override

    public void doSomething() {

        System.out.println(“Doing something in ConcreteProduct1”);

    }

}

 // Concrete Product 2

class ConcreteProduct2 implements Product {

    @Override

    public void doSomething() {

        System.out.println(“Doing something in ConcreteProduct2”);

    }

}

 // Creator class with factory method

abstract class Creator {

    // Factory method

    public abstract Product createProduct();

   // An operation that uses the product

    public void useProduct() {

        Product product = createProduct();

        product.doSomething();

    }

}

 // Concrete Creator 1

class ConcreteCreator1 extends Creator {

    @Override

    public Product createProduct() {

        return new ConcreteProduct1();

    }

}

 // Concrete Creator 2

class ConcreteCreator2 extends Creator {

    @Override

    public Product createProduct() {

        return new ConcreteProduct2();

    }

}

 public class Main {

    public static void main(String[] args) {

        // Using ConcreteCreator1

        Creator creator1 = new ConcreteCreator1();

        creator1.useProduct(); // Output: Doing something in ConcreteProduct1

         // Using ConcreteCreator2

        Creator creator2 = new ConcreteCreator2();

        creator2.useProduct(); // Output: Doing something in ConcreteProduct2

    }

}

 In this example:

 

– `Product` is the interface for the objects that the factory will create.

– `ConcreteProduct1` and `ConcreteProduct2` are concrete implementations of the `Product` interface.

– `Creator` is an abstract class defining the factory method `createProduct()`, which returns a `Product` object.

– `ConcreteCreator1` and `ConcreteCreator2` are concrete implementations of the `Creator` class. They override the `createProduct()` method to create specific instances of `Product`.

– In the `main` method, we demonstrate how different concrete creators can produce different concrete products.

For more information & classes Call: 2048553004
Registration Link: Click Here!

Author: Ambarish Durani

JAVA Trainer

IT Education Centre Placement & Training Institute

© Copyright 2024 | IT Education Centre.