"Object creation" mode of C + + design mode: Factory method

Source: Internet
Author: User

Part 0 "Object creation" mode

Bypassing new in object creation mode avoids tight coupling (relying on specific classes) caused by object creation (new), thus supporting the stability of object creation. It is the first step after the interface abstraction.

  Typical mode
Factory Method
Abstract Factory
Prototype
Builder

Part 1 Factory Method factory methods

  Motive (motivation)

In software systems, it is often the work of creating objects, and the specific types of objects that need to be created often change due to changes in requirements.

How to deal with this change? How do you bypass the general object creation method (new) and provide a "encapsulation mechanism" to avoid tight coupling between client programs and this "concrete object creation work"?

  Pattern definition

Defines an interface for creating objects, letting subclasses decide which class to instantiate. Factory method enables the instantiation of a class with a delay (purpose: Decoupling, means: virtual function) to subclasses. --"Design pattern" GoF

  Uml

  Summary of Points

The Factory method pattern is used to isolate the coupling between the consumer and the specific type of the class object. In the face of a specific type that is constantly changing, tight coupling relationships (new) can lead to software fragility.

The Factory method mode, by means of object-oriented approach, defers the work of the concrete object to be created to the subclass, thus implementing a strategy of extension (not change), which solves the tight coupling relationship better.

Factory method mode solves the requirement change of "single object". The disadvantage is that the creation method/parameter is required to be identical.

"Object creation" mode of C + + design mode: Factory method

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.