(11) reading (big talk Design Model) Abstract Factory Model

Source: Internet
Author: User

(11) reading (big talk Design Model) Abstract Factory Model

After learning the design pattern for a while, there will be an individual meeting on the design pattern, that is, the design program follows some principles to make the code reusable, during the modification, there is no need to involve too many classes. It is easy to expand and abstract. The name of the abstract factory mode sounds abstract, but it is not abstract at all. It may not be easy to understand in languages. I like to understand it through code, and then become your own things.

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

< <大话设计模式> > I want to use another database as an example when the project went online. If I didn't use SqlServer and now I switched to access, the Code is as follows:

1. Access the database Factory

/**

*@ AuthorJzhf

* 2014-11-03

* Create a factory interface to access the user

*/

Public interfaceIfacloud {

IUser createUser ();

IDepartment createDepartment ();

}

IUser refers to operations on user tables, and IDepartment refers to operations on department tables. These two classes are described below.

2. SQL Server database factory operations

Public classSqlServerFactoryImplementsIfacloud {

/* (Non-Javadoc)

* @ Seecom. test. model. abstractFactory. ifacloud # createUser ()

*/

PublicIUser createUser (){

//TODOAuto-generatedmethod stub

Return newSqlServerUser ();

}

/* (Non-Javadoc)

* @ Seecom. test. model. abstractFactory. ifacloud # createDepartment ()

*/

PublicIDepartment createDepartment (){

//TODOAuto-generatedmethod stub

Return newSqlServerDepartment ();

}

}

3. Perform user operations on specific classes

/**

*@ AuthorJzhf

* 2014-10-31

*/

Public classSqlServerUserImplementsIUser {

/* (Non-Javadoc)

* @ Seecom. test. model. abstractFactory. IUser # getUser (int)

*/

Public voidGetUser (IntId ){

//TODOAuto-generatedmethod stub

System.Out. Println ("sqlserverget User by Id ");

}

/* (Non-Javadoc)

* @ Seecom. test. model. abstractFactory. IUser # insert (com. test. model. abstractFactory. User)

*/

Public voidInsert (User user ){

//TODOAuto-generatedmethod stub

System.Out. Println ("sqlserverinsert user ");

}

}

4. User Interface

Public interfaceIUser {

VoidInsert (User user );

VoidGetUser (IntId );

}

5. The Department class is similar to the user code, slightly; the access database code is consistent with the SQL Server database code, slightly

6. Client

Public classUserClient {

/**

*@ ParamArgs

*/

Public static voidMain (String [] args ){

//TODOAuto-generatedmethod stub

IFactory factory =NewSqlServerFactory ();

IUser iUser = factory. createUser ();

User user =NewUser ();

User.SetId(0 );

User. setName ("Xiao Wang ");

IUser. insert (user );

IUser. getUser (0 );

IFactory factory2 =NewAccessFactory ();

IUser iUser2 = factory2.createUser ();

IUser2.insert (user );

IUser2.getUser (0 );

// ----------- --------- After adding the Department Interface -----------

IDepartmentIDepartment= Factory. createDepartment ();

// Then modify the input values of insert.

}

}

From the code and the icon below, ifacloud is an abstract factory interface, which contains the Abstract METHODS created by all products, while SqlServerFactory and accessFactory are specific factories, this specific factory then creates product objects with specific implementations, that is, to create different product objects, the client should use different specific factories.

Advantages and disadvantages of Abstract Factory

Advantage: it is easy to switch the product series. Because of the specific factory class, it only needs to appear once during initialization in an application, which makes it very easy to change the specific factory of an application, it only needs to change the specific factory to use different product configurations. The second advantage is that it separates the process of creating an instance from the client. The client uses their abstract interfaces to manipulate the instance, and the specific class name of the product is also separated by the implementation of the specific factory, does not appear in client code. The principles of openness and closeness and dependency reversal are followed.

The disadvantage is that if I want to add a function, I need to add three classes: abstract factory class, factory specific class and specific implementation class. I also need to change the original abstract factory to fully implement it, very troublesome.

Attached structure:

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.