Abstract Factory Design Pattern

Factory of factories. To keep things simple you can understand it like, you have a set of ‘related’ factory method design pattern. Then you will put all those set of simple factories inside a factory pattern. So in turn you need not be aware of the final concrete class that will be instantiated. You can program for the interface using the top factory.

There is also a view that abstract factory is ‘also’ implemented using prototype instead of factory methords pattern. Beginners for now please don’t yourself with that. Just go with factory methods pattern.

As there is a word ‘abstract’ in the pattern name don’t mistake and confuse it with java ‘abstract’ keyword. It is not related to that. This abstract is from object oriented programming paradim.

Sample abstract factory design pattern implementation in Java API

XML API implements abstract factory. There is a class name SchemaFactory. This acts as a factory and supports implemenation of multiple schemas using abstract factory design pattern.

Sample Java Source Code for Factory Method Design Pattern

Following is the interface, that will be returned as the final end product from the factories.

package com.javapapers.sample.designpattern.abstractfactory;

public interface Animal {
	public void breathe();
}

Following is the interface for which the factory implementation should be done. Inturn all abstract factory will return this type.

package com.javapapers.sample.designpattern.abstractfactory;

public interface AnimalFactory {
	public Animal createAnimal();
}

One of the factory from a predefined set which will instantiate the above interface.

package com.javapapers.sample.designpattern.abstractfactory;

public class SeaFactory implements AnimalFactory {

	public Animal createAnimal() {
		return new Shark();
	}

}

Second factory from a predefined set which will instantiate the Animal interface.

package com.javapapers.sample.designpattern.abstractfactory;

public class LandFactory implements AnimalFactory {
	public Animal createAnimal() {
		return new Elephant();
	}
}

Ads by Google

Implementation of an Animal. This class is grouped with the first abstract factory.

package com.javapapers.sample.designpattern.abstractfactory;

public class Shark implements Animal {
	public void breathe() {
		System.out.println("I breathe in water! He he!");
	}
}

Implementation of an Animal. This class is grouped with the second abstract factory.

package com.javapapers.sample.designpattern.abstractfactory;

public class Elephant implements Animal {
	public void breathe() {
		System.out.println("I breathe with my lungs. Its easy!");
	}
}

Following class consumes the abstract factory.

package com.javapapers.sample.designpattern.abstractfactory;

public class Wonderland {
	public Wonderland(AnimalFactory factory) {
		Animal animal = factory.createAnimal();
		animal.breathe();
	}
}

Testing the abstract factory design pattern.

package com.javapapers.sample.designpattern.abstractfactory;

public class SampleAbstractFactory {

	public static void main(String args[]){
		new Wonderland(createAnimalFactory("water"));
	}

	public static AnimalFactory createAnimalFactory(String type){
		if("water".equals(type))
			return new SeaFactory();
		else
			return new LandFactory();
	}
}

Output of the above sample program for abstract factory pattern

I breathe in water! He he!

This Creational Design Pattern tutorial was added on 11/11/2009.

«

»

Comments on “Abstract Factory Design Pattern”

  1. hey thanks for gr8 tutorials. Can you also add singleton pattern as it is widely asked question in interviews.

  2. Joe

    I am working on tutorials for all GOF design patterns and will post them one by one soon.

    Will post singleton design pattern within couple of weeks.

    By the way, your website is cool Priyan!

  3. Pankaj

    hey thanks for gr8 tutorials it’s very useful. Can you also add Visitor pattern

  4. Ritesh

    G8 tutorial
    example is very easy to understand

    thanks!

  5. Sandeep

    Nice tutorial, nice blog, it refreshed lot of things in quite easy way.
    Thank You!

    Can you please add something on threads as well. It kills me whenever I have to work on them. I hate them alot. Maybe your way of defining things will make them lil sweet for me :)

    Thank You Again!
    Cheers

  6. Anonymous

    The example for Abstract Factory pattern is still missing in your blog. You have once again given the example for factory pattern.
    The example deals only with one object which is Animal here, but the Abstract Factory Pattern will handle families of objects like Animal, Tree etc..,

  7. Yeah,
    Your description on all java concepts is helpful for me.
    Can you please explain How one can write a immutable class?

    • Gaurav

      1. By not providing the SETTER methods in you class. Like if you have var int i; then do not provide the

      public void setI(int i) {
      this.i = i;
      }

      by doing this u are not providing the way that can change the state.

      2. Also u can make ur class as final.

  8. venkat

    can you give one more example for factorfy and abstract factory design pattern.

  9. venkat

    can you give diff for factory and abstract factory

  10. Raajesh

    Hi Joe,

    Thanks for sharing your knowledge to the world.

    Regards
    Raajesh

  11. Anonymous

    Thanks for sharing your knowledge to the world.

  12. Anonymous

    Hi Joe,
    First i would like to thank to post these articles.Can you please explain the difference between Factory method pattern and Factory pattern? From long time i am trying to understand this concept but getting more confused.

    Thanks,
    Rajani

  13. Arjun

    Hi Joe,

    Thanks for sharing your knowledge . your thinking and presentation is simple and dynamic.

    One request can share hibernate .

    Regards
    arjun

    Raajesh on January 27th, 2012 9:48 am

  14. Madhu

    HI Joe, Thanks for the article, could you please add the import statements to the programs.

  15. jp

    May be the most simple and straight forward article on Abstract Factory

  16. Avase

    Joe You are really doing a great job

  17. sami

    best explanation to abstract factory pattern.

  18. Harshal

    Hi Joe,
    Thanx for sharing the abstract factory desing pattern, it helps a lot for solving my problem…

    Regards,
    Harshal

  19. Alex

    thanks for sharing your knowledge.
    Great Blog. Keep it up!

  20. saniosan

    Great discussion,will clear any one…

  21. Indramohan G

    I think it resembles the example of Factory design pattern. Please correct me if i am wrong.

  22. Vinuthna

    Nice Article

  23. Ydder Isran

    Sir, Could you please provide us with that simple UML diagram for the same.

  24. santosh

    hey can please post UML diagram for Abstract Factory Patterns

  25. Abhishek

    Nice example with ultimate tutorials

  26. rajesh

    Very good example …give one more abstract factory method for clearly understood………

  27. Shwetanjali

    Very nice tutorial. I really enjoy reading your blog, it is very informative.

    Thanks

  28. Jitendra

    Its too good.

  29. Anonymous

    Joe I really like the way you explain tough topics in a simple and easy manner. Greatly appreciate your work.

  30. Anonymous

    Hi Joe,
    I really like your explanations and examples on design patterns and the way you make them look trivial.
    Having said that I would like to underline the fact that the above pattern is more like a Factory pattern, as in this example the AnimalFactory creates an object.Whereas Abstract Factory creates a family of related objects.
    Am I wrong or am I missing something?
    Thanks

  31. Purush

    What is difference between Factory Method Pattern and Abstract Factory

  32. Purush

    Factory Design pattern is used to create object based on Inheritence where as Abstract Factory is based on Composition of objects

  33. Sundar

    Your explanation is great and easy to understand . If you could also add the class diagram along with it we can understand better
    a picture is more than 1000 words :)

  34. Anuj singh

    hi it really very good concept how to implement factory pattern .
    thanks dude…..

  35. Thanx for this great study…

  36. Raj

    Joe, First of all many thanks for writing such a good blog. I got one doubt in Abstract Factory pattern. could you please help me on clarifying it. If the Animals that lives in Sea might have some traits that are different from the one lives in Land. So we cannot add those methods in the Animal interface. We can create a new Interface and our Shark can implement it but the problem is we cannot use the interface type as return type for the Factory Method. How do we handle these kind of scenarios

  37. ravi

    Nice Example

  38. mehdi ghadimi

    hi . tnx for design pattern articles

  39. Jay

    Too good, Thank you so much for your efforts to create this in such simple language to understand.

  40. Anonymous

    Really very useful..thank you.

  41. Anonymous

    Nice tutorial!!!

  42. Sunil r

    Thanks for writing such a good blog. I got one doubt in Abstract Factory pattern.

    How should the code handle following scenario, where multiple animals exists that live in sea and land.

    Land ex: elephant, lion, tiger
    Sea ex: shark, whale, eel

  43. Anonymous

    thanks

  44. pritika

    Very helpful tutorials..

  45. Reeba

    Please update the block diagram for abstract factory pattern.

  46. Anonymous

    Thanks Joe…it’s a great help

  47. Saurabh

    do abstract factory class need to declared as abstract ?
    As:
    public abstract class AbstractFactory {

    }
    Kindly provide clearification

  48. Senthil

    Hi Joe,

    I learned what is AF Pattern and also program to Interface not to implementation now from the above example. so I wish you to keep writing such technical blog as simple as you do.

    thanks,
    senthil

  49. Anonymous

    Hi,

    Thanks for the work !!
    one small rquest….
    actually in interviews I fell it wont be good enough to give examples in animal, fruit etc..
    So please provide some technical or real time examples, so that we can impress the interviewer.

    Thanks,
    Yours..

  50. Anonymous

    Please also provide explanation about “How Abstract factory can be implemented using Prototype”

  51. abinaya

    cool !!!!!thank you so much…u helped a lot in need

  52. ilayarajakumaran a

    IF you have one hierarchy, you have one factory. If you have two hierarchy, you should have two factories.

    What if you have multiple hierarchy, will have multiple factories. He…He…

  53. Mohd Kose Avase

    Hi Joe,

    Your articles are very simple and straight forward. Thanks a lot for your work

  54. pavan

    This is just a factory method pattern where we have introduced one level of abstraction between factories. I do not see a factory handling factories.

  55. Hiten

    thanks Joe!

Your Comment