Creating Objects with the Factory Pattern

The factory pattern, also known as the factory method pattern or the virtual constructor, is one of the patterns used to create objects.

Intent: If you are in situation where you would not know the precise type of the object you want to create until runtime, then the factory might be a good pattern to apply.

Motivation: Frameworks use abstract classes to define and maintain relationships between objects. A framework is often responsible for creating these objects as well including deciding which subclass to use and make the creation implementation as loosely coupled as possible.

Solution: The idea behind using the factory class is to produce instances of objects based on a set of input parameters. Factory patterns are often implemented using static methods that return objects and do not require a pointer to an instance of the factory class.

Implementation: Let’s say you have an event marketing website and you are customizing the main content of your home page based on the location of the visitor. Your content engine needs to display different events to different locations.
For simplicity, we are going to group the locations to East, West, North and South. We are also going to choose a main event to display for each location.


public abstract class Event {
    private LocalDate eventDate;
    private String eventPlace;
    public Event(String eventplace, LocalDate eventDate){
        this.eventDate = eventDate;
        this.eventPlace = eventplace;
    }

    public LocalDate getEventDate() {
        return eventDate;
    }

    public String getEventPlace() {
        return eventPlace;
    }
 
    public abstract void displayed();
}

The EventFactory class:


public class EventFactory {
    public static Event getEvent(String location){
        switch(location){
            case "east": 
                return new ClassicalMusic(
                    "The Opera House", 
                    LocalDate.of(2017, Month.SEPTEMBER, 16));
            case "west": 
                return new MovieOpening(
                    "Cinema Groucho Marx ", 
                    LocalDate.of(2017, Month.SEPTEMBER, 19));
            case "north": 
                return new FoodFestival(
                    "Little Italy", 
                    LocalDate.of(2017, Month.SEPTEMBER, 24));
            case "south": 
                return new Fireworks(
                    "The Shoreline", 
                    LocalDate.of(2017, Month.SEPTEMBER, 20));
        }
        throw new UnsupportedOperationException(
            "Unrecognized location: " +location);
    }
}

And an example of using the EventFactory will be:


public class ContentEngine {
    public static void main(String[] args){
        final Event event = EventFactory.getEvent("west");
        event.displayed();
    }
}

 

Below is one of the classes that extend Event:


public class ClassicalMusic extends Event {

    public ClassicalMusic(String place, LocalDate date) {
        super(place, date);
    }
 
    @Override
    public void displayed(){
        System.out.print("ClassicalMusic event was displayed "
            + "location: " +getEventPlace() +
            " and time: " +getEventDate());
    }
}

 

 

Leave Comment

Your email address will not be published. Required fields are marked *