Detailed explanation of abstract factory pattern example of Java design pattern

This paper describes the abstract factory pattern of Java design pattern. Share with you for your reference, as follows:

Specific factory class: create a specific product object for production.

Abstract product classes can use interfaces or parent classes to describe the behavior characteristics of product objects.

A specific product class is a specific object.

What are the differences between abstract factory patterns and factory patterns?

In fact, the biggest difference is that when the structure of product classes is more complex, the abstract factory pattern defines different behaviors for different product families (i.e. a type of product object), that is, different generation methods are defined in the parent class or interface. Different product families call their own creation methods. At the same time, different product families also have the same characteristics that can be classified. These characteristics are embodied in a factory.

For example, Apple makes mobile phones and tablets.

This is obviously two different product families. Mobile phones and tablets are not the same kind of products. Therefore, when defining the factory behavior in the factory, you need to design a creation method for each of the two product families.

Specific to a factory, it will produce these two products. In fact, the horizontal comparison of these two products has something in common, that is, the models produced by Apple factories in a certain period of time are regular, so the factory in 2011 will not produce the product object of iPhone 5 (because there was no iPhone 5 at that time)

Compared with the factory pattern, the abstract factory pattern is actually more complex. When there is only one product family, it naturally degenerates to the use of factory pattern.

Design mode:

Abstract factory class iapplefactroy

Specific factory class applefactoryin2011 applefactoryin2012

Abstract product class imobile icomputer

Specific product category pad3 Padmini phone4s phone5

Test:

For more Java related content, interested readers can view the special topics of this site: Java data structure and algorithm tutorial, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>