"Java design mode" Factory mode

Source: Internet
Author: User

Simple Factory mode

The simple factory model is not one of 23 kinds, in short, there is a class that specializes in producing a product.
For example, in the mouse factory, specializing in the production of mouse, to parameter 0, the production of Dell Mouse, to parameter 1, the production of HP Mouse.

Example code:

//AThe product interface has only one product interface, which indicates that it is a class of products the following abstract Factory mode has multiple product interfaces, multiple types of products Public Interfaceproducta{};//Product categoryimplement the same product interface for different products of the same type Public classProductA1Implementsproducta{} Public classProductA2Implementsproducta{} Public classProductA1Implementsproducta{}//AThe factory class is called by it to call different product classes to produce the above products Public classSimplefactory { Public StaticProduct Factory (String productName)throwsexception{if(Productname.equals ("Product1")){             return NewProduct1 (); }Else if(Productname.equals ("Product2")){             return NewProduct2 (); }Else if(Productname.equals ("Product3")){             return NewProduct3 (); }Else{             Throw NewException ("No Product"); }     } } //with this factory class, we can start placing orders, Simplefactory will decide what products to produce according to different order classes.  Public Static voidMain (string[] args) {Try{simplefactory.factory ("Product1"); Simplefactory.factory ("Product2"); Simplefactory.factory ("Product3"); } Catch(Exception e) {e.printstacktrace (); } } 

The core of a simple factory is a simplefactory class that has the necessary logical judgment and the right to create all the products, and we just have to give him the order to get the product we want. This seems to be very convenient to use.
But, in fact, this simplefactory has a lot of limitations.

First, every time we want to add a new product, we have to modify the original code of Simplefactory.

Secondly, when we have a lot of products, and there are complex hierarchical relationship between products, this class must have complex logic judgment ability, and its code will continue to surge, which is the maintenance of the future is simply a horror of two words ...
There is, the whole system is heavily dependent on the Simplefactory class, as long as the Simplefactory Class A problem, the system will enter the state of the inability to work, which is also the most deadly point ....
These deficiencies will be resolved in two other states of the factory model.

Factory mode

Factory mode is the mouse factory is a parent class, there is the production of mouse this interface.
Dell Mouse factory, HP mouse factory inherit it, can be produced separately Dell Mouse, HP mouse.
What kind of mouse is produced is no longer determined by the parameters, but is created by the Dell Mouse factory when the mouse factory is created.
Subsequent calls to the mouse factory directly. Production Mouse ()

Example code:

//a product interface has only one product interface, stating that it is a class of products the following abstract Factory mode has multiple product interfaces, multiple types of products Public Interfaceproducta{};//implement the same product interface for different products of the same type Public classProductA1Implementsproducta{} Public classProductA2Implementsproducta{}//Factory Interface Public Interfacefactory{ PublicProduct Create ();} //Multiple Product plants Public classProducta1factoryImplementsfactory{ Publicproducta Create () {return NewProductA1 (); } }   Public classProducta2factoryImplementsfactory{ Publicproducta Create () {return NewProductA2 (); } }  

The code for creating a Product object from above shows that the main difference between a factory method and a simple factory is that a simple factory places the function of creating a product in a class, And the factory method put different products in the implementation of the factory interface of the different factory class, so that even if one of the factory class out of the problem, other factory class can work normally, each other is not affected, and then add new products, but only to add a factory interface to implement the factory class, you can achieve without modifying the existing code . But factory methods also have his limitations, that is when the face of the product has a complex hierarchical structure, for example, the factory in addition to the production of home appliances, but also the production of mobile phone products, so that home appliances, mobile phones are two major product families, the two families below contains a large number of products, each product has multiple models, This creates a complex product tree. If the factory method to design this product family system, you must create a corresponding factory class for each model of the product, when there are hundreds of or even thousands of products, also must have the corresponding hundreds of thousands of factory class, this is the legend of the class explosion, for future maintenance, is simply a disaster ....

Abstract Factory mode

Abstract Factory mode is not only the production of mouse, but also the production of keyboards.
That is, the PC manufacturer is a parent class, has the production mouse, the production keyboard two interfaces.
Dell Factory, HP factory inherits it, can produce Dell mouse + Dell keyboard separately, and HP mouse + HP keyboard.
When the factory is created, it is created by The Dell factory.
Follow-up plant. Production mouse () production of Dell Mouse, factory. The production keyboard () produces a Dell keyboard.

In the abstract factory model, suppose we need to add a factory

Assuming we add ASUS factory, we need to add Asus factory, like Dell Factory, inherit PC vendor.
After creating the Asus mouse, inherit the mouse class. Create an ASUS keyboard and inherit the keyboard class.
Can.

In the abstract factory model, suppose we need to add a product

Assuming we add the headset to this product, first we need to add the headset to the parent class, plus the Dell headset, HP headset, two subcategories.
Then in the PC maker this parent class, the production of the headset interface is increased. Finally, in the Dell Factory, HP factory, the two categories, respectively, the production of Dell headsets, HP headset features.
Above.

The factory method is used to create a product , it has no concept of classification, and the abstract factory is used to create a series of products , so the product Classification becomes the focus of the abstract factory ,

Example code:

The correspondence between graphs and codes

Pcfactory-Factory

Hpfactory-Factory1

Dellfactory-Factory2

Mouse-ProductA

Hpmouse-ProdectA1

Dellmouse--ProductA2

Keybo-PRODUCTB

Hpkeybo-ProdectB1

Dellkeybo--ProductB2

//multiple product interfaces, multiple product types Public Interfaceproducta{}; Public Interfaceproductb{};//various types of products (A, B) There are also categories under each product A1 A2 B1 B2 Public classProductA1Implementsproducta{} Public classProductA2Implementsproducta{} Public classProductB1Implementsproducta{} Public classProductA2Implementsproducta{}//Factory Interface Public Interfacefactory{ Publicproducta createproducta ();  PublicPRODUCTB CREATEPRODUCTB ();} //each plant in a different factory creates a series of products Public classFactory1Implementsfactory{//create a Class A product        PublicProductA1 Createwasher () {return NewProductA1 (); }         //Create a Class B product        PublicProductB1 Createicebox () {return NewProductB1 (); } }   Public classFactory2Implementsfactory{//create a Class A product        PublicProductA2 Createwasher () {return NewProductA2 (); }         //Create a Class B product        PublicProductB2 Createicebox () {return NewProductB2 (); } } 

PS Summary:

1 Simple Factory mode: only one product interface (one product) a factory class that returns an instance of the corresponding product according to the given parameters

2 Factory mode: Only one product interface (one product) multiple factory classes, each returning their own product instance benefits compared to the simple factory model: In the case of a factory class, the factory class is paralyzed, all products are not produced, and in the case of more than one factory class, a Factory class hanging off, do not affect the production of other products

3 Abstract Factory mode: Multiple product interfaces (heavy in product category) multiple factory classes, each factory class to create an instance of all products compared to the factory model, the abstract factory is used to create a series of products, so the product Classification is the focus of the abstract factory

Reference

Http://www.cnblogs.com/zhouqiang/archive/2012/07/20/2601365.html

"Java design mode" 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.