Design Mode 3 (Factory method mode)

Source: Internet
Author: User

Factory method mode: defines an interface for creating objects, so that the subclass decides to instantiate the class. The factory method delays the instantiation of a class to its subclass.

The biggest advantage of the simple factory mode (http://www.cnblogs.com/aehyok/archive/2013/05/10/3072008.html) is that the factory class contains the necessary logic judgment, according to the selection conditions of the client to dynamically instantiate the relevant class, for the client, remove dependencies with specific products.

When the factory method mode is implemented, the client needs to decide to instantiate the factory to implement the computing class, and select whether the problem still exists. That is to say, the factory method moves the internal logic judgment of the simple factory to the client code. If you want to add a function, you would have changed the factory class (simple factory mode), but now you want to modify the client.

 

If you want to add a function, such as M's Npower, you only need to add the operation class and the corresponding factory class of this function.

1. Write the operation class in Step 1:

           NumberA { ;   NumberB { ;    result = 

2. Then we need to implement the addition, subtraction, multiplication, division operation class.

                  result = = .NumberA +         result = = .NumberA -               result = = .NumberA *               result =  (.NumberB ==   Exception(= .NumberA / 

Step 3 Write the Abstract Factory

         

The operation class is returned by the factory interface.

Step 4: Implement addition, subtraction, multiplication, division, and abstract factory

                                                    

Example of calling an addition in step 5

      Main(            IFactory OperationFactory = ===

The result is

The factory method pattern overcomes the disadvantages of a simple factory that violates the open-closed principle and maintains the advantages of encapsulating the object creation process. The factory method mode is further abstract and promoted by the simple factory mode. Due to the use of polymorphism, the factory method pattern maintains the advantages of the simple factory pattern and overcomes its shortcomings. However, the disadvantage is that each time you add a product, you need to add a product factory class to increase the additional development workload. Of course there are better solutions ......

If you do not know much about the simple factory model of the previous section, take a look at the http://www.cnblogs.com/aehyok/archive/2013/05/10/3072008.html.

Related Article

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.