Design Pattern learning-daily note (5. creator pattern)

Source: Internet
Author: User

Creator Mode

A. Definition:

Separates the creation process and representation of a complex object so that different representations can be constructed for the same creation process.

B. Definition Analysis:

" SetComplex objectsThe creation process and representation separation, so that different representations can be constructed for the same creation process. "We assume that an object is very complex, and many components are Pa, Pb, PC, and PD.

" SetComplex objectsOfCreation process and representation SeparationSo that different representations can be constructed for the same creation process. "The interfaces for building and creating are buildpa, buildpb, buildpc, and buildpd, and each part has multiple options of raw materials. the raw material we are talking about, for example, color, is "representation ". the creation process is a call to the buildpx interface, which will never change. the Creator mode allows the process to be separated from the results created for each part, that is, decoupling.

"ReplaceComplex objectsOfCreation process and representation Separation,So that different representations can be constructed for the same creation process. "After decoupling, the same creation process can be expressed differently.

C. Problem

(1) how to decouple it?

Set a director role to organize the builder process. This process will never change.

Class director {

Build_product (){

Builder. buildpa ();

Builder. buildpb ();

Builder. buildpc ();

Builder. buildpd ();

}

};

Builder has four interfaces, such as buildpx, but it is not implemented by itself. Different subclasses represent different representations and assembly methods.

Class builder {

Buildpa ();

Buildpb ();

Buildpc ();

Buildpd ();

};

For example, red color is used when redcolorbuilder builds Pa.

Class redcolorbuilder: Public Builder {

Buildpa ()

{

Setredcolor ();

}

};

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.