(3) Abstract Factory mode

Source: Internet
Author: User
Tags hosting

One: The advantages of the abstract factory model

---> Abstract Factory mode is the object creation mode, which is a further extension of the factory method pattern.

---> Assume that a subsystem requires some product objects, and these products belong to more than one product hierarchy. The abstract factory model can be introduced in order to separate the responsibility for consuming these product objects from the responsibility of creating these product objects. In this case, the consumer product does not need to directly participate in the creation of the product, but only to a common factory interface to request the required products.

---> by using the Abstract factory pattern, you can handle the creation of product objects in multiple product families with the same (or similar) hierarchy.

Two: The drawbacks of the abstract factory model

--->


Three: Application Scenarios

--->/**
* Abstract Factory
* @author SXF
* Product Grade Concept: Intel's CPU and AMD CPU are a product level.
* Intel's motherboard and AMD CPU are a product class.
*
* Product Family concept: Intel's CPU and hosting is a product family of
* AMD's CPU and motherboard are a product family.
*
* Simple Factory mode: [Vertical Division]
* (1) to produce different products of the same grade. For example: passing in different parameters, generating Intel's CPU or AMD's CPU
* (2) Only one core factory class, Judge more, business complex, expand change Core class, trouble.
*
* Factory method Mode: [Vertical Division]
* (1) to produce different products of the same grade.
* (2) a product has its own factory class. Add a new product, just one more specific factory class.
* (3) Product single, belong to a level of products
*
* Abstract Factory mode: [Horizontal Division]
* (1) Facing the generation of products of the same family
* (2) A product family has its own factory class.
* (3) Intel Factory: Production CPU, motherboard, memory. AMD Factory: Production CPU, motherboard, memory
*
*/

IV: The role evolution of the abstract factory model






Five: code example for abstract Factory mode

[1] Abstract Factory

1  Packagecom.yeepay.sxf.interfaces;2 /**3 * Abstract Factory4  * @authorSXF5 * Product Grade Concept: Intel's CPU and AMD CPU are a product level. 6 * Intel's motherboard and AMD CPU are a product class. 7  * 8 * Product Family concept: Intel's CPU and hosting is a product family of9 * AMD's CPU and motherboard are a product family. Ten  *  One * Simple Factory mode: [Vertical division] A * (1) to produce different products of the same grade. For example: passing in different parameters, generating Intel's CPU or AMD's CPU - * (2) Only one core factory class, Judge more, business complex, expand change Core class, trouble.  -  *  the * Factory method mode: [Vertical division] - * (1) to produce different products of the same grade.  - * (2) a product has its own factory class. Add a new product, just one more specific factory class.  -  *  + * Abstract Factory mode: [Horizontal division] - * (1) Facing the generation of products of the same family + * (2) A product family has its own factory class. Intel's factory: Production CPU, motherboard, memory. AMD Factory: Production CPU, motherboard, memory A  * at   */ -  Public InterfaceAbstartfactoryforgroup { -     /** - * The department that created the CPU -      * @paramCPU -      * @return in      */ -      PublicCPU Create (String CPU); to     /** + * The department that created the motherboard -      * @paramCPU the      * @return *      */ $      PublicZhuban Create (CPU CPU);Panax Notoginseng}
View Code

[2] different factories

1  PackageCom.yeepay.sxf.interfaces.impl;2 3 ImportCom.yeepay.sxf.interfaces.AbstartFactoryForGroup;4 ImportCom.yeepay.sxf.interfaces.Cpu;5 ImportCom.yeepay.sxf.interfaces.ZhuBan;6 7 /**8 * Intel's Factory9  * @authorSXFTen  * One  */ A  Public classIntelfactoryImplementsabstartfactoryforgroup{ -     /** - * Create CPU the      */ - @Override -      PublicCPU Create (String CPU) { -         //TODO auto-generated Method Stub +         return NULL; -     } +     /** A * Create motherboard at      */ - @Override -      PublicZhuban Create (CPU CPU) { -         //TODO auto-generated Method Stub -         return NULL; -     } in } -  to  +  PackageCom.yeepay.sxf.interfaces.impl; -  the ImportCom.yeepay.sxf.interfaces.AbstartFactoryForGroup; * ImportCom.yeepay.sxf.interfaces.Cpu; $ ImportCom.yeepay.sxf.interfaces.ZhuBan;Panax Notoginseng /** - * AMD's Factory the  * @authorSXF +  * A  */ the  Public classAmdfactoryImplementsabstartfactoryforgroup{ +  - @Override $      PublicCPU Create (String CPU) { $         //to create an implementation of its CPU -         return NULL; -     } the  - @OverrideWuyi      PublicZhuban Create (CPU CPU) { the         //to create an implementation of its motherboard -         return NULL; Wu     } -  About      $}
View Code

[3] Product interface

1  Packagecom.yeepay.sxf.interfaces;2 /**3 * CPU Interface4  * @authorSXF5  *6  */7  Public InterfaceCpu {8          Publicstring Readneicun (string neicun);9 }Ten  One  A  Packagecom.yeepay.sxf.interfaces; - /** - * Motherboard Interface the  * @authorSXF -  * -  */ -  Public InterfaceZhuban { +      PublicString readcpu (CPU CPU); -}
View Code

(3) abstract 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.