Java research-detailed description of abstract factory Model

Source: Internet
Author: User
Tags samsung tablet

Java research-detailed description of abstract factory Model
Zookeeper

Introduction:

When each abstract product has more than one concrete subclass, how does the factory role know which subclass to instantiate? For example, each abstract product [1] has two specific products. The abstract factory model provides two factory roles, which correspond to the two product roles respectively. Each factory role is only responsible for the instantiation of one product role. Each factory class is only responsible for creating instances of a specific subclass of an abstract product.

Each mode is a solution to a certain problem. The factory method mode is for a product level structure, while the abstract factory mode is for multiple product level structures. (From Baidu encyclopedia)

The words are too abstract. The best way for programmers to express code is to use uml class diagrams.

UML class diagram:

From the preceding class diagram, we can see that there are two product families: Abstract product 1 and abstract product 2. Each factory can produce products of multiple product families, for example, product 1 produces both product 11 and product 21.

Product Family: Refers to a family of products with functions associated with different product levels. For example, product 11 and product 12 form a product family.

Code profiling:


12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 // Product family 1 interface implements actproduct1 {} class Product11implements implements actproduct1 {public Product11 () {System. out. println ("Product 11") ;}} classProduct12 implementsAbstractProduct1 {publicProduct12 () {System. out. println ("Product 12") ;}// product family 2interface?actproduct2 {} classProduct21 implements=actproduct2 {publicProduct21 () {System. out. println ("Product 21");} class Product22implements AbstractProduct2 {public Product22 () {System. out. println ("produced product 22") ;}} interface AbstractFactory {AbstractProduct1 createP1 (); // The product AbstractProduct2 createP2 () that produces product family 1 (); // produce product of Product Family 2} // The specific factory 1 is determined by the specific factory 1. Which product of each product family is produced? classFactory1 implements?actfactory {public=actproduct1 createP1 () {return newProduct11 ();} public AbstractProduct2createP2 () {returnnew Product21 () ;}// factory 2, the specific factory 2 decides which product class Factory2implements of each product family is produced. actfactory {public AbstractProduct1createP1 () {returnnew Product12 ();} publicpolicactproduct2 createP2 () {return newProduct22 ();}}

Differences between Abstract factories and specific factories:

Factory method mode: An abstract product class that can be derived from multiple specific product classes. An abstract factory class can be derived from multiple factory classes. Each factory class can only create one instance of a specific product class.

Abstract Factory Model: Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes. An abstract factory class can be derived from multiple factory classes. You can create multiple product instances for each specific factory class.

Differences: The Factory method mode has only one abstract product class, while the abstract factory mode has multiple. The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.

When to use the abstract factory mode:

1. A system should not depend on how product instances are created, combined, or expressed in detail, which is important for all forms of factory models.

2. This system has more than one product family, and the system only consumes one of them. (This is the original meaning of the abstract factory model)

3. products belonging to the same product family are used together. This constraint must be reflected in the system design.

4. The system provides a product library. All products share the same interface implementation, but the client does not rely on the specific implementation.

More in line with the open and closed principles:

The open/closed principle requires a software system to enhance its functions by extending without modifying the original code.

The abstract factory model is more supported for adding a new product family.

Instance boot:

Assume that Foxconn works as a OEM product for two brands: Apple and Samsung. Both brands have mobile phones and tablets. Due to different production processes, Foxconn has opened two production lines, one line only produces mobile phones, and the other line only produces tablets. Apple launched a new product and ordered the production of iPad from Foxconn. The head of Foxconn went to the tablet production factory and demanded the production of Apple pad. Soon the iPad was produced. Apple has a new iphone to be produced, and orders it from Foxconn. The head of Foxconn turned to the factory that produces mobile phones and demanded iphone production. Soon the iPhone was ready.

Sample class diagram:

Sample Code:


123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 // Interface of the Apple product family Apple {} classIphone implementsApple {// publicIphone () {System. out. println ("iphone");} class Ipadimplements Apple {// Apple pad publicIpad () {System. out. println ("ipad") ;}// SamSung interfacesamments {} class SanSungPhoneimplements SamSung {// SamSung mobile phone publicSanSungPhone () {System. out. println ("Samsung mobile phones produced") ;}} classSamSungPad implementsSamSung {// Samsung flat publicSamSungPad () {System. out. println ("Samsung tablet") ;}// Foxconn factory abstract interface interfaceFuShiKang {Apple createApple (); // SamSungcreateSamSung () for the production of Apple products (); // products of the Samsung product family} // Foxconn's cell phone factory class PhoneFactoryimplements FuShiKang {publicApple createApple () {// returnnew Iphone ();} publicSamSung createsamloud () {// produce SamSung mobile phone returnnew SanSungPhone () ;}// Foxconn specializes in the production of tablet computers in the classPadFactory implementsFuShiKang {publicApple createApple () {// Apple ipad returnnew Ipad () ;}publicsamsung createSamSung () {// Samsung tablet returnnew SamSungPad ();}} public classMain {publicstatic voidmain (String [] args) {// The orderer came to Foxconn and ordered an iphone. The owner told the factory that produced the mobile phone to process and produce FuShiKangfactory = newPhoneFactory (); apple iphone = factory. createApple (); // then ask for some SamSung tablets, so the person in charge tells the production factory of the tablet to process and produce factory = newPadFactory (); SamSung samsungPad = factory. createSamSung ();}}

Result:

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.