Design Mode: Five creation Modes

Source: Internet
Author: User
Tags lenovo
To enhance my understanding of these five creation modes, I will take computer production as an example. A computer contains many components: CPU, disk, monitor, keyboard, and mouse. Of course, there are five factory types: cpufactory, diskfactory, monitorfactory, keyboardfactory, and mousefactory. Here, the factory mode is used. For example, cpufactory can have intel, AMD, and so on. diskfactory can have maxto and Seaga. These are computer component manufacturers. For a computer component manufacturer, it needs to claim the instances of these factories so that they can be directly generated every time a component is required. So an abstract factory created by managing all computers around the world appeared. People are like their names. This abstract factory is really abstract, and there is no such organization. Class cpufactory {public: virtual CPU * createcpu ();} class intelcpufactory: Public cpufactory {public: virtual CPU * createcpu ();} class amdcpufactory: Public cpufactory {public: virtual CPU * createcpu ();} class mouseprototype {public: Virtual mouse * createmouse ();} class singletongateactcomputerfactory {virtual cpufactory * createcpufactory (); virtual diskfactory * creatediskfactory ();... virtual MOU Seprototype * createmouse ();} Obviously, this abstract factory only needs one, so it is Singleton. In this way, we can make an intelcpufactory anywhere in the world. In the abstract above, both CPU and disk claim a factory. For example, intelcpufactory can be generated to generate maxtodiskfactory. Since many mice make some modifications from a prototype, you can declare the original method. With the above definition, we can Program To obtain a CPU or a disk. For example, we only produce intelcpu in singletongateactcomputerfactory. Code Can also be configured), if you want to produce other new types of CPU, you only need to modify this Singleton class. Now we have all the components. We need a computer. What should we do? We can ask Lenovo to do this. This is builder class builder {public: Virtual void setcpu (CPU * CPU); Virtual void setdisk (Disk * disk ); virtual void linktocomputer ();} class legendbuilder: Public builder {public: Virtual void setcpu (CPU * CPU); Virtual void setdisk (Disk * disk); Virtual Computer * makecomputer ();

}

Here we can see that the difference between builder and abstract factory may be that their declarations are the same, but they do different things. Abstract Factory focuses on creating a few things, while builder focuses on composing existing things into a new complex object. Therefore, Intel is factory, While Lenovo is builder.

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.