23 design patterns (3): Abstract Factory patterns

Source: Internet
Author: User
Definition:Provides an interface for creating a set of related or interdependent objects without specifying their detailed classes.

Type:Create Class Mode

Class diagram:

Differences between Abstract Factory mode and factory method mode

Abstract Factory mode is the update version number of the factory method mode. It creates a group of related or mutually dependent objects. The difference between the factory method model and the factory method model is that the factory method model is for a product level structure, while the abstract factory model is for multiple product level structures. In programming, a product structure is usually represented as an interface or abstract class. That is to say, all the products provided by the factory method mode are derived from the same interface or abstract class, the abstract factory model provides products derived from different interfaces or abstract classes.

In the abstract factory model, there isProduct FamilyConcept: the so-called product family refersA family of products with functions associated with different product levels. Abstract A series of products provided by the factory model constitute a product family, and a series of products provided by the factory method are called a hierarchical structure. We still use the example of producing cars to illustrate the differences between them.

In the above class diagram, the hatchback and the sedan are called two different hierarchical structures, while the 2.0 and 2.4 displacement vehicles are called two different product families. To be more detailed, the 2.0-displacement hatchback and the 2.4-displacement hatchback belong to the same level structure. The 2.0-displacement sedan and the 2.4-displacement sedan belong to another level structure; while 2.0-displacement hatchways and 2.0-displacement sedan cars belong to the same product family, 2.4-displacement hatchways and 2.4-displacement sedan cars belong to another product family.

After clarifying the hierarchical structure and product family concepts, we can understand the differences between the factory method model and the abstract factory model. If all the factory products belong to the same hierarchical structure, they belong to the factory method model; assume that the factory products come from multiple hierarchical structures and belong to the abstract factory model. In this example, assume that a factory model provides a 2.0-displacement hatchback and a 2.4-displacement hatchback, then it belongs to the factory model; assume that a factory model provides two products: A 2.4-displacement hatchback and a 2.4-displacement sedan, the factory model is an abstract factory model, because the products it provides belong to two different levels of structure. Of course, assume that a factory provides products of all four models. Because the products belong to two hierarchical structures, they also belong to the abstract factory model.

Abstract factory pattern code

Interface iproduct1 {public void show ();} interface iproduct2 {public void show ();} class product1 implements iproduct1 {public void show () {system. out. println ("this is a type 1 product");} class product2 implements iproduct2 {public void show () {system. out. println ("this is type 2 Product") ;}} interface ifacloud {public iproduct1 createproduct1 (); Public iproduct2 createproduct2 ();} class factory implements ifacloud {public iproduct1 createproduct1 () {return New product1 ();} public iproduct2 createproduct2 () {return New product2 () ;}} public class client {public static void main (string [] ARGs) {ifactory factory = new factory (); factory. createproduct1 (). show (); factory. createproduct2 (). show ();}}

Abstract strengths of the factory Model

Abstract Factory mode apart from the advantages of factory method mode, the most basic strength is to be able to constrain the product family within the class. The so-called product family generally has a certain degree of Association. The abstract factory model can define and describe the association between product families within the class, instead of introducing a new class for management.

 

Disadvantages of abstract factory Model

Expansion of the product family will be a very laborious task. If you need to add a new product in the product family, then almost all the factory classes need to be modified. Therefore, when using the abstract factory model, it is very important to divide the product hierarchy.

 

Applicable scenarios

Abstract Factory mode can be used when the object to be created is a series of products that are correlated or dependent on each other. It is more clear that in an inheritance system, assume that there are multiple levels (that is, there are multiple abstract classes ), in addition, there are certain associations or constraints between implementation classes in each hierarchy, so we can use the abstract factory mode. If there is no association or constraint between implementation classes in each hierarchy, it is more appropriate to create products using multiple independent factories.

 

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 form and characteristics. Their ultimate goal is to decouple. In use, we don't have to worry about whether the factory method mode or the abstract factory mode of this mode, because the evolution between them is often puzzling. You will often find that the factory method model is clearly used. When a new requirement comes and a slight change is made, 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 coupling degrees has been achieved.

 

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.