I recently read "headfirst design mode" and found that the difference between simple factory, factory mode, and abstract factory mode is not very easy to understand. After research, I wrote several examples to help me better understand the differences. The previous article introduced the factory model and "Fun with factory model". Next I will write another example to deepen my understanding of the abstract factory model.
Package go. derek; // This is an abstract factory class public abstract class CarFactory {abstract void brand (); abstract Car create (String type) ;}// BMW factory, inherited abstract factory class BmwFactory extends CarFactory {// combines manufacturing locations into the BMW factory MakeUpFactory makeUpFactory; BmwFactory (MakeUpFactory mf) {this. makeUpFactory = mf;} public void brand () {System. out. println ("this is the BMW factory, using the BMW designated accessories and manufacturing process");} // BMW's factory method public Car create (String type) {if (type. equals ("745Li") {return new Bmw_745Li (makeUpFactory);} else if (type. equals ("765Li") {return new Bmw_765Li (makeUpFactory);} else return null ;}// Audi factory, inherited abstract factory class AudiFactory extends CarFactory {// combine manufacturing locations into Audi factory MakeUpFactory makeUpFactory; AudiFactory (MakeUpFactory mf) {this. makeUpFactory = mf;} public void brand () {System. out. println ("this is the Audi factory, using Audi designated accessories and manufacturing process");} // The Factory method of Audi public Car create (String type) {if (type. equals ("A8L") {return new Audi_A8L (makeUpFactory);} else if (type. equals ("A6L") {return new Audi_A6L (makeUpFactory);} else return null ;}} // define an excuse for manufacturing location interface MakeUpFactory {void make (String type);} // China factory class ChinaFactory implements MakeUpFactory {public void make (String type) {System. out. println ("this car is made in China and sold to East Asia") ;}// class GermanyFactory implements MakeUpFactory {public void make (String type) {System. out. println ("this Car is made in Germany and sold to Europe") ;}// abstract class Car of the Car abstract class Car {// specify the manufacturing location MakeUpFactory makeUpFactory when obtaining the Car; abstract void intro () ;}// BMW 745Li class Bmw_745Li extends Car {public Bmw_745Li (MakeUpFactory mf) {this. makeUpFactory = mf;} private String type = "Bmw_745Li"; public void intro () {System. out. println ("this BMW model is" + type + ""); makeUpFactory. make (type) ;}}// BMW 765Li class Bmw_765Li extends Car {public Bmw_765Li (MakeUpFactory mf) {this. makeUpFactory = mf;} private String type = "Bmw_765Li"; public void intro () {System. out. println ("this BMW model is" + type + ""); makeUpFactory. make (type) ;}}// Audi A8L class Audi_A8L extends Car {public Audi_A8L (MakeUpFactory mf) {this. makeUpFactory = mf;} private String type = "A8L"; public void intro () {System. out. println ("This Audi Model is" + type + ""); makeUpFactory. make (type) ;}}// Audi A6L class Audi_A6L extends Car {public Audi_A6L (MakeUpFactory mf) {this. makeUpFactory = mf;} private String type = "A6L"; public void intro () {System. out. println ("This Audi Model is" + type + ""); makeUpFactory. make (type );}}
Package go. derek; // test class, simulate the client to call public class FactoryTest {public static void main (String [] args) {// create a Chinese factory, and the German factory MakeUpFactory china = new ChinaFactory (); MakeUpFactory germany = new GermanyFactory (); // create a BMW factory object, construct the parameter CarFactory bmw = new BmwFactory (china) based on the manufacturing location; // execute the bmw factory process. brand (); // get the Car car1 = bmw of the specified model. create ("745Li"); car1.intro (); Car car2 = bmw. create ("765Li"); car2.intro (); // create an audi factory object, with the manufacturing location as the construction parameter CarFactory audi = new AudiFactory (germany ); // execute the audi Engineering Process audi. brand (); // get the Car car3 = audi of the specified model. create ("A6L"); car3.intro (); Car car4 = audi. create ("A8L"); car4.intro ();}}
The running result is as follows: This is the BMW factory, which uses the designated parts and manufacturing technology of BMW. The model of this BMW is Bmw_745Li, which is made in China and sold to East Asia. The model of this BMW is Bmw_765Li, which is made in China, this is an Audi factory sold to East Asia. It uses Audi's designated accessories and manufacturing technology. The model of This Audi is A6L. This car is made in Germany. The model sold to Europe is A8L. This car is made in Germany, as can be seen from this example, the difference between the abstract factory model and the factory model is that the former is expanded in combination, while the latter is inherited. Both of them have specific application scenarios. The factory model is very good at adding products, but the demand for non-product extension is insufficient. The abstract design mode is not the same. Adding products is not his own business. It is very powerful to expand products without increasing product requirements. Both of them are very important design models, so it is worth your understanding ~