mardi 16 juin 2015

Design patterns on initializing an object?

What's the recommended way to handle an object that may not be fully initialized?

e.g. taking the following code (off the top of my head in ruby):

class News
  attr_accessor :number

  def initialize(site)
    @site = site
  end

  def setup(number)
    @number = number
  end

  def list
    puts news_items(@site, @number)
  end
end

Clearly if I do something like:

news = News.new("siteA")
news.list

I'm going to run into problems. I'd need to do news.setup(3) before news.list.

But, are there any design patterns around this that I should be aware of?

Should I be creating default values? Or using fixed numbers of arguments to ensure objects are correctly initialized?

Or am I simply worrying too much about the small stuff here.

Aucun commentaire:

Enregistrer un commentaire