Take the 2-minute tour ×

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

class Plan
   attr_accessible :cost

class CreditCard
  def charge_card(amount)

class Event
  attr_accessible :message
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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

share|improve this answer
Thanks Arun! I thought about a strategy, but I'm less concerned with if we bill than the act of how to pass the messages around. When Person changes, both CreditCard and Event need to be notified, but neither notifications really apply to Person's domain. –  Greg Olsen Oct 30 '12 at 17:12
@GregJ Good point. So, there are two problems in hand. You could always use Strategy for your calculation purposes and like you mentioned in your question, use Observer for publish/subscribe problem. –  Arun Manivannan Oct 30 '12 at 17:19
Thanks Arun, I figured Observer would probably be the way to go. –  Greg Olsen Oct 30 '12 at 17:29
@GregJ or you could use both. No harm in using patterns hand in hand. After all, you do have two problems. –  Arun Manivannan Oct 30 '12 at 17:35
Definitely! Sorry I should have said, "I figure the Observer would probably be the way to go... for the message passing issue" :) –  Greg Olsen Oct 30 '12 at 17:49

Your Answer


