Abstract Factory Design Pattern

The Abstract Factory Design Pattern is classified as a creational pattern and is similar to the Factory Design Pattern in a sense that Abstract Factory creates other factories whereas Factory creates objects. Both of these patterns decouple the details of implementation, whether it be factories or objects. The Abstract Factory groups individual factories that have a common theme together and creates families of related objects without having to depend on their concrete classes. This allows new concrete types to be derived without change to the base code.

There are about five components in implementing the Abstract Factory Design Pattern. They are:

Design

designPatterns-AbstractFactoryPattern.png

Abstract Factory Class

AbstractFactory.java

public abstract class AbstractFactory {
    public abstract Sedan getSedan(int type);
    public abstract Coupe getCoupe(int type);
}

Factory Classes

SedanFactory.java

public class SedanFactory extends AbstractFactory {
    public static final int CAMRY = 0;
    public static final int ACCORD = 1;
    public static final int ALTIMA = 2;

    @Override
    public Sedan getSedan(int type) {
        if(type == CAMRY)
            return new Camry();
        else if(type == ACCORD)
            return new Accord();
        else if(type == ALTIMA)
            return new Altima();
        return null;
    }

    @Override
    public Coupe getCoupe(int type) {
        return null;
    }
}

CoupeFactory.java

public class CoupeFactory extends AbstractFactory {
    public static final int SUPRA = 0;
    public static final int NSX = 1;
    public static final int Z = 2;

    @Override
    public Sedan getCoupe(int type) {
        if(type == SUPRA)
            return new Supra();
        else if(type == NSX)
            return new Nsx();
        else if(type == Z)
            return new Z();
        return null;
    }

    @Override
    public Coupe getSedan(int type) {
        return null;
    }
}

Interfaces

Sedan.java

public interface Sedan {
    public void drive();
}

Coupe.java

public interface Coupe {
    public void drive();
}

Concrete Classes

Camry.java

public class Camry implements Sedan {
    @Override
    public void drive() {
        System.out.println("Camry::drive()");
    }
}

Accord.java

public class Accord implements Sedan {
    @Override
    public void drive() {
        System.out.println("Accord::drive()");
    }
}

Altima.java

public class Altima implements Sedan {
    @Override
    public void drive() {
        System.out.println("Altima::drive()");
    }
}

Supra.java

public class Supra implements Coupe {
    @Override
    public void drive() {
        System.out.println("Supra::drive()");
    }
}

Nsx.java

public class Nsx implements Coupe {
    @Override
    public void drive() {
        System.out.println("Nsx::drive()");
    }
}

Z.java

public class Z implements Coupe {
    @Override
    public void drive() {
        System.out.println("Z::drive()");
    }
}

Factory Producer Class

FactoryProducer.java

public class FactoryProducer {
    public static final int SEDAN = 0;
    public static final int COUPE = 1;

    public AbstractFactory getFactory(int type) {
        if(type == SEDAN)
            return new SedanFactory();
        else if(type == COUPE)
            return new CoupeFactory();
        return null;
    }
}

This article is my 24th oldest. It is 479 words long, and it’s got 0 comments for now.