"Big talk design mode" Ruby Code: Factory method Mode

Source: Internet
Author: User

First, take a look at the simple factory model

Advantages of the Simple factory model: The factory class contains the necessary logical judgments, dynamically instantiating the relevant classes based on the client's choice, and, for the client, the reliance on the specific product.

Disadvantages of the Simple factory model: When adding functionality, it is necessary to increase the conditional branch of the case, that is, to modify the factory class, violating the "open-closed principle".

2, factory method mode:

#-*-encoding:utf-8-*-#Operation classclassOperation Attr_accessor:number_a,:number_bdefInitialize (number_a = nil, Number_b =nil) @number_a=number_a @number_b=Number_b Enddefresult 0 EndEnd#Addition ClassclassOperationadd <Operationdefresult Number_a+Number_b endend#Subtraction ClassclassOperationsub <Operationdefresult Number_a-Number_b endend#Multiplication ClassclassOperationmul <Operationdefresult Number_a*Number_b endend#Division classclassOperationdiv <OperationdefresultRaise 'the divisor cannot be 0' ifNumber_b = =0 number_a/number_b endendmodule factorymoduledefcreate_operation endend#Additive Factoryclassaddfactory include Factorymoduledefcreate_operation operationadd.new End End#Subtraction Factoryclasssubfactory include Factorymoduledefcreate_operation operationsub.new endend#Multiplication Plantclassmulfactory include Factorymoduledefcreate_operation operationmul.new End End#Division Factoryclassdivfactory include Factorymoduledefcreate_operation operationdiv.new End Endfactory=Addfactory.newoper=factory.create_operationoper.number_a= 1Oper.number_b= 2P Oper.result

Instead of the simple factory model, the change here is to remove the factory class and replace it with a specific computing plant, namely the addition plant, the subtraction plant, the multiplication plant, and the division plant.

Factory method Mode: Defines an interface for creating an object, letting subclasses decide which class to instantiate. A factory method is an instantiation of a class that is deferred to a subclass.

When the factory method pattern is implemented, the client decides which factory to instantiate to implement the Operation class, and the problem of choosing judgment is still there, that is, the factory method transfers the internal logic judgment of the simple factory to the client code. To add functionality, you would have modified the factory class and now modify the client.

The advantage of this is that it overcomes the shortcomings of the simple factory against the "open-closed principle" and preserves the advantages of the encapsulation object creation process.

The problem is that the logical branch of judgment still needs to be implemented, but it is transferred from the factory class to the client, using "reflection" can solve the problem of branch judgment.

"Big talk design mode" Ruby Code: Factory method Mode

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.