Abstract Factory mode of JAVA design mode and java design mode Factory

Source: Internet
Author: User

Abstract Factory mode of JAVA design mode and java design mode Factory
This article continues to introduce the abstract factory model of the 23 design patterns series. We have already introduced the simple factory mode and factory method mode. Here we will continue to introduce the third factory mode-Abstract Factory mode, or take automobile manufacturing as an example.
Example Background: As customer requirements increase, BMW cars need air conditioners, engines, and other accessories with different configurations. So the factory began to produce air conditioners and engines for assembling cars. At this time, the factory has two series of products: air conditioners and engines. The BMW 320 series is equipped with a-type air conditioners and A-type engines. The BMW 230 Series is equipped with B-type air conditioners and B-type engines.
Concept: Abstract Factory mode is an upgraded version of factory method mode. It creates a set of related or mutually dependent objects. For example, if 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, the abstract factory model is used, when producing related accessories for the 320 series, you do not need to specify the accessory model. It will automatically produce the corresponding accessory Model A according to the model.
This section describes the abstract factory model on Baidu encyclopedia:
When each abstract product has more than one concrete subclass (air conditioners have two types: model A and model B, and engines have two types: model A and model B ), how does a factory role know which subclass to instantiate? For example, each abstract Product role has two specific products (product air conditioners have two specific products, air conditioners A and air conditioners B ). The abstract factory model provides two specific factory roles (the BMW 320 series factory and the BMW 230 Series factory), which correspond to the two specific product roles respectively, each specific factory role is only responsible for instantiating a product role. Each factory class is only responsible for creating instances of a specific subclass of an abstract product.

Abstract factory pattern code

Product Type:

// 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 conditioner 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 ("Manufacturing --> AirconditionB ");}}


Create a factory:

// Create a factory interface public interface AbstractFactory {// create an Engine public Engine createEngine (); // create an air conditioner public Aircondition createAircondition ();} // For the BMW 320 series production accessories public class FactoryBMW320 implements AbstractFactory {@ Override public Engine 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) {// produce BMW 320 series accessories FactoryBMW320 factoryBMW320 = new FactoryBMW320 (); factoryBMW320.createEngine (); callback (); // produce the BMW 523 series accessories FactoryBMW523 factoryBMW523 = new FactoryBMW523 (); factoryBMW320.createEngine (); factoryBMW320.createAircondition ();}}

I will not talk about the differences between the abstract factory mode and the factory method mode. I feel that I can understand the differences after reading the examples several times. There are also many concepts such as product family and hierarchical structure, it is more difficult to understand.


Abstract The Origin of the factory ModelThe following describes the origin of an abstract factory model:
The origin or earliest application of the abstract factory model is used to create windows that belong to different operating systems. For example, the command buttons and Text boxes are both Windows. In the Windows environment of the UNIX operating system and Windows environment of the Windows operating system, the two builds have different local implementations, and their details are different. In each operating system, there is a Windows build family. Here is the product family composed of buttons and Text. Each window component has its own hierarchical structure. An abstract role gives an abstract function description, and a specific sub-class gives specific implementations under different operating systems.
We can see that in the above product class diagram, there are two product level structures: the Button level structure and the Text level structure. There are two product families, namely the UNIX product family and the Windows product family. The UNIX product family consists of UNIX buttons and UNIX Text products, while the Windows product family consists of Windows buttons and Windows Text products.
The system needs to create product objects to meet the hierarchical structure of a project. Two specific engineering roles are available: UnixFactory and WindowsFactory. The UnixFactory object is used to create products in the Unix product family, while the WindowsFactory object is used to create products in the Windows product family. This is the application of the abstract factory model. Solutions for the abstract factory model are as follows:
Obviously, a system can only run in a Windows environment of a certain operating system, rather than running on different operating systems at the same time. Therefore, the system can only consume products belonging to the same product family. In modern applications, the scope of use of the abstract factory model has been greatly expanded, and the system is no longer required to consume only one product family.

Summary: both the simple factory model, the factory method model, and the abstract factory model belong to the factory model, which is extremely similar in terms of form and characteristics, their ultimate goal is to decouple. In use, we don't have to worry about whether this mode is factory method mode or abstract factory mode, because the evolution between them is often unpredictable. You will often find that the factory method mode is clearly used. When a new requirement comes and a slight modification is made, after a new method is added, because the products in the class constitute the product family with different levels of structure, it becomes the abstract factory model. For the abstract factory model, when a method is reduced to make the provided product no longer constitute a product family, it becomes the factory method model. Therefore, when using the factory model, you only need to care about whether the goal of reducing the coupling degree has been achieved.

Author: jason0539

Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)

Scan the QR code to follow my public account


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.