Abstract Factory mode and factory mode Summary

Source: Internet
Author: User

The simple factory mode and the factory mode are described above. This section describes the abstract factory mode. At last, we will summarize the three models to deepen our understanding of these models.

  

Definition:

Abstract Factory mode: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

Role:

Abstract Factory: declare methods to generate a series of abstract products. Specific Factory: execute a series of abstract product generation methods to generate a series of specific products.

Abstract product: Declares interfaces of a series of products. Specific products: define specific product objects generated by a specific factory to implement product interfaces.

UML diagram:

  

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + CqGhoaG + records/uxL/records/b7dv + records/b7dv + LBrL3T0 ++ records/uxL/records/b7dv + KjrM6qwcu4/LzTsePA + 7 XYveK + records/Oy + sa3us2 + 38zlsvrgt73hyaujuqghpc9wpgokpgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140412/20140412090645182.gif" alt = "Copy code">

Class User {private int id; public int ID {get {return id;} set {id = value ;}} private string name; public string Name {get {return name ;} set {name = value ;}} interface IUser {void Insert (User user);} class SqlserverUser: IUser {public void Insert (User user) {Console. writeLine ("adding a record to the User table in sqlserver") ;}} class AccessUser: IUser {public void Insert (User user User) {Console. writeLine ("adding a record to the User table in Access ");}}

Abstract Factory and specific factory roles:

interface IFactory    {        IUser CreateUser();    }    class SqlserverFactory : IFactory    {        public IUser CreateUser()        {            return new SqlserverUser();        }    }    class AccessFactory : IFactory    {        public IUser CreateUser()        {            return new AccessUser();        }    }

Client call:

User user = new User();            IFactory factory = new SqlserverFactory();            IUser iu = factory.CreateUser();            iu.Insert(user);

After reading the above example, I feel like the factory model, not the abstract factory, right? So do I. Let's look at the UML diagram:

Below is a piece of code:

View Code

Its UML class diagram is as follows:

  

The abstract factory mode is defined to provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

Factory mode definition: defines an interface for creating objects, so that the subclass determines which class to instantiate.

Simple Factory: A class is responsible for creating instances of other classes. The specific instance creation is determined by the input parameters, and these classes all inherit from a parent class or have a common interface.

The differences between the abstract factory UML class diagram and the factory model UMl class diagram seem to be obvious. The abstract factory model provides a series of object interfaces, while the factory model is not. (I don't know if I understand it ). Abstract Factory mode has multiple methods to create multiple products. The factory mode has only one method to create a product. (Multiple methods can be added to IUser .) Since the abstract factory and factory model UML diagrams are available, today we put the simple factory together to compare the results.

Simple factory UML:

The system creates the required instance based on the external information, so the client does not have to care about how the product is produced, but only specifies the product to be produced.

Factory mode: Let the subclass decide what product to create, that is, delay the instantiation of a class to the subclass.

Abstract Factory mode: This mode facilitates the exchange of product series, and isolates the process of creating an instance from the client. Clients manipulate instances through their abstract interfaces, the specific Class Name of the product is also separated by the implementation of the specific factory and will not appear on the client.

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.