Study Notes: Design Model-factory Model

Source: Internet
Author: User

I am currently working on an upload/download template. the upload/download function must be implemented in a class, but the two parameters are obviously different. Therefore, we need to create different instances, if you use the constructor to create upload and download classes, the "expression" is not very clear. So I thought of using the factory mode to create an instance outside the class. This is the reason why I learned the engineering model, but later I found that it was too clever. After all, the essence of the factory model lies in the reusability of code.

There are many excellent articles on the Internet, they speak the factory model clearly, very clear (such as this article: http://www.cnblogs.com/poissonnotes/archive/2010/12/01/1893871.html#2605935 ). However, few people explain what the factory model is. The purpose of this article lies in this.

Generally, we divide the factory model into three types: simple factory model, factory method model, and abstract factory model.

Simple factory model: Abstracts products. The implementation method is divided into three steps:

Step 1: extract the common factor-method or attribute to the "Product interface" (implemented using an interface or abstract class ). Here is an aside. The C # interface does not contain fields, but you can use attributes, that is, the accessors and accessors.

Step 2: create a product category and implement the "Product interface".

Step 3: Create a factory. The factory provides a create method to produce products. This method receives a parameter and then determines the product to be produced based on the parameter, and then returns the "Product interface" using the upstream transformation.

Step 4: Call.

For example, we now have a music player, and we may have a video player in the future.

1. Extract the method ---- play, pause, end, fast forward, and fast back ---- and put it into the media interface.

2. Create a music class, inherit from and implement the media interface.

3. Create a media factory and receive a string. When it is music, create a music class and return the media interface. Otherwise, an exception is thrown.

4. Use a media factory in a music player to instantiate a media interface and call the method provided by the interface.

OK. Now let's take a look at the work that will be done when you prepare a video player.

1. Copy media interface

2. Create a video class, inherit from and implement the media interface.

3. Copy media factory.

4. Modify the media factory and add a judgment. When the parameter is video, create a video class and return the media interface.

5. Copy the music playback code to set the parameter during Instantiation to video.

Now the factory model is very effective, code reuse, only a small number of changes.

A good design should achieve high cohesion and low coupling. It is unreasonable to modify the factory class every time in the simple factory mode. To solve this problem, the factory method model came into being.

Compared with the simple factory mode, the factory method mode "extracts" the factory and creates a "factory interface" (the abstract class implementation is recommended here. After all, the interface does not support the static method ).

Let's take a look at this example.

The first two steps are the same as the simple factory mode. We start with step 3.

3. Create a "Media factory interface" that specifies that the returned value is a media interface.

4. Create a Music Factory, inherit from and implement the "Media factory interface ".

5. Call.

Now let's take a look at what we will do for video players in the future.

1. Copy media interface.

2. Create a video class and implement media interfaces.

3. Copy "Media factory interface ".

4. Create a video factory, inherit from and implement the "Media factory interface ".

5. Copy the music player code, modify it, and replace the class used by the instantiated factory interface with the video factory.

It can be seen that the video player reuse the code of the music player, and the code is well separated, which does not affect each other.

So what is the abstract factory model?

It has only one more hierarchical relationship, and a factory can produce multiple products with Mutual Association. In the end, it can still be implemented in a simple factory or factory method mode. But as a good design, we should still implement it in the factory method mode, that is, the factory that can produce multiple products will "extract" again and make an "interface ".

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.