Hi,
I need some help in understanding Abstract Factory.
#################################################################
From my understanding, Factory
Pattern is the central point of the creation of object that relates to a Base class
e.g.
abstract class
Fruit,
class Apple extends Fruit
class Orange extends Fruit
class FruitFactory
public static void getFruit(type para)
{
if (para == x)
return new Apple();
else
return new Orange();
}
client code
Fruit f = FruitFactory.getFruit(para);
In this case, client do have to know if f is apple or orange.
#################################################################
For Abstract Factory,
abstract class House - defines method getChair, getTable
class Hut extends House - implements getChair, getTable
class Apartment extends House - implements getChair, getTable
getChair and getTable will create Chair and Table object accordingly.
Thus, I can use either Hut or Apartment to create Chair and Table object.
However, from the client I'd still need to know whether to create Hut or Apartment.
client
House h = new Hut(); // or new Apartment();
So, in actual case, the client still need to know which object to create as compare to Factory pattern, right?
Does it means that to hide the Abstract Factory from client, I'd need a Factory for the Abstract Factory which means a
HouseFactory, which will create an appropriate Hut or Apartment which then create the Table and Chair.
Whew!
Please comment if I've understand Abstract Factory correctly.
Cheers.
Han Ming