The difference between abstract factory pattern and factory method pattern in java

王林
Release: 2019-11-15 16:01:32
Original
11426 people have browsed it

The difference between abstract factory pattern and factory method pattern in java

1. Simple factory pattern

Features:

1. It is a specific Class, non-interface abstract class. There is an important create() method, which uses if or switch to create a product and return it.

2. The create() method is usually static, so it is also called a static factory.

Disadvantages:

1. Poor scalability (I want to add a kind of noodles. In addition to adding a new noodle product class, I also need to modify the factory class method).

2. It is not supported when different products require different additional parameters.

2. Factory method pattern

Pattern description:

Provides an interface for creating objects (factory interface) , let its implementation class (factory implementation class) decide which class (product class) to instantiate, and the implementation class creates instances of the corresponding class.

ModelForm elements:

Provide a product class interface. All product classes must implement this interface (it can also be an abstract class, that is, an abstract product).

Provide a factory class interface. All factory classes must implement this interface (ie, abstract factory).

Instances of the product class are created by the factory implementation class. The factory implementation class should have a method to instantiate the product class.

3. Abstract Factory Pattern

Pattern description:

To create a group of related or Interdependent objects provide an interface without specifying their concrete classes.

4. Difference

The abstract factory pattern is an upgraded version of the factory method pattern. It is used to create a set of related or interdependent objects.

The difference between it and the factory method pattern is that the factory method pattern targets a product hierarchical structure; while the abstract factory pattern targets multiple product hierarchical structures.

In programming, usually a product structure is represented by an interface or abstract class. That is to say, all products provided by the factory method pattern are derived from the same interface or abstract class, and the abstract factory pattern The products provided are derived from different interfaces or abstract classes.

In the abstract factory pattern, there is a concept of product family: the so-called product family refers to a family of functionally related products located in different product hierarchical structures. The series of products provided by the abstract factory pattern form a product family; while the series of products provided by the factory method are called a hierarchical structure.

Recommended tutorial:Java tutorial

The above is the detailed content of The difference between abstract factory pattern and factory method pattern in java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!