First, what is the factory model
The factory pattern (Factory pattern) is the same as its name, in object-oriented programming, a factory is usually an object used to create other objects. The Factory mode implements different allocation schemes and creates objects according to different parameters. For example, using a factory to create 人 This object, if we need a man object, the factory will create a man for us; if we need a woman, the factory will produce a woman for us (Nu wa Empress of the visual Sense AH).
The factory model is usually divided into:
- Simple Factory mode
- Factory method Mode
- Abstract Factory mode
This lesson explains the simple factory model, the factory method mode, the abstract factory model will be discussed in the next lesson.
Second, the Factory model class diagram
Just now we're talking about creating people in Factory mode. Now we come back to the "Nu wa empress", first create a man, he every day "eat, sleep, play peas" ... Then we create a woman, she also "eat, sleep, hit the peas" every day ... (If you call peas, please leave me ...) )
Let's take the simple factory model for example.
Iii. Factory mode Sample code
Talk is cheap, show me the code.
Abstract ProductsAbstractClassHuman {PublicAbstractvoidEat();PublicAbstractvoidSleep();PublicAbstractvoidBeat();}Specific Products-MenClassMansExtendshuman{PublicvoidEat() {System.Out.println ("Man can eat."); }PublicvoidSleep() {System.Out.println ("Man can sleep."); }PublicvoidBeat() {System.Out.println ("Man can beat Doudou."); }}Specific products-WomenClassFemaleExtendshuman{PublicvoidEat() {System.Out.println ("Female can eat."); }PublicvoidSleep() {System.Out.println ("Female can sleep."); }PublicvoidBeat() {System.Out.println ("Female can beat Doudou."); }}Simple FactoryPublicClasshumanfactory {PublicStatic HumanCreatehuman(String gender) {Human Human =Nullif (Gender.equals ( "man") {human = new Man ();} else if (gender.equals ( "female")) {human = new Female ();} return Human;} //nu wa made man public class goddess {public Static void main (String[] args) Throws IOException {//build a man Human Human = Humanfactory.createhuman ( "man"); Human. Eat (); Human. Sleep (); Human. Beat (); } }
The simple factory model is the way it is, so what is the difference between a factory method and a model? The factory method model abstracts The simple factory based on the simple factory model.
Abstract ProductsAbstractClassHuman {PublicAbstractvoidEat();PublicAbstractvoidSleep();PublicAbstractvoidBeat();}Specific Products-MenClassMansExtendsHuman {PublicvoidEat() {System.out.println ("Man can eat."); }PublicvoidSleep() {System.out.println ("Man can sleep."); }PublicvoidBeat() {System.out.println ("Man can beat Doudou."); }}Specific products-WomenClassFemaleExtendshuman{PublicvoidEat() {System.out.println ("Female can eat."); }PublicvoidSleep() {System.out.println ("Female can sleep."); }PublicvoidBeat() {System.out.println ("Female can beat Doudou."); }}The simple factory becomes the abstract factoryAbstractClasshumanfactory {PublicAbstract HumanCreatehuman(String gender)Throws IOException;}Specific factory (each specific factory is responsible for a specific product)ClassManfactoryExtendshumanfactory{Public HumanCreatehuman(String gender)Throws IOException {ReturnNew Man (); } }ClassFemalefactoryExtendshumanfactory{public Human createhuman (String gender) throws ioexception {return new Female ();} } //nu wa made man public class goddess { Public static void main (string[] args) throws ioexception {//build a Man Humanfactory HF = new manfactory (); Human h = Hf.createhuman (
Iv. Application of Factory model
You may have asked, Factory mode Factory mode, I have not seen where to use AH? Here, here, inside the Java library. Depending on the parameters, the getinstance () method returns a different Calendar object.
inLocale)
Children who write Android shoes should also know that BitmapFactory this is an application of the factory model.
V. Reference documents
- Factory method
- Java Design Pattern:factory
Factory mode of design mode