Question

The difference between Abstract Factory and Factory design pattern is that AbstractFactory pattern uses composition to delegate responsibility of creating object to another class while Factory design pattern uses inheritance and relies on derived class or sub class to create object.

Below is a typical example of an abstract factory(http://www.oodesign.com/abstract-factory-pattern.html) Could some one please explain me where the abstract factory is using object composition?

abstract class AbstractProductA{
public abstract void operationA1();
public abstract void operationA2();
}

class ProductA1 extends AbstractProductA{
    ProductA1(String arg){
        System.out.println("Hello "+arg);
    } // Implement the code here
    public void operationA1() { };
    public void operationA2() { };
}

class ProductA2 extends AbstractProductA{
    ProductA2(String arg){
        System.out.println("Hello "+arg);
    } // Implement the code here
    public void operationA1() { };
    public void operationA2() { };
}

abstract class AbstractProductB{
    //public abstract void operationB1();
    //public abstract void operationB2();
}

class ProductB1 extends AbstractProductB{
    ProductB1(String arg){
        System.out.println("Hello "+arg);
    } // Implement the code here
}

class ProductB2 extends AbstractProductB{
    ProductB2(String arg){
        System.out.println("Hello "+arg);
    } // Implement the code here
}

abstract class AbstractFactory{
    abstract AbstractProductA createProductA();
    abstract AbstractProductB createProductB();
}

class ConcreteFactory1 extends AbstractFactory{
    AbstractProductA createProductA(){
        return new ProductA1("ProductA1");
    }
    AbstractProductB createProductB(){
        return new ProductB1("ProductB1");
    }
}

class ConcreteFactory2 extends AbstractFactory{
    AbstractProductA createProductA(){
        return new ProductA2("ProductA2");
    }
    AbstractProductB createProductB(){
        return new ProductB2("ProductB2");
    }
}

As for as I understood subclass's ConcreteFactory1 and ConcreteFactory1 are returning object's to the client. And its typically working as a Factory class with more than one product.

Where as Client code could be

AbstractFactory factory = new ConcreteFactory2();
AbstractProductA prodA = factory.createProductA();

Could some one please explain me where does the Object composition/delegation happen in the Abstract Factory?

Was it helpful?

Solution

Lets take this sentence and figure out what it is.

AbstractFactory pattern uses composition to delegate responsibility of creating object to another class

Abstract Factory can be termed as "Factory of Factory pattern". Here one more class will be there, lets call it FactoryOfFactory which creates/holds multiple Factories depending on the type of request and returns the end product.

class FactoryOfFactory {

   enum Type { P1, P2}

   public AbstractProductA createProductA(Type t) {
        switch(t) {
            case P1:
               return new ConcreteFactory1().createProductA();
            case P2:
               return new ConcreteFactory2().createProductA();
            ....
        }
   }

   public AbstractProductB createProductB(Type t) {
         switch(t) {
            case P1:
               return new ConcreteFactory1().createProductB();
            case P2:
               return new ConcreteFactory2().createProductB();
            ....
        }
   }

}

The definition of Composition is

Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

Here the container is FactoryOfFactory and the contained objects are different implementation of Factory classes such as ConcreteFactory1, ConcreteFactory2 etc. FactoryOfFactory delegates the request to respective factory implementation depending on Type

OTHER TIPS

AbstractFactory usually has multiple methods for creating related objects of Different types.

In your case ConcreteFactory1 encapsulates the relation between ProductA1 and ProductB1, and ConcreteFactory2 between ProductA2 and ProductB2 respectively.

I believe this is the point. Concrete Factory composites several target object types (subtypes). Delegation is slightly unusual, by invoking appropriate constructors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top