Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How DocumentBuilderFactory#newInstance() is an example of Abstract factory pattern?

What is Abstract Factory Pattern :-

It provides a way where a top level factory encapsulate a group of individual factories that are further able to create the family of related products without specifying their concrete classes.

As per Examples of GoF Design Patterns in Java's core libraries below is cited as example of Abstract pattern

javax.xml.parsers.DocumentBuilderFactory#newInstance()

But i am not sure how come its following the Abstract Factory Pattern here.

Is DocumentBuilderFactory considered as top level factory which internally cotains individual factory i.e DocumentBuilderFactory#newInstance() which is able to create the family of related products without specifying their concrete classes (as it just return the DocumentBuilderFactory not any specific implementation). Is this correct?

like image 594
M Sach Avatar asked Dec 04 '25 14:12

M Sach


2 Answers

Reference

What is a factory pattern?

A Factory method pattern (aka Factory pattern) is a creational pattern. The creational patterns abstract the object instantiation process by hiding how the objects are created and make the system independent of the object creation process.

An Abstract factory pattern is one level of abstraction higher than a factory method pattern which means it returns the factory classes.

enter image description here

Abstract factory pattern example

Const

public interface Const {
    public static final int SHAPE_CIRCLE =1;
    public static final int SHAPE_SQUARE =2;
    public static final int SHAPE_HEXAGON =3;
}

ShapeFactory

public abstract class ShapeFactory {
    public abstract Shape getShape(int shapeId);
}

In addition to SimpleShapeFactory we create new one:

ComplexShapeFactory

public class ComplexShapeFactory extends ShapeFactory {
    public Shape getShape(int shapeTypeId){
        Shape shape = null;
        if(shapeTypeId == Const.SHAPE_HEXAGON) {
            shape = new Hexagon();//complex shape
        }
        else{
            // drop an error
        };
        return shape;
    }
}

Now let’s create at the abstract factory, which returns one of the types of ShapeFactory:

ShapeFactoryType

public class ShapeFactoryType {
    public static final int TYPE_SIMPLE = 1;
    public static final int TYPE_COMPLEX = 2;

    public ShapeFactory getShapeFactory(int type) {
        ShapeFactory sf = null;
        if(type == TYPE_SIMPLE) {
            sf = new SimpleShapeFactory();
        }
        else if (type == TYPE_COMPLEX) {
            sf = new ComplexShapeFactory();
        }
        else throw new BadShapeFactoryException("No factory!!");
        return sf;
    }
}

And now the main call:

      ShapeFactoryType abFac = new ShapeFactoryType();

    ShapeFactory factory = null;
    Shape s = null;
    //returns a ShapeFactory but whether it is a
    //SimpleShapeFactory or a ComplexShapeFactory is not known to the caller.
    factory = abFac.getShapeFactory(1);//returns SimpleShapeFactory
    //returns a Shape but whether it is a Circle or a Pentagon is
    //not known to the caller.
    s = factory.getShape(2); //returns square.
    s.draw(); //draws a square
    //returns a ShapeFactory but whether it is a
    //SimpleShapeFactory or a ComplexShapeFactory is not
    //known to the caller.
    factory = abFac.getShapeFactory(2);
    //returns a Shape but whether it is a Circle or a Pentagon is
    //not known to the caller.
    s = factory.getShape(3); //returns a pentagon.
    s.draw(); //draws a pentagon

From DocumentBuilderFactory

The DocumentBuilderFactory is like ShapeFactoryType in example.

The newInstance(String factoryClassName,ClassLoader classLoader) returns new instance of a DocumentBuilderFactory based on factoryClassName (in my case I used abFac.getShapeFactory(1); and abFac.getShapeFactory(2);).

like image 194
Maxim Shoustin Avatar answered Dec 06 '25 03:12

Maxim Shoustin


DocumentBuilderFactory#newInstance() gives you a DocumentBuilder, which is a factory for Documents.

So DocumentBuilderFactory is a factory that produces factories, or in other words, an abstract factory. This is required because there are many implementations of DocumentBuilder depending on JDK version and installed libraries.

Does that clear it up?

like image 33
tom Avatar answered Dec 06 '25 05:12

tom