13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Constant MethodYou are writing a Composed Method. You may need a Default Value Method.How do you represent a constant?<strong>Smalltalk</strong> provides several options for representing shared constants or variables. Pools provide aset of variables that can be used in any number of different classes. Class variables are variablesthat are shared throughout a hierarchy.I know that some folks use lots of pools in their applications. I never use them. Here’s what I don’tlike about pools- they make it too easy to stop making useful responsibilities and instead justspread data around.The IBM <strong>Smalltalk</strong> window system is a good example. There is a pool called CwConstants thatcontains hundreds of constants. Rather than being able to send a list pane a message like#singleSelect to make it select one item at a time, you send it “selectionPolicy:XmSINGLESELECT”.Of course, you could say that this is just one little thing you have to remember, and besides, Cprogrammers have to deal with stuff like this all the time. However, multiply “one little thing toremember” by several hundred and you don’t have a system that feels much like <strong>Smalltalk</strong> anymore, at least not when you’re working with the window system.I would rather limit the visibility of constants to a single class. Then that class can providemeaningful behavior to other objects. If you can refactor your code so that only a single class caresabout a constant, then you can represent that constant as a method.In the list pane example, this means leaving the original implementation of #selectionPolicy: alone,but not expecting outside objects to invoke it. Instead, for each of the possible argument values wecreate a method:ListPane>>singleSelectself selectionPolicy: 15If other methods also needed to know that 15 was the magic constant for single selection, we createa method for it, a method that just returns 15.ListPane>>singleSelectPolicy^15ListPane>>singleSelectself selectionPolicy: self singleSelectPolicyOne of the downsides of representing constants as methods is it is easier to automatically generatethe initialization for a pool than it is to automatically create a method. If you have to synchronize aset of constants with other programs, you may find it easier to use pools. However, you should stillstrive to provide rich protocol so that few classes need to know about the pool.Another question about Constant Methods is what happens when you have hundreds or thousandsof constants? A pool can easily grow to contain as many entries as necessary. A class with athousand Constant Methods would look cluttered.Create a method that returns the constant.<strong>Coding</strong> <strong>Patterns</strong> page 67 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!