Grinding design mode-2 simple factory Mode

Source: Internet
Author: User
ArticleDirectory
    • 1. Simple factory to solve
    • 2.2 simple factory structure and description
    • 2.3 simple factory sample code
    • 2.4 example of simple factory Rewriting
2 solution 1 simple factory solution

A reasonable solution for solving the above problems is a simple factory. What is a simple factory?
1: simple factory Definition


 

2: application of simple factory Solutions
When analyzing the above problems, although the module cannot be informed of the specific implementation in the module, the module can be aware of the implementation class, and the Creation Interface requires the specific implementation class.
Create a class in the module, create an interface in the class, and return the created interface to the client, in this way, the external application only needs to obtain the corresponding interface object based on this class, and then the interface definition method can be operated. Let's call such an object a simple factory.
In this way, the client can obtain the required interface object through this factory, and then call the interface method to implement the required functions, and the client no longer needs to care about the specific implementation.

2.2 simple factory structure and description

The structure of a simple factory is shown in Figure 5:

 

API:
Define the functional interfaces required by the customer
Impl:
There may be multiple implementation classes for specific implementation APIs
Factory:
Factory, select an appropriate implementation class to create an API object
Client:
The client obtains the API object through the factory, and then programming for the API

2.3 simple factory example Code

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

 

   code highlighting produced by actipro codehighlighter (freeware) 
http://www.CodeHighlighter.com/
--> /**
* interface definition, this interface can be created through a simple factory
*/
Public interface API {
/**
* indicating the specific function and method definition
* @ Param S signal, required parameter
*/
Public void operation (string S);
}< br>

 

 

(2) define the interface to implement it. The sample code of impla is as follows:

 

  /**  
* Interface implementation object
*/
Public Class Impla Implements API {
Public Void Operation (string s ){
// Code for implementing the function.
System. Out. println ( " Impla S = " + S );
}
}

 

 

The implementation of implb is basically the same as that of impla. The sample code is as follows:

 

 /**  
* The specific implementation object B of the interface
*/
Public Class Implb Implements API {
Public Void Operation (string s ){
// Code for implementing the function.
System. Out. println ( " Implb S = " + S );
}
}

 

 

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

 
/*** Factory class, used to create an API object */public class factory {/*** specific method for creating an API object * @ Param condition, select conditions passed in from the outside * @ return the created API object */public static API createapi (INT condition) {// select the specific implementation object to be created based on certain conditions. // these conditions can be passed in from the outside or obtained from other channels. // If there is only one implementation, the conditions can be omitted because there is no need for selection. // Use the conditional API = NULL; If (condition = 1) {API = new impla ();} else if (condition = 2) {API = new implb ();} return API ;}}

 

 

(4) let's take a look at the demo of the client. The sample code is as follows:

 

  /**  
* The client uses the API
*/
Public Class Client {
Public Static Void Main (string [] ARGs ){
// Get interface object through simple factory
API = Factory. createapi ( 1 );
Api. Operation ( " Using simple factory " );
}
}

 

 

 

2.4 example of simple factory Rewriting

To use a simple factory to override the previous example, we mainly need to create a simple factory object, so that a simple factory is responsible for creating interface objects. Then let the client get the interface object through the factory, instead of creating the interface object by the client itself.
The system structure is shown in Figure 6.

 

Figure 6 using a simple factory to override the structure of the example

(1) The interface API and implementation class impl are the same as the previous example, so we won't go into details.

(2) create a simple factory object. The sample code is as follows:

 

  /**  
* Factory class used to create API objects
*/
Public Class Factory {
/**
* Specific API object creation methods
* @ Return Create a good API object
*/
Public Static API createapi (){
// Since there is only one implementation, no conditional judgment is needed.
Return New Impl ();
}
}

 

 

 

(3) Use a simple factory
How does the client use the functions provided by a simple factory? At this time, the client does not need to create the interface object by itself. The factory should be used to obtain the object. After transformation, the client code is as follows:

 

 
/*** Client: test the use of the API interface */public class client {public static void main (string [] ARGs) {// important change, no new impl, replaced by factory. createapi () API = factory. createapi (); API. test1 ("Haha, don't be nervous, it's just a test! ");}}

 

 

 

As shown in the preceding example, the client creates an object to implement the interface through a simple factory and then faces interface programming. From the perspective of the client, it does not know what the specific implementation is, I don't know how to implement it. It only knows that an interface object is obtained through the factory, and then the desired function can be obtained through this interface.
In fact, a simple factory can help us really start interface-oriented programming. As in the past, we actually only use the functions of interface polymorphism, the most important "encapsulation isolation" is not reflected.

 

 

 

With the help of everyone, the book "grinding Design Patterns" has been published. For more information, see the previous blog. Thank you for your support!

 

Dangdang sales link:Http://product.dangdang.com/product.aspx? Product_id = 20994349 & ref = search-0-mix

 

 

I hope my friends can score this book review and write some comments. Thank you for your support!

 

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.