Abstract engineering pattern of design pattern

Source: Internet
Author: User

1. Definition: Provides an entity class that creates a series of related or interdependent object interfaces without specifying them.

2. Extension:

Each pattern is a solution to a certain problem. The problem of abstract Factory mode is the system design of multi-product hierarchy.
Before you learn the concrete examples of an abstract factory, you should understand two important concepts: product family and product grade.

Product family: Refers to the family of products that are related to the function in different product grade structures. such as AMD's CPU and ADM chip motherboard, the formation of a family. Intel's CPU and Intel chip motherboards are also part of a family. And both families come from two product levels: CPU, motherboard. A hierarchical structure is made up of products of the same structure, as follows:

Understanding the structure of the product is the key to understanding the abstract factory model, and it can be seen that each factory in the abstract factory model creates a family of products instead of one or a group. Groups are free to mix! In fact, the factory method model and the abstract factory model this point difference.

2. Composition:

Abstract Factory:

Specific factory:

Abstract Products:

Specific Products:

3.UML class Diagram:

Let's take a look at the UML diagram of the abstract factory pattern:

Is the Abstract Factory schema structure diagram, allowing us to make a more convenient description:

    1. Abstractproduct: Abstract products, they all have the potential to have two different implementations.
    2. Concreteproduct: Including ProductA and PRODUCTB, the implementation of the specific classification of two abstract products.
    3. Abstractfactory: Abstract factory interface, which should contain all the abstract methods of product creation.
    4. Concretefactory: Includes Concretefactorya and CONCRETEFACTORYB, specific factories that create product objects with specific implementations.

4: Example:

Background: Demonstrate this design pattern with an instance of the operations of tables (User and Department) in different databases (Oracle or SQL Server). First look at the code structure diagram:

3.1 first defines two abstract product classes: Iuser.java and Idepartment.java.

Source code for Iuser.java:

[HTML]View Plaincopy
    1. Package com.andyidea.patterns.abstractproduct;
    2. /**
    3. * Abstract Product role: User interface
    4. * @author Andy.chen
    5. *
    6. */
    7. Public interface Iuser {
    8. }

Idepartment.java Source:

[HTML]View Plaincopy
    1. Package com.andyidea.patterns.abstractproduct;
    2. /**
    3. * Abstract Product role: Department interface
    4. * @author Andy.chen
    5. *
    6. */
    7. Public interface Idepartment {
    8. }

3.2 Defining abstract Factory classes: Idbfactory.java

[HTML]View Plaincopy
    1. PACKAGE COM.ANDYIDEA.PATTERNS.ABSTRACTFACTORY;  
    2.   
    3. import com.andyidea.patterns.abstractproduct.idepartment;  
    4. import com.andyidea.patterns.abstractproduct.iuser;  
    5.   
    6. /**  
    7.  *  abstract factory Role: Factory interface   
    8.  *  @author  andy.chen  
    9.  *  
    10.  */  
    11. PUBLIC INTERFACE IDBFACTORY {  
    12.   
    13.      public iuser createuser ();   
    14.     public  idepartment createdepartment ();   
    15. }  

3.3 Create a specific product role class: Oracleofuser.java;oracleofdepartment.java;sqlserverofuser.java;sqlserverofdepartment.java. Inherit Iuser.java and Idepartment.java respectively.

Oracleofuser.java Source:

[HTML]View Plaincopy
    1. Package com.andyidea.patterns.concreteproduct;
    2. Import Com.andyidea.patterns.abstractproduct.IUser;
    3. /**
    4. * Specific product role: User in Oracle
    5. * @author Andy.chen
    6. *
    7. */
    8. public class Oracleofuser implements iuser{
    9. Public Oracleofuser () {
    10. SYSTEM.OUT.PRINTLN ("Oracle Factory: Operating the user table in Oracle.");
    11. }
    12. }

Oracleofdepartment.java Source:

[HTML]View Plaincopy
    1. PACKAGE COM.ANDYIDEA.PATTERNS.CONCRETEPRODUCT;  
    2.   
    3. import com.andyidea.patterns.abstractproduct.idepartment;  
    4.   
    5. /**  
    6.  *  specific product role: department  in Oracle  
    7.  *  @author  andy.chen  
    8.  *  
    9.  */  
    10. public class oracleofdepartment implements  idepartment{  
    11.       
    12.      public oracleofdepartment () {  
    13.          SYSTEM.OUT.PRINTLN ("Oracle Factory: Operating the Department table in Oracle.");   
    14.     }  
    15.   
    16. }   

Sqlserverofuser.java Source:

[HTML]View Plaincopy
    1. PACKAGE COM.ANDYIDEA.PATTERNS.CONCRETEPRODUCT;  
    2.   
    3. IMPORT COM.ANDYIDEA.PATTERNS.ABSTRACTPRODUCT.IUSER;  
    4.   
    5. /**  
    6.  *  specific product role: Sql server user  
    7.  *  @author  andy.chen  
    8.  *  
    9.  */  
    10. public class sqlserverofuser implements iuser{   
    11.       
    12.     public  Sqlserverofuser () {  
    13.          System.out.println ("Sql server Factory: Operate the user table in Sql server.");   
    14.     }  
    15.   
    16. }   

Sqlserverofdepartment.java Source:

[HTML]View Plaincopy
  1. Package com.andyidea.patterns.concreteproduct;
  2. Import com.andyidea.patterns.abstractproduct.IDepartment;
  3. /**
  4. * Specific Product roles: department in SQL Server
  5. * @author Andy.chen
  6. *
  7. */
  8. public class Sqlserverofdepartment implements idepartment{
  9. Public Sqlserverofdepartment () {
  10. SYSTEM.OUT.PRINTLN ("SQL Server factory: Manipulating the Department table in SQL Server.");
  11. }
  12. }

3.4 Create specific factory classes: Oraclefactory.java and Sqlserverfactory.java.

Oraclefactory.java Source:

[HTML]View Plaincopy
  1. Package com.andyidea.patterns.concretefactory;
  2. Import Com.andyidea.patterns.abstractfactory.IDBFactory;
  3. Import com.andyidea.patterns.abstractproduct.IDepartment;
  4. Import Com.andyidea.patterns.abstractproduct.IUser;
  5. Import com.andyidea.patterns.concreteproduct.OracleOfDepartment;
  6. Import Com.andyidea.patterns.concreteproduct.OracleOfUser;
  7. /**
  8. * Specific factory role: Oracle Factory
  9. * @author Andy.chen
  10. *
  11. */
  12. public class Oraclefactory implements idbfactory{
  13. @Override
  14. Public Iuser CreateUser () {
  15. return new Oracleofuser ();
  16. }
  17. @Override
  18. Public Idepartment createdepartment () {
  19. return new Oracleofdepartment ();
  20. }
  21. }

Sqlserverfactory.java Source:

[HTML]View Plaincopy
  1. Package com.andyidea.patterns.concretefactory;
  2. Import Com.andyidea.patterns.abstractfactory.IDBFactory;
  3. Import com.andyidea.patterns.abstractproduct.IDepartment;
  4. Import Com.andyidea.patterns.abstractproduct.IUser;
  5. Import com.andyidea.patterns.concreteproduct.SQLServerOfDepartment;
  6. Import Com.andyidea.patterns.concreteproduct.SQLServerOfUser;
  7. /**
  8. * Specific factory role: SQL Server Factory
  9. * @author Andy.chen
  10. *
  11. */
  12. public class Sqlserverfactory implements idbfactory{
  13. @Override
  14. Public Iuser CreateUser () {
  15. return new Sqlserverofuser ();
  16. }
  17. @Override
  18. Public Idepartment createdepartment () {
  19. return new Sqlserverofdepartment ();
  20. }
  21. }

3.5 Client Test class: Abstractfactoryclient.java

[HTML]View Plaincopy
  1. Package com.andyidea.patterns.client;
  2. Import com.andyidea.patterns.abstractproduct.IDepartment;
  3. Import Com.andyidea.patterns.abstractproduct.IUser;
  4. Import Com.andyidea.patterns.concretefactory.OracleFactory;
  5. Import Com.andyidea.patterns.concretefactory.SQLServerFactory;
  6. /**
  7. * Abstract Factory Test class
  8. * @author Andy.chen
  9. *
  10. */
  11. public class Abstractfactoryclient {
  12. public static void Main (string[] args) {
  13. System.out.println ("Welcome to Andy.chen blog!" + "\ n"
  14. + "Abstract Factory Patterns." + "\ n"
  15. +"-------------------------------");
  16. Iuser Oracleuser,sqluser;
  17. Idepartment oracledept,sqldept;
  18. Oraclefactory of = new Oraclefactory ();
  19. Sqlserverfactory SF = new Sqlserverfactory ();
  20. Oracleuser = of.createuser ();
  21. oracledept = of.createdepartment ();
  22. SqlUser = sf.createuser ();
  23. sqldept = sf.createdepartment ();
  24. }
  25. }

"4" program Run results:

[HTML]View Plaincopy
    1. Welcome to Andy.chen blog!
    2. Abstract Factory Patterns.
    3. -------------------------------
    4. Oracle Factory: Operate the user table in Oracle.
    5. Oracle Factory: Operate the Department table in Oracle.
    6. SQL Server Factory: Manipulate the user table in SQL Server.
    7. SQL Server Factory: Manipulate the Department table in SQL Server.

5. Application Summary:

Abstract Factory mode Benefits:

First, the easy-to-exchange product line, due to specific factory classes such as idbfactory factory = new Oraclefactory (), only needs to occur once during initialization in an application, making it very easy to change the specific factory of an application, It needs to change the specific plant to use different product configurations.
Second, it separates the concrete creation instance from the client, the client manipulates the instance through their abstract interface, and the specific class name of the product is separated from the implementation of the specific factory, and does not appear in the client code.

6. Extensions:

+ Reflection + configuration files further meet the Five principles of OO

Abstract engineering pattern of design pattern

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.