Java Factory mode

Source: Internet
Author: User

The Java Factory model is divided into three types: Simple Factory mode, factory method mode, abstract Factory mode.

Simple Factory pattern is the innovative model of the class, also called the Static Factory method pattern (FactoryMethod), which is responsible for creating instances of other classes by specifically defining a class. The instances that are created typically have a common parent class.

The simple factory pattern is the use of an "all-in-one class" that determines which specific class of objects to create based on the information conveyed by the outside world. such as (Lazy to write the demo, know the last cut one.) ):

With a simple factory class, the corresponding product object is created according to name.

About the simple class if you have not understood, more information can refer to http://blog.csdn.net/weiwenlongll/article/details/6918164.

The simple factory model violates the "open and close principle", which is difficult to expand, thus producing the factory method pattern.

The factory approach model (FACTORY method) is an abstraction of the simple factory model, which conforms to the "open and close principle" and is extensible.

The meaning of the factory method mode is to define a factory interface that creates the product object and defer the actual creation to the subclass Factory. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles.

Factory method Pattern Usage Scenarios:

such as the generation of mobile phone assembly factory. From the cell phone raw materials factory to obtain enclosures, displays, motherboards, keys, batteries and other accessories for assembly. Assembly Cell phone Factory is only responsible for mobile phone assembly, and not responsible for the production of spare parts, do not need to care about the equipment from the cell phone factory accessories change, as long as the mobile phone configuration interface is not changed. For example, the raw material factory display from the TFT replaced by the UFB display, for the assembly of mobile phone generation factory, as long as the interface has not changed, just need to continue to assemble the line.

For example, first set up a raw material factory interface, the raw material Factory interface defines the method of creating the shell, display, motherboard, battery, respectively.

LB cell phone raw materials Factory is responsible for the LB mobile phone needs accessories, batteries, TFT display, motherboard, leather shell.

The following is the interface of the handset assembly factory, the Assembly factory is only responsible for obtaining accessories from the raw material factory and assembling the new mobile phone, the other is not responsible.

LB cell Phone assembly plant, responsible for assembling lb raw materials factory supplies accessories. Of course, occasionally also pick up the reverse telecommuting, assemble other mobile phone factory phone.

LB mobile phone sales are very hot, all over the country have an assembly plant. One day the screen of LB mobile phone will be changed suddenly, from the TFT display to uft display. Reaching the benefits of the factory approach model. No matter how the LB phone configuration changes, the Createdisplay () interface remains the same for assembly plants. This requires that the display of the raw material factory be replaced with the UFT display from the TFT display. The benefit of the factory model is that it provides the user with the product interface created, regardless of the type of product, as long as the functionality of the product created by the interface does not change, the user does not need to make any changes.

Java Factory mode

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.