Java Design Pattern---the difference between three factory patterns

Source: Internet
Author: User

Simple Factory, factory method, abstract factory all belong to the creation pattern in design pattern. Its main function is to help us extract the instantiation part of the object, optimize the architecture of the system, and enhance the extensibility of the system.

This article is my understanding of these three patterns after learning a summary and the difference between them.

 

Simple Factory

Factory classes in simple Factory mode typically use static methods to return different object instances by receiving different parameters.

it cannot be extended without modifying the code.

Factory Method

The factory approach is to provide a factory class for each product. Create a different product instance from a different factory instance.

in the same hierarchy, support the addition of any product.

Abstract Factory

Abstract Factory is to deal with the concept of product family. For example, every car company may have to produce cars, trucks and buses at the same time, so every factory has a way to create cars, trucks and buses.

It is easy to add new product lines to the concept of product family, but it is not possible to add new products.

Summary

★ Factory mode, the important thing is the factory class, not the product class. Product classes can be multiple forms, multiple layers of inheritance, or a single class. But to be clear, the factory-mode interface returns only one type of instance, which needs to be noted when designing a product class, preferably with a parent class or interface that is implemented together.

★ Use Factory mode, the returned instance must be created by the factory, not from other objects.

★ Factory Mode The returned instance can be not newly created, and it is also possible to return a good instance created by the factory.

 

difference between

Simple factory: Used to produce any product in the same grade structure. (For the addition of new products, powerless)

Factory method: Used to produce fixed products in the same grade structure. (Support for adding any product)
Abstract Factory: All products used to produce families of different products. (for adding new products, powerless; Support to increase the product family)

 

The above three factory methods have different levels of support in the two directions of hierarchical structure and product family. So consider which method should be used depending on the situation.

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.