Grinding design mode-factory method mode-2

Source: Internet
Author: User
Document directory
  • 2.1 solution to factory methods
  • 2.2 schema structure and description
  • 2.3 sample code of factory method mode
  • 2.4 use the factory method mode for implementation example

 

2 solution 2.1 solution to the factory Mode

A reasonable solution for solving the above problems is the factory method model. So what is the factory method model?
(1) Factory method mode definition
Defines an interface used to create objects so that the subclass determines which class to instantiate. The Factory Method delays the instantiation of a class to its subclass.

 

(2) how to solve the problem by using the factory method model
After careful analysis of the above problems, in fact, in the business function objects that implement data export, there is no idea which file format to use, therefore, this object should not be coupled with the object of the specific exported file. It only needs the interface for the exported file object.
However, a new problem arises: The interface cannot be used directly. You need to use a specific interface to implement the instance of the object.
Isn't that self-contradictory? Interface-oriented is required to avoid coupling with specific implementations, but an instance of the specific implementation object of the interface needs to be created. How can this problem be solved?
The solution to the factory method mode is very interesting, that is, the solution does not solve the problem, and the method is taken for nothing: do not need an interface object, then define a method to create it; but in fact, it does not know how to create this interface object. It doesn't matter, so it can be defined as an abstract method. If it cannot be implemented by itself, let the subclass implement it, in this way, the object itself can be only interface-oriented, without having to worry about how to create interface objects.

2.2 schema structure and description

The structure of factory method mode 3 is shown below:

Figure 3 structure of factory method mode

Product:
Defines the interface of the object created by the factory method, that is, the interface of the object actually needed.
ConcreteProduct:
The implementation object of the specific Product interface.
Creator:
The builder declares the factory method. The factory method usually returns an Instance Object of the Product type, and is mostly an abstract method. You can also provide the default Implementation of the factory method in the Creator, so that the factory method returns a default Product type instance object.
ConcreteCreator:
The specific Creator object overwrites the factory method defined by the Creator and returns the specific Product instance.

2.3 sample code of factory method mode

(1) Let's take a look at the Product definition. The sample code is as follows:

/**

* Interface of the object created by the factory Method

*/

Public interface Product {

// You can define Product attributes and methods.

}

(2) Let's look at the implementation objects of specific products. The sample code is as follows:

/**

* Specific Product object

*/

Public class ConcreteProduct implements Product {

// Method for implementing Product requirements

}

(3) let's take a look at the definition of the generator. The sample code is as follows:

/**

* The Creator declares the factory method.

*/

Public abstract class Creator {

/**

* How to Create a factory for a Product

* @ Return Product object

*/

Protected abstract Product factoryMethod ();

/**

* Indicates a method to implement certain functions.

*/

Public void someOperation (){

// In the implementation of these methods, you need to call the factory method to obtain the Product object.

Product product = factoryMethod ();

}

}

(4) Let's look at the specific implementation object of the creator. The sample code is as follows:

/**

* Specific creator implementation object

*/

Public class ConcreteCreator extends Creator {

Protected Product factoryMethod (){

// Redefine the factory method and return a specific Product object

Return new ConcreteProduct ();

}

}

 

2.4 use the factory method mode for implementation example

To use the factory method mode to implement the example, first follow the structure of the factory method mode to find out which products are created and which are creators. The exported file object interface ExportFileApi is equivalent to the Product, and the business function object used to export data is equivalent to the Creator. After separating Product and Creator, You can implement them separately.
The sample program structure 4 is implemented using the factory mode:

Figure 4 Use the factory mode to implement the sample program structure
Let's take a look at the code implementation.
(1) The implementation of the exported file object interface ExportFileApi has not changed. I will not go into details here.
(2) Let's take a look at the implementation of the interface ExportFileApi. To make the example simple, we can only export text file formats and database backup files. First, let's take a look at the implementation of the exported text file format. The sample code is as follows:

/**

* Objects exported as text files

*/

Public class ExportTxtFile implements ExportFileApi {

Public boolean export (String data ){

// A simple illustration. The file operation is required here.

System.Out. Println ("export data" + data + "to text file ");

Return true;

}

}

Let's take a look at the implementation of objects exported as database backup files. The sample code is as follows:

/**

* Objects exported as database backup files

*/

Public class ExportDB implements ExportFileApi {

Public boolean export (String data ){

// A simple illustration. You need to operate the database and files here.

System.Out. Println ("export data" + data + "Back up database file ");

Return true;

}

}

(3) for the implementation of Creator, first look at the implementation of ExportOperate. The sample code is as follows:

/**

* Business function objects for data export

*/

Public abstract class ExportOperate {

/**

* Export files

* @ Param data the data to be saved

* @ Return whether the file is exported successfully

*/

Public boolean export (String data ){

// Use the factory Method

ExportFileApi api = factoryMethod ();

Return api. export (data );

}

/**

* Factory method: Create the interface object of the exported file object

* @ Return interface object of the exported file object

*/

Protected abstract ExportFileApi factoryMethod ();

}

(4) added two Creator implementations. First, let's look at the creation and export of objects in text file format. The sample code is as follows:

/**

* Specific creators implement objects and create and export objects in Text File Format

*/

Public class ExportTxtFileOperate extends ExportOperate {

Protected ExportFileApi factoryMethod (){

// Create an object exported as a text file

Return new ExportTxtFile ();

}

}

Let's take a look at the creation and export of objects in the form of database backup files. The sample code is as follows:

/**

* The specific generator implementation object is used to create and export objects in the form of Database Backup Files

*/

Public class ExportDBOperate extends ExportOperate {

Protected ExportFileApi factoryMethod (){

// Create an object exported as a database backup file

Return new ExportDB ();

}

}

(5) The client directly creates the Creator object to be used and then calls the corresponding function method. The sample code is as follows:

Public class Client {

Public static void main (String [] args ){

// Create the Creator object to be used

ExportOperate operate = new ExportDBOperate ();

// Call the function of data output

Operate. export ("Test Data ");

}

}

The running result is as follows:

Export data test data to database backup files

You can also modify the new object of the client, switch to another implementation object, and try to see what will happen. It seems that the application factory method mode is very simple, right.

 

To be continued ......

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.