The difference between factory method patterns and abstract factory patterns that everyone can understand

Source: Internet
Author: User

The factory method pattern and the abstract factory pattern in the design pattern have been unknowingly used in the programming, but often only practice did not carry out theoretical guidance, so feel that there is a 1:30 path to the monk without the system to learn the feeling of not practical. Now summarizes the abstract factory patterns used in the previous project to enhance understanding of the factory pattern in the Create mode.

Let's start with a chestnut. Perceptual understanding of the difference between the factory method pattern and the abstract factory pattern.

There is a wealthy businessman, in Shenzhen opened three mobile phone factory, respectively, OEM Apple mobile phone, Samsung mobile phone, Huawei mobile phone. Wealthy businessmen often inspected the 3 factories, in the Apple mobile phone factory said "everyone to do a good Apple phone", in the Samsung factory said " everyone to do a good Samsung mobile phone", in the Huawei factory said " Everyone should do a good job of Huawei Mobile." This is not very common sense. as a common sense, when inspecting a factory, just say " everyone should do a good cell phone ". Since it's a wealthy businessman, it's certainly not stupid. then, according to the object-oriented idea in the program design can be simplified to the Factory mode , and now the simple factory model is implemented:


Simple Factory mode implementation

public interface phonefactory{public void Makephone ();} public class iphone implement phonefactory{public void Makephone () {System.out.println (' Make a best iphone ');}} public class Samsung implement phonefactory{public void Makephone () {System.out.println ("Make a best Samsung");} public class Huawei implement phonefactory{public void Makephone () {System.out.println ("Make a Best Huawei");} public class Factory{public Static gotophone (String phone) {if (phone.equal ("iphone")) {return new iphone ();} else if (phone.equal ("Samsung")) {return new Samsung ();} Else{return new Huawei ();}} public class boss{public       static void Main (string[] args) {Phonefactory factory = Factory.gotophone ("Iphone"); Factory.makephone ();       }}

You can see that the simple factory pattern is differentiated by the IF judgment statement, so that the client is exempt from the responsibility of directly creating the Product object and is only responsible for "consuming" the product (as the owner of the phone factory does). Join now need to join ZTE Mobile phone, below we from the opening and closing principle (to expand open; Modify closed) to analyze the simple Factory mode. If you need to join ZTE Mobile phone, then you need to modify the factory in the Gotophone (String), to join an if branch, which is obviously modified, contrary to the open and close principle of the modification of the closed, but also because the application of the use if judgment statement to classify caused by the extension is not enough. So now we're going to use the factory method mode to directly remove the middle factory.


Factory method Pattern Implementation

public interface phonefactory{public void Makephone ();} public class iphone implement phonefactory{public void Makephone () {System.out.println (' Make a best iphone ');}} public class Samsung implement phonefactory{public void Makephone () {System.out.println ("Make a best Samsung");} public class Huawei implement phonefactory{public void Makephone () {System.out.println ("Make a Best Huawei");} public class boss{public       static void Main (string[] args) {Phonefactory factory = new IPhone (); Factory.makephone (); c1/>}}
Now can be seen, but also to increase ZTE phone do not modify the code, is directly can achieve a ZTE phone phonefactory on it. Just without modifying any code, just add an implementation class. If one day the ZTE mobile phone factory shut down, then there will be a direct deletion of the ZTE factory class is good, and will not modify other classes.

So here goes the abstract approach.
If the factory now wants to make mobile phone accessories, respectively, production of iphone headphones and film, then you need to first change the original factory to produce parts of the factory, and then production of the corresponding parts:


Abstract Factory mode implementation

public interface phonefactory{public void Makephonepart ();} public class IPhone implement Phonefactory{public Iphonepart Makephonepart () {return new Iphonepart ();}} public class Samsung implement Phonefactory{public Samsungpart Makephonepart () {return new Samsungpart ();}} public class Huawei implement Phonefactory{public Huaweipart Makephonepart () {Return to New Huaweipart ();} public interface phonepart{public void Makeearphone ();p ublic void Makescreentheca (); public class Iphonepart implement phonepart{public void Makeearphone () {System.out.println (' Make a best IPhone ');} public void Makescreentheca () {System.out.println (' Make a best IPhone ');}} public class Samsungpart implement phonepart{public void Makeearphone () {System.out.println (' Make a best IPhone ');} public void Makescreentheca () {System.out.println (' Make a best IPhone ');}} public class Huaweipart implement phonepart{public void Makeearphone () {System.out.println (' Make a best IPhone ');} public void Makescreentheca () {System.out.println ("MaKe a best IPhone ");}} public class boss{public static void Main (string[] args) {Phonefactory factory = new IPhone (); Factory.makephonepart (       ); Factory.makeearphone (); Factory.makescreentheca (); }}

so here's phonefactory as a layer of abstract factory , realize phonepart this layer only is the real production of products.

It can be seen that the abstract factory has multiple production parts of the specific factory class , they are abstracted from an abstract factory class. Then each specific mobile phone parts factory class here can create multiple specific product classes (product class instances).

Compared to the factory method, is one layer less, is an abstract factory class, you can derive a number of specific factory classes, each specific factory class can only create an instance of a specific product class.


Original from CSDN blog http://blog.csdn.net/dreamintheworld/article/details/42928079




The difference between factory method patterns and abstract factory patterns that everyone can understand

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.