2

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?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Shloka
  • 23
  • 5
  • possible duplicate of [Design Patterns: Abstract Factory vs Factory Method](http://stackoverflow.com/questions/4209791/design-patterns-abstract-factory-vs-factory-method) – Ted Hopp Jul 11 '13 at 06:16
  • Difference in intent is well explained in the above link. Abstract factory is just an interface for the concrete factories to create a family of products. It leaves it on to the concrete factories to create the products. And thats how maybe the delegation happens. But IMO delegation is incorrect word. – Narendra Pathai Jul 11 '13 at 06:24
  • A good example of pizza factory is provided in Head First Design patterns. – Narendra Pathai Jul 11 '13 at 06:26
  • Please note: composition is the act of composing a class out of references to other objects. It's known as a "Has-a" relationship. So think about that in the context of your question... – IgorGanapolsky Apr 24 '14 at 18:01

2 Answers2

4

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

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • Thank you Sanbhat.I was missing FactoryOfFactory class where the actual delegation happens, this explanation helped me a lot. – Shloka Jul 11 '13 at 07:26
  • @sanbhat So is the user's question illustrated with Composition or Aggregation? In other words, can AbstractProductA and AbstractProductB exist independent of ConcreteFactory1 and ConcreteFactory2? – IgorGanapolsky Apr 24 '14 at 18:06
1

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.

Dmitry Tikhonov
  • 301
  • 1
  • 8