Abstract Factory pattern of Java design patterns

Source: Internet
Author: User

This article continues to introduce the abstract factory model of 23 design pattern series. The simple factory model and the factory method pattern have been described earlier, and the third plant model-Abstract Factory mode-is still being introduced, as is the case with the manufacture of automobiles.
Example background:as customers become more demanding, BMW needs different configurations of air conditioning and engine accessories. So the factory started to produce air conditioners and engines to assemble cars. At this time the factory has two series of products: air conditioning and engine. The BMW 320 series is equipped with a model air conditioner and a model engine, the BMW 230 series is equipped with B model air conditioning and B model engine.
Concept:Abstract Factory mode is an upgraded version of the factory method pattern that is used to create a set of related or interdependent objects. For example, the BMW 320 series uses air-conditioning model A and engine model A, while the BMW 230 series uses air-conditioning model B and engine Model B, then using the abstract Factory mode, in the production of accessories for the 320 series, there is no need to develop accessories model, it will automatically according to the model of the production of the corresponding accessory model A.
For Baidu Encyclopedia on the Abstract factory model of the introduction, combined with this example is as follows:
when each abstract product has more than one specific subclass (air conditioning models A and b two, the engine also has models A and b two), the factory role how to know which sub-class to instantiate it? For example, each abstract product role has two specific products (product air conditioning has two specific products air conditioning A and air conditioning B). The abstract factory model provides two specific factory roles (BMW 320 series factory and BMW 230 series factory), respectively, corresponding to these two specific product roles, each specific factory role is responsible for the instantiation of one product role. Each specific factory class is only responsible for creating an instance of a specific subclass of the abstract product.

Abstract Factory mode Code

Product Category:

Engine and model public  interface engine {  } public  class Enginea extends engine{public      Enginea () {          System.out.println ("Manufacturing-->enginea");      }  }  public class Enginebextends engine{public      engineb () {          System.out.println ("Manufacturing-->engineb");      }  }  Air conditioning and model public  interface Aircondition {  } public  class Airconditiona extends aircondition{      Public Airconditiona () {          System.out.println ("Manufacturing-->airconditiona");      }  }  public class Airconditionb extends aircondition{public      airconditionb () {          System.out.println ("Make- Airconditionb ");      }  


To create a factory class:

Create a factory interface public  interface Abstractfactory {      //Manufacturing engine public engine    createengine ();    Manufacturing Air Conditioning public     aircondition createaircondition ();}  Public Engine for BMW 320 Series Production Accessories Public  class FactoryBMW320 implements abstractfactory{            @Override      Createengine () {            return new Enginea ();      }      @Override public      aircondition createaircondition () {          return new Airconditiona ();      }  }  BMW 523 Series public class FactoryBMW523 implements Abstractfactory {         @Override public      Engine Createengine () {            return new Engineb ();      }      @Override public      aircondition createaircondition () {          return new airconditionb ();      }  

Customer:

public class Customer {public      static void Main (string[] args) {          //Production BMW 320 Series Accessories        FactoryBMW320 factoryBMW320 = New FactoryBMW320 ();          Factorybmw320.createengine ();        Factorybmw320.createaircondition ();                  Production of BMW 523 Series Accessories          FactoryBMW523 factoryBMW523 = new FactoryBMW523 ();          Factorybmw320.createengine ();        Factorybmw320.createaircondition ();    }  }

on the difference between the abstract factory model and the factory method model, it is not said, feel more than a few examples can understand, there are many references to the product family, hierarchical structure and other concepts, said it is more difficult to understand.


The origin of the abstract factory patternThe following refers to the origin of an abstract factory pattern:
The origin or earliest application of the abstract factory pattern is used to create Windows constructs that belong to different operating systems. For example, the command button and text box (text) are all Windows constructs, in the UNIX operating system Windows environment and Windows Operating System window environment, these two constructs have different local implementation, their details are different. in each operating system, there is a building family consisting of a Windows build. Here is the product family of button and text. Each window component constructs its own hierarchical structure, which is given abstract function description by an abstract role, and the specific subclass gives the specific implementation under different operating systems.
It can be found that in the above product class diagram, there are two product hierarchy structures, namely the button hierarchy and the text hierarchy. There are also two product families, namely the UNIX product family and the Windows product family. The UNIX product family consists of a Unix button and a UNIX text product, while the Windows product family consists of Windows button and the Windows text product.
The requirements for the creation of a product object are met by the hierarchy of a project, which has two specific engineering roles, namely Unixfactory and Windowsfactory. The Unixfactory object is responsible for creating products in the Unix product family, while the Windowsfactory object is responsible for creating products in the Windows product family. This is the application of abstract Factory mode, the solution of abstract Factory mode such as:
Obviously, a system can only run in a Windows environment of one operating system, but not on different operating systems at the same time. Therefore, the system can actually only consume products belonging to the same product family. in modern applications, the use of abstract Factory mode has been greatly expanded, no longer require the system to consume only one product family.

Summary:whether it's a simple factory model, a factory method model, or an abstract factory model, they all belong to the factory model and are very similar in form and feature, and their ultimate goal is to understand the decoupling. In use, we don't have to care whether this pattern is a factory method or an abstract factory pattern, because the evolution between them is often elusive. Often you will find that the factory method is clearly used, when the new requirements come, slightly modified, adding a new method, because the products in the class constitute a different hierarchical structure of the product family, it becomes an abstract Factory mode, and for the abstract factory model, when the reduction of a method of the provided products no longer constitute the product family, It evolved into a factory method model. Therefore, when using the factory model, it is only necessary to care whether the purpose of reducing coupling is achieved.

jason0539

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)

Sweep code Follow me public number


Abstract Factory pattern of Java design patterns

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.