سؤال

I'm trying to figure out the best way to encapsulate the following scenario with Object Oriented Design principles. What type of design pattern would best suit?

I've added very basic class definitions below, basically a person has a plan and a credit card. When a person changes their plan, we want to bill them the difference (if the plan's cost was more) and create an Event

I could do this in callbacks or an observer, check if the Plan for that Person changed, but in a larger system with lots of cross model behaviour, this becomes unmanageable and difficult to test with lots of unrelated callbacks affecting state.

class Person
   has_one :plan
   has_one :credit_card
   has_many :events
end

class Plan
   attr_accessible :cost
end

class CreditCard
  def charge_card(amount)
    ...
  end
end

class Event
  attr_accessible :message
end
هل كانت مفيدة؟

المحلول

If I understand it right, your credit card is your condition based on which your billing differs. Strategy pattern is probably the right one to fit in, IMHO.

Replacing Conditional with Strategy

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top