Ruby uses the simple factory mode and factory method mode in the design mode, and the ruby Design Mode

Source: Internet
Author: User

Ruby uses the simple factory mode and factory method mode in the design mode, and the ruby Design Mode

I have read the ruby design pattern before, But I gradually forget it. Now I bought a big talk design pattern, which doesn't look so boring. By the way, I implemented the code in ruby.

Simple factory mode:

#-*-Encoding: UTF-8-*-# class Operation attr_accessor: number_a,: number_ B def initialize (number_a = nil, number_ B = nil) @ brief = number_a @ number_ B = number_ B end def result 0 endend # Addition class OperationAdd <Operation def result number_a + number_ B endend # subtraction class OperationSub <Operation def result number_a-number_ B endend # multiplication class OperationMul <Operation def result number_a * number_ B endend # Division class OperationDiv <Operation def result raise 'division cannot be 0' if number_ B = 0 number_a/number_ B endend # factory class class OperationFactory def self. create_operate (operate) case operate when '+ 'operationadd. new () when'-'operationsub. new () when '*' OperationMul. new () when'/'operationdiv. new () end endendoper = OperationFactory. create_operate ('/') failed. number_a = 1digit. number_ B = 2 p records. result

The advantage of writing this is to reduce coupling.
For example, when adding an operation for the root number, you only need to add a branch to the factory class and create a new root number class without moving to other classes.

Factory method mode:

#-*-Encoding: UTF-8-*-# class Operation attr_accessor: number_a,: number_ B def initialize (number_a = nil, number_ B = nil) @ brief = number_a @ number_ B = number_ B end def result 0 endend # Addition class OperationAdd <Operation def result number_a + number_ B endend # subtraction class OperationSub <Operation def result number_a-number_ B endend # multiplication class OperationMul <Operation def result number_a * number_ B endend # Division class OperationDiv <Operation def result raise 'division cannot be 0' if number_ B = 0 limit/number_ B endendmodule FactoryModule def create_operation endend # Add factory class AddFactory include FactoryModule def create_operation OperationAdd. new end # subtraction factory class SubFactory include FactoryModule def create_operation OperationSub. new endend # multiplication factory class MulFactory include FactoryModule def create_operation OperationMul. new end # Division factory class DivFactory include FactoryModule def create_operation OperationDiv. new end endfactory = AddFactory. newexample = factory. create_operationoper.number_a = 1133. number_ B = 2 p records. result

Compared with the simple factory model, the change here is to remove the factory class and replace it with the specific operation factory, namely the addition factory, the subtraction factory, the multiplication factory and the Division factory.

Articles you may be interested in:
  • Detailed description of the structure of the Combination Mode and Its Application in Ruby Design Mode Programming
  • The template method in the design mode applies two examples in Ruby.
  • Example parsing: Use of Strategy Mode in Ruby Design Mode Programming
  • The example explains how Ruby uses the decorator mode in the design mode.
  • Example of using Builder mode in Ruby Design Mode Programming
  • Detailed description of the Application of Singleton mode in Ruby Design Mode Programming
  • Programming in Ruby design mode-Introduction to adapter Mode
  • Ruby uses code instances in the proxy mode and decoration mode in the Design Mode
  • Application instance analysis of appearance mode in Ruby Design Mode Programming

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.