Tuesday, 27 February 2007

27 Feb 2007 Abstract Factory

27 Feb 2007

Today’s lesson was entirely related to Abstract Factory, where we covered almost all of it. Good discussion took place where I believe I was asking most of the questions because I read about Abstract Factory in advance. All readers especially for those who didn’t attend the class I hope my notes will be helpful.

Abstract Factory

In abstract factory without specifying concrete classes we can create set/families of related or dependent objects.
Lets take example of pasta maker that makes different types of pasta i.e. spaghetti, rigatoni, spiral etc. the shape of pasta depends on the pasta maker ‘disk’ which makes different shapes. Each specific pasta disk is a factory because they make different types of pasta. Whereas pasta maker is abstract factory. When we place the disk on the maker, the factory, which is disk, inherits the properties from the Abstract Factory (pasta maker). Pasta maker doesn’t care what disk is doing. Start the maker and shapes starts coming out. Now lets go back to the definition, “without giving all the details of concrete classes” that is, without giving the details of size and shape of pasta we require, we can make families or types of pasta (spaghetti, rigatoni, spiral etc.)

Of course in real world, every time we’ll have to change the disk but in abstract factory you can imagine that you can put all the disks once and wouldn’t require changing it manually.
Another example can be of a printer, that has different color cartridges(factory) in the printer(abstract factory). Client only deals with the printer(abstract type) without knowledge or defining which cartridges to use. Client uses the printouts(objects) coming from the printer (abstract factory)
In software programming factory is where the objects are defined and the intent of this pattern is to use/create these objects when required straight from the source without changing the concrete class or rewriting the code. However this pattern increases the complexity and extra work in initial coding.
I’ vent written any code yet but I have come across very good example of Abstract factory coding and UML diagram

UML :
http://www.fluffycat.com/javasource/javapatterns/UML/AbstractFactory.jpg

Abstract Factory
http://www.fluffycat.com//javasource/javapatterns/AbstractSoupFactory.txt

Concrete Factories
http://www.fluffycat.com//javasource/javapatterns/BostonConcreteSoupFactory.txt
http://www.fluffycat.com//javasource/javapatterns/HonoluluConcreteSoupFactory.txt

Test Abstract Factory
http://www.fluffycat.com//javasource/javapatterns/TestAbstractSoupFactory.txt

6 comments:

Abbas said...

very good examples Rizwan and well explained

Hani said...
This comment has been removed by the author.
Hani said...

Hi riz again!i liked your example.According to my knowledge abstract factory is an object creator but i remember you were talking about it alot in the class so I have a question in my mind may you can advise me about it.
Does Abstract factory eliminates the need of classes to bind in to the code?

Anonymous said...

hi rizwan, you have well defined the abstract factory, it was really easy for me to understand the abstract factory from the example u gave.

Anonymous said...

Sir!

Thanks indeed for the information.

As a matter of fact, when Dr. Sri gave the lecture, I couldn’t get the grasp of what it actually is about but ever since I’ve gone through this blog got an in-depth understanding of the core of that lecture.

Much obliged.

Asmat Ali said...

Rizwan! Well done! You got it right. After reading your blog, I understood it.