LFactory Method Mode Overview?in Factory method mode, the abstract factory class is responsible for defining the interfaces that create objects, and the creation of specific objects is implemented by the concrete classes that inherit the abstract factory. LAdvantages?The client does not need to be responsible for the creation of objects, thus clarifying the responsibilities of each class, if there are new objects added, only need to add a specific class and the specific factory class, do not affect the existing code, late maintenance easy, enhance the system extensibilityLDisadvantages?Additional writing code required, increased workload
Animal abstract class: publicabstract Animal {public abstract void Eat ();}
Factory Interface: publicinterface Factory {public abstract Animalcreateanimal();}
Specific Dog Categories: Publicclass Dog extends Animal {}
Specific cat categories: Publicclass Cat extends Animal {}
at first, each specific content in the test class is created by itself, but the work of creating objects is more troublesome and requires someone to do it, so Has created a Special class to create objects. Find every modification ( mainly post-add ) Factory code too cumbersome, with factory method improvement, for each specific implementation to provide a specific factory.
Dog Factory: Publicclass dogfactory implements Factory {
public Animal createanimal() {...}
}
Cat Factory: Publicclass catfactory implements Factory {
public Animal createanimal() {...}
}
The specific code is as follows:
Animal Abstract class:
Package Creation _ factory method mode; public abstract class Animal {public abstract void Eat ();}
Dog class:
Package Creation _ factory method mode; public class Dog extends Animal {@Overridepublic void Eat () {System.out.println ("Dog eats Meat");}}
Cat class:
Package Creation _ factory method mode; public class Cat extends Animal {@Overridepublic void Eat () {System.out.println ("Cat eats Fish");}}
Factory Interface:
Package Creation _ factory method mode; public interface Factory {public abstract Animal createanimal ();}
The Dogfactory class that specializes in creating dog factories:
Package Creation _ factory method mode;//This factory mainly makes dogs, but others do not know, because is returned Animalpublic class Dogfactory implements Factory {@Overridepublic Animal Createanimal () {return new Dog ();}}
The Catfactory class that specializes in creating cat factories:
Package Creation _ factory method mode;//specially Created cat factory public class Catfactory implements Factory {@Overridepublic Animal createanimal () {return New Cat ();}}
Main method Animalmain class:
Package Creation _ factory method mode; public class Animalmain {public static void main (string[] args) {//requirement: I want to buy a dog. Factory factory = new DOGFAC Tory (); Animal Animal = Factory.createanimal (); Animal.eat (); System.out.println ("------------");//demand: I want to buy a cat factory = new catfactory ();//To Buy a cat, here the program will replace the factory with the factory to build the cat animal = Factory.createanimal (); Animal.eat ();}}
The factory method pattern for Java Design patterns