Sunday, August 2, 2015

SOLID Principle

S for Single responsibility principle

Note 
       
class Order < ActiveRecord::Base

	def self.find_purchased

	# ...

	end

	def self.find_waiting_for_review

	# ...

	end



	def self.find_waiting_for_sign_off

	# ...

	end



	def self.find_waiting_for_sign_off

	# ...

	end

	def self.advanced_search(fields, options = {})

		def self.simple_search(terms)

    end


	def to_xml

	# ...

	end



	def to_json

	# ...

	end



	def to_csv

	# ...

	end



	def to_pdf

	# ...

	end

# ...

end
      
 

O for Open closed principle which means , a class should be close for modification and open for extension

Example:

       
class Purchase

	def initialize(payment_process)

		@payment_process = payment_process

	end

	def charge_user!

		@payment_process.charge(user: user, amount: amount)

	end

end



Purchase.new(MachPayAdapter).charge_user!





class Stripe

	def charge(user,amount)

	end

end



class Paypal

	def charge(user,amount)

	end

	

end



class MachPay

 def charge_amount(amount,user)

 end

	end



class MachPayAdapter



  def charge(user,amount)

  	MachPay.charge_amount(amount,user)

  end

end



    
 

No comments:

Post a Comment