These two days suddenly want to learn Java source code, but see an article said Java source code to look at the design pattern, so easier to understand the structure of the source code, so ready to learn Java design patterns, first of all, look at the Java creation mode of the Factory mode, just beginning to see the abstract factory model is what it means, Baidu Encyclopedia is really not understand, and later in the online look at other articles, after careful thinking, but also understood, and now share with you.
First of all, the simple factory model, I use the real car factory example, first there is a car class interface called car, there is a method called drive (), and then there is a Geely car class called Jili, to achieve car interface, there is a Chery car class called Qirui, Also realized the car interface (because all cars, of course, can open).
1 Interfacecar{2 Public voidDrive () {3System.out.println ("I'm Driving");4 }5 classJiliImplementscar{6 PublicJili () {7System.out.println ("Production of a Geely car");8 }9PulicvoidDrive () {TenSYSTEM.OUT.PRINTLN ("I drive a Geely car"); One } A } - classQiruiImplementscar{ - PublicQirui () { theSystem.out.println ("Production of a Chery car"); - } -PulicvoidDrive () { -SYSTEM.OUT.PRINTLN ("I drive a Chery car"); + } -}
Now if I'm going to use the Jili class, the most basic way is
Jili jili=New Jili ();
If you use the Qirui class, the most basic way is
Qirui qirui=New Qirui ();
Well, now if there is a BYD car will have to new Biyadi (), and then there is a drive () method, so that each has to new car object, but in fact, Geely, Chery and BYD they belong to the car, also have drive () method, Is it not that we can create a factory that produces cars, and then you want a car just tell the factory and the factory will help you, so you don't have to take care of the details of the production (that is, the process of the new object), so it's better that the simple factory model comes out, The simple factory pattern is to create an instance of a class with the same characteristics, and then static the method of creating the car so that it can be produced directly from the factory, the code is as follows
1 classcarfactory{2 Public StaticCar Createcar (String carname) {3 if("Jili". Equals (Carname)) {4 return NewJili ();5}Else if("Qirui". Equals (Carname)) {6 return NewQirui ();7}Else if("Biyadi". Equals (Carname)) {8 return NewBiyadi ();9 }Ten}
That's OK, so if you're going to produce a car, call car Car=carfactory.createcar ("Jili"); Although this kind of factory is very good, but if I want to add a new car every time I have to modify the factory class to add new judgments, so do not conform to the open and close principle, so slowly people come up with a better production mode, this is the factory method model. First we abstracted the factory class of the production car as an interface,
1 Interface carfactory{2 Public StaticCar Createcar ();3 }4 then, let the specific factory class to achieve this production car interface, so there will be three specific factories, one is Geely car production plant, one is Chery car production plant, one is BYD car production plant5 classJilifactoryImplementscarfactory{6 Public StaticCar Createcar () {7 return NewJili ();8 }9 }Ten classQiruifactoryImplementscarfactory{ One Public StaticCar Createcar () { A return NewQirui (); - } - } the classBiyadifactoryImplementscarfactory{ - Public StaticCar Createcar () { - return NewBiyadi (); - } +}
The advantage of this is that if I still have to produce a great Wall car, do not have to modify carfactory this factory, only need to write a Great Wall factory class to achieve carfactory this interface can be. This is the factory method model, but the factory method model and the simple factory model each has the merit, also cannot say the factory method pattern is good, the key is to see the actual situation to decide which mode to use.
With the progress of the society, there are many kinds of cars, such as two series of SUVs and sports cars, and the factory method is generally aimed at the production of a series of abstract products, and the factory method to expand the model, into a variety of products can be produced, this is the abstract Factory mode. Baidu said that the abstract factory model is to create multiple product families of product objects, then I explain what is the product family, now we still take the car factory production of automobiles, as an example, with the development of the automotive industry, automotive applications are divided into SUVs and sports cars, so that the original car this interface can not be universal, and become Yueyeche ( and Paoche () These two interfaces, while the SUV is suitable for running on the mountain Road, the sports car is suitable to run on the race road, all drive methods have changed, the code is as follows
1 Interface Yueyeche () {2 Public Driveshanlu (); 3 }4 interface Paoche () {5 Public Drivesailu (); 6 }
Geely and Chery also produce sports cars and SUVs, so that everyone understands that the sports car is an abstract product family, SUV is also an abstract product family, and Geely sports car, Geely SUV, Chery sports car, Chery SUV is the real class, so we have four classes
1 PublicJilipaocheImplementspaoche{2 PublicDrivesailu () {3System.out.println ("I'm driving a Geely sports car in Race Road"); 4 }5 }6 PublicQiruipaocheImplementspaoche{7 PublicDrivesailu () {8System.out.println ("I'm driving a Chery sports car in Race Road"); 9 }Ten } One PublicJiliyueyecheImplementspaoche{ A PublicDrivesailu () { -System.out.println ("I'm driving a Geely SUV on the mountain Road"); - } the } - PublicQiruiyueyecheImplementspaoche{ - PublicDrivesailu () { -System.out.println ("I'm driving a Chery SUV on the mountain Road"); + } -}
Car interface and class is defined, I believe you also understand what is the product family, the following we should see how the factory realized, first or factory method mode, there is a production of car factory interface, but we have some expansion of this interface, because car class is divided into sports cars and SUVs, All of our interfaces are capable of producing both cars and SUVs.
1 Interface carfactory{2 Public Static Paoche Createpaoche (); 3 4 Public Static Yueyeche Createyueyeche (); 5 }
As with the factory method we need two real factory classes to produce real cars, uh ... Of course, the Geely plant can produce Geely sports cars and Geely SUV, Chery Factory can produce Chery sports car and Chery SUV, the code is as follows
1 classJilifactoryImplementscarfactory{2 Public StaticPaoche Createpaoche () {3 return NewJilipaoche ();4 }5 Public StaticYueyeche Createyueyeche () {6 return NewJiliyueyeche ();7 }8 }9 classQiruifactoryImplementscarfactory{Ten Public StaticPaoche Createpaoche () { One return NewQiruipaoche (); A } - Public StaticYueyeche Createyueyeche () { - return NewQiruiyueyeche (); the } -}
OK, the abstract Factory mode has been completed, if you want to produce a Geely sports car, you can jilifactory.createpaoche (); So the Geely sports car comes out, okay, Similarly, if we want a BYD car factory just implement carfactory this factory, the code is as follows
1 classBiyadifactoryImplementscarfactory{2 Public StaticPaoche Createpaoche () {3 return NewBiyadipaoche ();4 }5 Public StaticYueyeche Createyueyeche () {6 return NewBiyadiyueyeche ();7 }8}
I believe that you should understand what is the abstract factory model, but the specific circumstances of the choice of which mode, but also need to learn more to digest.
The Factory mode of Java design mode