Difference between internal and external messages

External messages are any messages sent to objects other than self. External messages are a dependency because the object needs to know the name of a message that other object will respond to.

class Comment
  attr_reader :body, :post
  def initalize(body, post)
    @body = body
    @post = post
  end

  def capitalize_body
    body.upcase
  end

  def post_title
    post.title
  end
end

post = Post.new(title: "hi may name is someone")
comment = Comment.new(body: "this is a cool post", post: post)
# example of a message sent to self
comment.capitalize_body

The Comment#capitalize_body method sends a message to self. The Comment#post_title message sends the title message to the post object and depends on the post object to be able to respond to the title message. External message dependencies often cannot be avoided, but are still real dependencies that should be monitored and occasionally isolated into separate methods.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s