Design Pattern 2 abstract factory pattern (Abstract Factory)

Source: Internet
Author: User
Concept

Provides an interface for creating a series of related or related dependent objects without specifying their specific classes. That is to say, we often learn how to create a series of dependent objects, however, due to changes in requirements, more objects are often created, so that you do not need to use the conventional object creation method (new ), is there a "encapsulation mechanism" to avoid strong coupling between customer programs and such "generation object creation work? Reducing the strong coupling between the client and creation, we introduced the abstract factory model.

Purpose

Provides an interface for creating a series of related or dependent objects without specifying their specific classes.

Logic Diagram

Structure chart

Differences between Abstract Factory mode and factory method mode

Abstract Factory mode is an upgraded version 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 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.

Code
// The specific factory class concretefactory2: abstractfactory {public override abstractproducta createproducta () {return New producta2 ();} public override abstractproductb createproductb () {return New productb2 ();}} // abstract product A abstract class abstractproducta {}// abstract product B abstract class abstractproductb {public abstract void interact (abstractproducta A);} // implement a class producta1: abstractproducta {} // implement B1 class productb1: abstractproductb {public override void interact (abstractproducta A) {console. writeline (this. getType (). name + "interacts with" +. getType (). name) ;}}// implement A2 class producta2: abstractproducta {}// implement B2 class productb2: abstractproductb {public override void interact (abstractproducta A) {console. writeline (this. getType (). name + "interacts with" +. getType (). name) ;}}// client class client {private abstractproducta; private abstractproductb; // constructor public client (abstractfactory factory) {abstractproductb = factory. createproductb (); abstractproducta = factory. createproducta ();} public void run () {abstractproductb. interact (abstractproducta );}}
Specific instance:
// Interface idepartment {void insert (Department); Department getdepartment (int id);} // inherits from idepartment class sqlserverdepartment: idepartment {public void insert (Department) {console. writeline ("adding a record to the Department table in sqlserver");} public department getdepartment (int id) {console. writeline ("getting a department table record by ID in sqlserver"); return NULL ;}// inherit from idepartment class accessdepartment: idepartment {public void insert (Department) {console. writeline ("adding a record to the Department table in access");} public department getdepartment (int id) {console. writeline ("obtain a department table record by ID in access"); return NULL ;}// Abstract Factory interface ifacloud {iuser createuser (); idepartment createdepartment ();} // accessfactory class, which implements the ifactory interface, instantiates accessuser and accessdepartment class accessfactory: ifacloud {public iuser createuser () {return New accessuser ();} public idepartment createdepartment () {return New accessdepartment () ;}// sqlserverfactory class, implements the ifactory interface, instantiates sqlserver and sqlserverdepartment class sqlserverfactory: ifacfactory {public iuser createuser () {return New sqlserveruser ();} public idepartment createdepartment () {return New sqlserverdepartment ();}}
Advantages of abstract factory Model

Abstract Factory mode not only has the advantages of factory method mode, but also has the primary advantage of being able to restrict 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.

It is easy to switch the product series. Because a specific project only needs to appear once during initialization in an application, it is very easy to change the specific factory of an application, it only needs to change the specific factory to use different product configurations.

It separates the instance creation process from the client. The client operates instances through their abstract interfaces. The specific class names of the products are also separated by the implementations of specific factories and will not appear in the client.

Disadvantages of abstract factory Model

Product Family expansion is a very laborious task. If a new product needs to be added to the product family, almost all factory classes need to be modified. Therefore, the classification of product hierarchy is very important when abstract factory models are used.

Applicable scenarios

Abstract Factory mode can be used when the object to be created is a series of interrelated or interdependent product families. A more clear point is that in an inheritance system, if there are multiple levels (that is, there are multiple abstract classes ), the abstract factory mode can be used if there are certain associations or constraints between implementation classes in each hierarchy. 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. The form and characteristics are extremely similar. Their ultimate goal is to decouple them. 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.

 

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.