Header Ads

  • Breaking Now

    What is difference between Abstract Factory and Factory Method design patterns?

    In order to answer this question we must first understand what are Abstract Factory and Factory Method design patterns.

    An Abstract Factory(AF) provides an interface for creating families of related or dependent objects without specifying their concrete classes. you usually have multiple factory implementations. Each factory implementation is responsible for creating objects that are usually in a related hierarchy.

    In case of Factory Method(or simply called Factory pattern), generally a key or parameter is provided and method obtains an object of that type.The classic example can be creating a Database Connection Factory which is responsible for providing a vendor specific database connection objects(like Oracle,DB2,MS SQL Server etc.) depending upon the kind of parameter is provided.

    AF is very similar to the Factory Method pattern.One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and
    relies on a subclass to handle the desired object instantiation.

    The best example to quote of AF is EJBHome object.When a client composes the home interface and calls a create method to get the component interface. The component interface is the abstract product and the implementation is created by the container. The client composes the abstract factory (home interface) and calls a create method on the abstract factory to create the product.

    In nutshell, AF is a creational pattern for the family of objects, whereas the factory method is a way to get one individual member of a family.So in a way, AF uses Factory method.

    Post Top Ad

    Post Bottom Ad