Java design Pattern (iv)-factory method mode (FactoryMethod pattern)

Source: Internet
Author: User

1     Concept Definition1.1   definition

Defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses.

1.2   type

To create a class pattern

2     principle Features2.1   class Diagram

2.2   Advantages

1) good encapsulation, clear code structure

2) High scalability, just modify the factory method or expand a factory class

3) shielding the product class, the caller does not need to care about product class changes

4) highly decoupled, high-level modules only need to know the product abstract class, no need to focus on implementation

2.3   Disadvantages

Increased code complexity

3     .Application Development3.1   Application Scenarios

1) There is a set of similar objects that need to be created

2) You cannot foresee the need to create instances of that kind when coding.

3) the system needs to consider extensibility and should not rely on the details of how product class instances are created, combined, and expressed.

4) factory methods can be used in heterogeneous projects

3.2   Practice Development

Https://github.com/Devonmusa/java-demos/tree/master/src/main/java/com/designpattern/factorymethods

4     Precautions

1). Factory method mode focus on the whole object, not on the process and details

Java design Pattern (iv)-factory method mode (FactoryMethod pattern)

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.