Abstract Factory of design pattern, design pattern Factory

Source: Internet
Author: User

Abstract Factory of design pattern, design pattern Factory

Let's talk about the last one in the factory.

Abstract Factory ModelDefinition(From Baidu encyclopedia ):
Abstract Factory mode is the most abstract and general factory mode in all forms. Abstract Factory mode refers to a factory mode used when multiple abstract roles exist.
Abstract Factory mode provides an interface to the client to create product objects in multiple product families without specifying the specific product. According to the lee's replacement principle,
Any place that accepts the parent type should be able to accept the child type. Therefore, what the system actually needs is only some instances with the same type as those of abstract products, rather than the instances of these abstract products.
In other words, it is an example of the concrete sub-classes of these abstract products. The factory class is responsible for creating instances of concrete subclasses of abstract products.

UML class diagram

Code:

Public class Client {public static void main (String [] args) {// Creator benzCreator = new BenzCreator (); // benzCreator. createCar (). run (); // Creator bmwCreator = new BmwCreator (); // bmwCreator. createCar (). run (); AbstractFactory factory1 = new Factory1 (); factory1.createProductA (). getProductAName (); factory1.createProductB (). getProductBName (); AbstractFactory factory2 = new Factory2 (); factory2.createProductA (). getProductAName (); factory2.createProductB (). getProductBName () ;}} public interface AbstractFactory {AbstractProductA createProductA (); AbstractProductB createProductB ();} public class Factory1 implements AbstractFactory {@ Override public AbstractProductA createProductA () {return new ProductA1 () ;}@ Override public AbstractProductB createProductB () {return new ProductB1 () ;}} public class Factory2 implements AbstractFactory {@ Override public AbstractProductA createProductA () {return new ProductA2 () ;}@ Override public AbstractProductB createProductB () {return new ProductB2 () ;}} public interface AbstractProductA {String getProductAName ();} public class ProductA1 implements AbstractProductA {ProductA1 () {System. out. println ("Product A1") ;}@ Override public String getProductAName () {return "Product A1 name" ;}} public class ProductA2 implements AbstractProductA {ProductA2 () {System. out. println ("Product A2") ;}@ Override public String getProductAName () {return "Product A2 name" ;}} public interface AbstractProductB {String getProductBName ();} public class ProductB1 implements AbstractProductB {ProductB1 () {System. out. println ("Product B1") ;}@ Override public String getProductBName () {return "Product B1 name" ;}} public class ProductB2 implements AbstractProductB {ProductB2 () {System. out. println ("Product B2") ;}@ Override public String getProductBName () {return "Product B2 name ";}}

Product Family:

It refers to a family of products that are in different product level structures and are associated with functions.

Example:

I would like to give a very inappropriate example.
For example, we use 32-bit and 64-bit computers to install software with the corresponding digits.
When you install the 32-bit and 64-bit software, you only need to take one disc to install the software.
Here, the CD is a specific factory. If each CD has QQ and 360 (what is the fight ?)
Here, the 32-bit disc is the Factory1 in the code above. Here, the 64-bit disc is the Factory2 in the code above.
The QQ in the 32-bit CD is ProductA1, and 360 is ProductB1.
The QQ in the 64-bit CD is ProductA2, and 360 is ProductB2.

For me, this little white user, just take the right disc, do not need to find both the right QQ digits and 360 digits, is it very nice.

Advantages and disadvantages
Advantages:
Separate interfaces and implementations so that the client does not need to know the specific implementation.
Product Family switching becomes easy, just like adding a CD to a 128-bit machine.

Disadvantages:
It is not easy to expand new products (imagine how many classes and methods should be added to add a product ).
The hierarchical structure of the class is complex (look at the class diagrams all want to vomit)

Nature:

Previously, the essence of the factory method is to select implementation, that is, to select a specific product.
The abstract factory is to select a product family, from a single product to a product family.

 

After the three factory models are completed, the difference between them and the evolution process are first dug up and then written.

 

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.