Give an example of a factory method pattern

I have read the difference between template factory

and template factory method

.

Factory method pattern overrides object creation for subclasses. It is also a Factory method, because the children of the "Creator" are responsible for implementing the "Create" method.

Where as a simple Factory is called directly by the class that wants to create the object.

But for the Factory pattern, we can also split the Factory class by adding class registration through reflection and using a subclassing layer for the objects to be instantiated and keeping the object instantiation logic inside that subclass.

I am not getting an example of a Factory Method Pattern that matches the above definition of a Factory Method Pattern.

Can you provide me with an example Factory method?

+3


source to share


2 answers


Suppose we want to produce fruit:

    public interface Fruit {
        public void plant();
        public void grow();
    }

    // concrete fruit Apple
    public class Apple implements Fruit {
        @Override
        public void plant() {
            System.out.println("Apple is planted.");
        }
        @Override
        public void grow() {
            System.out.println("Apple is growing.");
        }
    }

    // concrete fruit Banana
    public class Banana implements Fruit {
        @Override
        public void plant() { ... }
        @Override
        public void grow() { ... }
    }

      

In the factory pattern, there is one factory class responsible for creating new instances. The disadvantage though is that if you add one instance type, the logic of the factory class needs to change .

Example:



// Factory class
public class FruitGarden {
    public static Fruit createFruit(String fruitName) throws Exception {
        if(fruitName.equals("Apple")) {
            return new Apple();
        } else if(fruitName.equals("Banana")) {
            return new Banana();
        } else {
            System.out.printf("Sorry! %s not supported.\n", fruitName);
            throw new Exception();
        }
    }
    /* another way to create instance
    public static Fruit createApple() {
        return new Apple();
    }
    public static Fruit createBanana() {
        return new Banana();
    }
    */
}

      

In the factory pattern, there is an abstract factory that represents the creation of new instances. Different types of specimens are created in different specific factories. All of these concrete factories implement the abstract factory interface or extend the abstract factory class. If one new type is added, just add one concrete factory, so it has more extensibility .

Example:



// abstract factory interface
public interface FruitGarden {
    public Fruit createFruit();
}

// concrete factory which is responsible producing Apple
public class AppleGarden implements FruitGarden {
    @Override
    public Fruit createFruit() {
        return new Apple();
    }
}

// concrete factory which is responsible producing Banana
public class BananaGarden implements FruitGarden {
    @Override
    public Fruit createFruit() {
        return new Banana();
    }
}

      

+2


source


Java Collection.iterator()

is a factory method.



The specific implementation of the collection determines which specific iterator is returned by this method.

0


source







All Articles