Create a class mode primary key (Summary)

Source: Internet
Author: User

Labels: simple to use management SP application design and install different objects

The creation class mode includes the factory method mode, builder mode, abstract factory mode, Singleton mode, and prototype mode. They all provide object creation and management responsibilities. The Singleton mode and prototype mode are easy to understand. The Singleton mode requires only one object in the memory, and the prototype requires that a new object be generated by means of replication, these two are not easy to confuse. The following describes the other three modes.


I. Factory method mode vs builder Mode

1. Different intentions
In the factory method model, we focus on a product as a whole and do not need to worry about how each part of the product is created. But in the builder model, the production of a specific product depends on the generation and assembly sequence of each component. It focuses on "assembling product objects step by part". Simply put, the factory mode is a coarse-line application for object creation. The Builder mode outlines a complex object through fine lines, focusing on the creation process of product components.
2. Different product complexity
The product created in the factory method mode is generally a single product, while the builder mode creates a composite product, which is composed of various components. Of course, different parts have different product objects. This does not mean that the objects created in the factory method mode are simple, but that they have different granularities. In general, the object granularity of the factory method mode is relatively coarse, and the product object granularity of the builder mode is relatively small.

Practice: If you need to pay attention to the production and installation steps of a product component, select the builder; otherwise, the factory.


2. Abstract Factory mode vs builder Mode

The abstract factory model is like a giant with many "factories" that use "factories" to describe the builders. The Builder model is like a factory with many "workshops ", use "Workshop" to describe the builder. Abstract Factory mode refers to the creation of objects from a higher level. There are still many workshops in the factory, but these are all details hidden in the factory and will not be announced. The builder mode is different. It is composed of workshops. Different workshops complete different creation and Assembly tasks, A complete automobile production process can only be completed through the cooperation of the engine manufacturing workshop and engine assembly workshop. The basis of their cooperation is the design blueprint, which is in the hands of the workshop director (director class ). The builder mode is more concerned with the construction process. Although a workshop still produces vehicles from the outside world, the transformation of this workshop is very fast. You only need to design a blueprint to produce different products.

Practice: The abstract factory model is relatively larger than the builder model. It focuses on the entire product. The builder model focuses on the construction process. Therefore, the builder model can easily build a brand new product, as long as the director class can provide a specific process flow. Because of this, if you want to block the object creation process and only provide one well-encapsulated object, you can select the Abstract Factory method mode. The builder mode can be used in component assembly. For example, by assembling different components or different sequences of the same components, a new object can be generated, which can generate a very flexible architecture, it is convenient to expand and maintain the system.

Create a class mode primary key (Summary)

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.