"Big talk design mode" Ruby code: Simple Factory mode

Source: Internet
Author: User

I've seen the Ruby design pattern before, but I've forgotten all about it. Now buy a big talk design mode, it does not seem so boring, by the way the code with Ruby implementation.

#-*-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 endend#Factory classclassoperationfactorydefSelf.create_operate (operate) case operate when'+'operationadd.new () when'-'operationsub.new () when'*'operationmul.new () when'/'operationdiv.new () End Endendoper= Operationfactory.create_operate ('/') Oper.number_a= 1Oper.number_b= 2P Oper.result

"Big talk design mode" Ruby code: Simple Factory 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.