1

While reading about factories in this article, I came across these two terms AbstractFactory and ConcreteFactory.

enter image description here

While reading on StackOverflow, I found some good answers (like this SO question) that talk about factories and abstract factories, but still not clear about what a concrete factory is.

So, I wanted to ask:

  1. What is a concrete factory?
  2. How is it different from an abstract factory?

EDIT: Asking the question #2 in a separate question (Abstract Factory vs Concrete factory), to keep these two discussions separate.


My understanding so far (about factories):

  • At high level, I understand that, by a factory we refer to a method that returns a brand new something (can be anything, an object, a method, anything that's needed as our requirement), that we'd call a factory. Here, please correct me if I'm wrong.
  • Factories encapsulate and separate object creation from the rest of your code. Below is an example to illustrate the same:

// This function is a factory. When called, creates and returns a new object every time
function ourFactoryFn (firstName, lastName) {
    var a = {
        prop1:  firstName,
        prop2: lastName,
        prop3: firstName + ' ' + lastName + ' says Hello world!'
    }
    return a;
};

// Now, let's use our factory to produce new objects
// let's actually have an example to treat it like real life factories :P
var inputArr = [
    {firstName: 'Barack', lastName: 'Obama'},
    {firstName: 'Narendra', lastName: 'Modi'},
    {firstName: 'Mike', lastName: 'Tyson'},
    {firstName: 'Mahatma', lastName: 'Gandhi'},
    {firstName: 'Donald', lastName: 'Trump'},
    {firstName: 'Priyanka', lastName: 'Chopra'}
];
var outputArr = [];
inputArr.forEach(function (x) {
    var newObj = ourFactoryFn(x.firstName, x.lastName); // we used our factory
    console.dir(newObj); // print the freshly created object
    outputArr.push(newObj);
});
Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36

1 Answers1

1

A concrete factory is a class that implements the abstract factory and can be instantiated. An abstract factory is a non-instantiable class that defines the interface of the factory. For example (using Java for illustration), the abstract factory (FooFactory) would be something like:

public class Foo {}

public interface FooFactory {
    public Foo createFoo() { /* ... */ }
}

Being that this FooFactory is an interface (is abstract), it cannot be instantiated. For example, the following would be invalid (and cannot be compiled):

FooFactory factory = new FooFactory();

The concrete factory (called ConcreteFooFactory in this case) is an instantiable class that implements the FooFactory interface:

public class ConcreteFooFactory implements FooFactory {

    @Override
    public Foo createFoo() { /* ... */ }
}

FooFactory factory = new ConcreteFooFactory();

All of this begs the question: Why bother creating an abstract factory and then a concrete factory? The reason is that the abstract factory defines the interface (the methods that can be called) of the factory while not defining any of the specific behavior of the factory. This allows us to create multiple FooFactory implementations (concrete classes), each with different specific behavior. The clients that depend on the factory can depend on the abstract factory while and its behavior can change based on which concrete factory is passed to it:

public class BlueFooFactory implements FooFactory {

    @Override
    public Foo createFoo() { /* ...create blue Foo... */ }
}

public class RedFooFactory implements FooFactory {

    @Override
    public Foo createFoo() { /* ...create red Foo... */ }
}

public class FooClient {

    private FooFactory factory;

    public FooClient(FooFactory factory) {
        this.factory = factory;
    }

    public void doSomething() {
        Foo someFoo = factory.createFoo();
        // ...do something with someFoo...
    }
}

// Option 1
FooClient client = new FooClient(new BlueFooFactory());

// Option 2
FooClient client = new FooClient(new RedFooFactory());

While these examples are done in Java, they can be done in Javascript as well. See Object-oriented JavaScript: A Deep Dive into ES6 Classes for more information.

Justin Albano
  • 3,809
  • 2
  • 24
  • 51