Abstract Factory Pattern

The Abstract factory pattern is a design pattern that is useful for creating families of objects. We will create the application shown in the following UML diagram. It represents an factory that creates furniture of a certain type of material.AbstractFactoryDiagram

Interface for creating furniture.

public interface FurnitureFactory {
    public Chair createChair();
    public Table createTable();
}

Specialization for creating iron furniture.

public class IronFurnitureFactory implements FurnitureFactory {
    @Override
    public Chair createChair() {
        return new IronChair();
    }

    @Override
    public Table createTable() {
        return new IronTable();
    }
}

Specialization for creating wooden furniture.

public class WoodenFurnitureFactory implements FurnitureFactory {
    @Override
    public Chair createChair() {
        return new WoodenChair();
    }

    @Override
    public Table createTable() {
        return new WoodenTable();
    }
}

Interface for chairs.

public interface Chair {
}

Specialization for iron chairs.

public class IronChair implements Chair {
    private String name = "iron chair";

    @Override
    public String toString() {
        return name;
    }
}

Specialization for wooden chairs.

public class WoodenChair implements Chair {
    private String name = "wooden chair";

    @Override
    public String toString() {
        return name;
    }

}

Interface for tables.

public interface Table {
}

Specialization for iron tables.

public class IronTable implements Table {
    private String name = "iron table";

    @Override
    public String toString() {
        return name;
    }
}

Specialization for wooden tables.

public class WoodenTable implements Table {
    private String name = "wooden table";

    @Override
    public String toString() {
        return name;
    }
}

Here we create an instance of a factory.

public class Client {
    public static void main(String[] args) {
        FurnitureFactory factory = new WoodenFurnitureFactory();
        Chair chair = factory.createChair();
        Chair chair1 = factory.createChair();
        Table table = factory.createTable();

        System.out.println(chair);
        System.out.println(table);
        System.out.println(chair1);
    }
}

It creates the following output.

wooden chair
wooden table
wooden chair

The factory enables to switch the output of all prints with great ease. We switch WoodenFurnitureFactory to IronFurnitureFactory.

public class Client {
    public static void main(String[] args) {
        FurnitureFactory factory = new IronFurnitureFactory();
        Chair chair = factory.createChair();
        Chair chair1 = factory.createChair();
        Table table = factory.createTable();

        System.out.println(chair);
        System.out.println(table);
        System.out.println(chair1);
    }
}

This gives us the following output.

iron chair
iron table
iron chair

This simple example shows us that with a few small changes we can have a lot of impact on a big application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s