Factory method Mode (7)

Source: Internet
Author: User

Today, let's talk about the factory method pattern.

People may listen to this pattern a bit familiar, yes, the first blog post above, we talked about the simple factory model. Well, they are really very similar, and today we take examples from the simple factory model.

By learning the simple factory model, it is natural for the factory approach model.

As you know, the simple factory has a very obvious disadvantage, in the case, I add an algorithm, you need to add a class (this is no problem, expand it.) But, I also have to modify the factory, in the factory method of the class face more case judgment, which actually violates the principle of open closure. (Try not to modify a class that has already been written).

Well, we can solve this problem by using the factory method pattern.

We abstract the Operationfactory class from the case to an interface, and then let each of the cases be split into classes that inherit the interface.

The code is as follows:

1   //abstract interface of the factory2     Interfaceifactory3     {4 operation CreateOperation ();5     }6     //Additive Factory7     classaddfactory:ifactory8     {9          PublicOperation CreateOperation ()Ten         { One             return NewOperationadd (); A         } -     } -     //Subtraction Factory the     classsubfactory:ifactory -     { -          PublicOperation CreateOperation () -         { +             return Newoperationsub (); -         } +     } A     //Multiplication Plant at     classmulfactory:ifactory -     { -          PublicOperation CreateOperation () -         { -             return NewOperationmul (); -         } in     } -     //Division Factory to     classdivfactory:ifactory +     { -          PublicOperation CreateOperation () the         { *             return NewOperationdiv (); $         }Panax Notoginseng}

In this case, the client is changed to the following:

1  Public Static voidMain ()2         {3Operation oper =Newoperation ();4Addfactory ADDFAC =Newaddfactory ();5Oper =ADDFAC. CreateOperation ();6Oper. Numbera =2;7Oper. Numberb =3;8             varresult =Oper. GetResult ();9 Console.WriteLine (result);Ten Console.readkey (); One}

OK, so that the original simple factory model is adapted to the factory method mode.

Let's look at the factory method model for the simple factory model what are the advantages?

If we add an algorithm, we only need to increase the class of the algorithm, and then add a factory of this algorithm, you need to modify only the client.

So is there a better way? (Even the client does not have to be modified) then the last time we mentioned it, we use reflection to solve it. (later on in the blog post).

Well, this blog post is similar to the simple factory model, and you can look at it in comparison and deepen your understanding.

Ok. I'll talk about it here today. Next time, the prototype mode will be explained

This series will continue to update, like the small partners can click on the attention and recommendations, thank you for your support.

Factory method Mode (7)

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.