Three brothers' factory method model (4)

Source: Internet
Author: User
5 heavy load factory Method

Through further analysis, sunny developers found that the logger can be initialized in multiple ways. For example, they can provide default implementation for various logger, and provide database connection strings for Database Logger, provides the file path for the file logger. You can also encapsulate parameters in an object type object and pass configuration parameters to the factory class through the object. In this case, you can provide a set of reload factory methods to create product objects in different ways. Of course, for a specific factory, no matter which factory method is used, the product type created must be the same. 4:

Figure 4 Structure of the heavy load factory Method

After the overload method is introduced, the code of the abstract factory loggerfactory is modified as follows:

interface LoggerFactory {public Logger createLogger();public Logger createLogger(String args);public Logger createLogger(Object obj);}

Modify the code of the factory databaseloggerfactory as follows:

Class databaseloggerfactory implements loggerfactory {public logger createlogger () {// use the default method to connect to the database. The Code omitting logger = new databaselogger (); // initialize the Database Logger, code omitting return logger;} public logger createlogger (string ARGs) {// use The args parameter as the connection string to connect to the database. The Code omitting logger = new databaselogger (); // initialize the Database Logger with the Code omitted return logger;} public logger createlogger (Object OBJ) {// use the connection string encapsulated in the OBJ parameter to connect to the database, code omitted logger = new databaselogger (); // use the data encapsulated in the OBJ parameter to initialize the Database Logger. The Code omitted return logger; }}// other specific factory-class code is omitted

Define multiple overloaded factory methods in the abstract factory and implement these factory methods in a specific factory. These methods can contain different business logic to meet the needs of different product objects.

6 hiding factory methods

Sometimes, to further simplify the use of the client, you can also hide the factory method on the client. In this case, the product business method is called directly in the factory class, and the client does not need to call the factory method to create a product, you can directly use the business methods in the created object through the factory.

If the factory method is hidden from the client, the structure of the logger is changed to Figure 5:

Figure 5 structure of the logger after hiding the factory Method

In Figure 5, the code of the abstract factory class loggerfactory is modified as follows:

// Change to abstract class loggerfactory {// call the business method writelog () Public void writelog () {logger = this in the factory class directly. createlogger (); logger. writelog ();} public abstract logger createlogger ();}

The client code is modified as follows:

Class client {public static void main (string ARGs []) {loggerfactory factory; factory = (loggerfactory) xmlutil. getbean (); factory. writelog (); // use the factory object directly to call the Business Method of the product object }}

By moving the call of the Business Method to the factory class, you can directly use the factory object to call the Business Method of the product object. The client does not need to directly use the factory method, in some cases, we can also use this design scheme.

 

7 factory method mode Summary

The factory method mode is an extension of the simple factory mode. It inherits the advantages of the simple factory mode and also makes up for the shortcomings of the simple factory mode. The factory method mode is one of the most frequently used design modes, and is the core mode of many open-source frameworks and API class libraries.

 

1. Main advantages

The factory method mode has the following advantages:

(1) In the factory method mode, the factory method is used to create the product required by the customer, and the details of the specific product category to be instantiated are also hidden to the customer, users only need to care about the factory corresponding to the product, no need to care about the creation details, or even the Class Name of the specific product category.

(2) the design of polymorphism Based on the factory role and Product role is the key to the factory method model. It allows the factory to determine which product object to create, and the details of how to create this object are completely encapsulated in a specific factory. The factory method mode is also called the multi-state factory mode because all the factory classes have the same abstract parent class.

(3) Another advantage of using the factory method mode is that when adding a new product to the system, you do not need to modify the interfaces provided by the abstract factory and abstract product, and you do not need to modify the client, there is no need to modify other specific factories and products, but you only need to add a specific factory and product. In this way, the scalability of the system becomes very good, it fully complies with the "open and closed principles ".

 

2. Main disadvantages

The main disadvantages of the factory method mode are as follows:

(1) When adding a new product, you need to write a new specific product class and provide the corresponding factory class. The number of classes in the system will increase in pairs, to some extent, the complexity of the system is increased, and more classes need to be compiled and run, which will bring additional overhead to the system.

(2) Considering the scalability of the system, the abstract layer needs to be introduced and defined using the abstract layer in client code, which increases the abstraction and difficulty of the system, dom, reflection, and other technologies may be used to increase the difficulty of system implementation.

 

3. Applicable scenarios

The factory method mode can be considered in the following cases:

(1) the client does not know the class of the desired object. In the factory method mode, the client does not need to know the class name of the specific product class, but only needs to know the corresponding factory. The specific product object is created by the specific factory class, class names of specific factory classes can be stored in the configuration file or database.

(2) The abstract factory class uses its subclass to specify the object to be created. In the factory method mode, for abstract factory classes, you only need to provide an interface for creating products, and its subclass determines the specific object to be created, with the object-oriented polymorphism and the ripple replacement principle, when the program is running, the subclass object will overwrite the parent class object, making the system easier to expand.

Exercise

Design a program to read different types of image formats using the factory method. design an image reader for each image format, for example, the GIF image reader is used to read images in GIF format and the jpg image reader is used to read images in JPG format. System flexibility and scalability should be fully considered.

 

【Author】 Liu WeiHttp://blog.csdn.net/lovelion]

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.