Strategy template for billing models that use different data to calculate?

We have an account model that pays customers in several ways. For brevity, I’m going to focus on two: cost per impression and cost per phone request. My thought was to implement these (and the rest) as strategies, and then dynamically mix them with the invoice class.

This seems appropriate because there are different sources of information used to determine the number of impressions / calls. This can be encapsulated in a strategy while keeping the basic formula in the class Invoice.

Calculating the cost of the show is simple: num impressions X cost per impression.

Calculation of telephone inquiries a little more complicated: num calls X cost per call.

class Invoice
  def self.strategy
    self.class_eval <<-EOS
    include #{billing_type}
    EOS
  end

  def invoice_amount
    # this will used the module mixed in above
    self.rate * calculate_impressions
  end
end

Then the modules can be:

module PerImpressionCalculation
  def calculate_impressions
     # get the number of impessions from source a...
  end
end

module PerInquiryCalcuation
  def calculate_impressions
     # get the number of impessions from source b...
  end
end

, , , , . , , .

, ? -, 10- , 30-, . , , 15 , . ?

+3
2

. PerInquiryCalculation Invoice, .

, , . PerInquiryStrategy , PerInquiryCalculation .

+3

ancestors. , myInvoice, myInvoice.class.ancestors. , .

, , / : , . , , ...

0

Source: https://habr.com/ru/post/1724567/


All Articles