Provide an interface to create a series of related or interdependent objects without specifying their specific classes.
A monomer instance of a CONCREATEFAACTORY class is usually created in Run-Time. This ConcreteFactory creates a ConcreteProduct object. In order to create different ConcreteProduct objects, Clients needs to use different concretefactory.
AbstractFactory (ContinentFactory) Defines an interface to create abstract products. ConcreteFactory (AfrciaFactory, AmericaFactory) implements creates specific Products.
AbstractProduct (Herbivore, Carnivore) declare an interface represents one product type Product (Wildebeest, Lion, Bison, Wolf) the definition of the interface is implemented AbstractProduct Client (AnimalWorld) product related objects Concrete factory created using AbstractFactory and AbstractProduct class code: // AbstractFactory public interface ContinentFactory {public Herbivore CreateHerbivore (); public Carnivore CreateCarnivore ();} // ConcreteFactory public class AfricaFactory implements ContinentFactory {public Herbivore CreateHerbivore () {return new Wildebeest ();} public Carnivore CreateCarnivore () {return new Lion () }} Public class American {public herbivore createherbivore () {return new bison (); public Carnivore CreateCarnivore () {return new Wolf ();}} // AbstractProduct public interface Herbivore {} public interface Carnivore {public void Eat (Herbivore h);} // Product public class Wildebeest implements Herbivore {} public class Lion implements Carnivore {Public Void Eat (Herbivore H) {system.out.println ("lion eats" h.getname ());
}} Public class Bison implements Herbivore {} public class Wolf implements Carnivore {public void Eat (Herbivore h) {System.out.println ( "Wolf eats" h.getName ());}} // Client public class AnimalWorld { private herbivore herbivore; private Carnivore carnivore; public AnimalWorld (ContinentFactory factory) {carnivore = factory CreateCarnivore ();.. herbivore = factory CreateHerbivore ();} // Mehtods public void RunFoodChain () {carnivore.Eat (herbivore);} public Static void main (string [] args) {animalworld world = new animalworld (new africafactory (); world.runfoodchain (); world = new animalworld (New amalwor Ericafactory ()); world.Runfoodchain (); Outputlion Eats Wildebeestwolf Eats Bison