From: Seebs Date: 2009-11-17T06:40:10+09:00 Subject: Re: Newbie question: Defining a numeric type On 2009-11-16, Marnen Laibow-Koser wrote: >> (Note the "adjust_to" -- you can't, for obvious reasons, assign a new >> value with =. Things which have stats define the attr= for those stats >> to use adjust_to.) > Philosophical point: many people (myself included, I think) believe that > unlike entity objects (say, Player in your game), value objects should > be immutable. This means > > class Stat > def adjust_to(n) > # bad: > @base = n > # good: > Stat.new(n, @modifier) > end > end > > See http://c2.com/cgi/wiki?ValueObjectsShouldBeImmutable . Among other > things, this means you never have to worry about modifying an object > referred to in multiple places. In this case, I think the correct model is that this isn't really a value object, it's just an object which has a value. Imagine that I wanted to talk about "my paycheck". Well, there's a base salary, and there's various taxes and deductions and withholding... But except when I'm doing the tax accounting, all I *really* care about is the take-home pay, so if I refer to "salary" I probably mean the output value of all those calculations. Basically, if someone else has a reference to a specific stat, and it changes, I think they DO want to see the now-changed value. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!