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...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Complete Creation MethodA Composed Method has had to create an object.How do you represent instance creation?The most flexible way to represent instance creation is by a simple “new” method, followed by aseries of messages from the client to the new instance. That way, if there are differentcombinations of parameters that make sense, the client can take advantage of just those parametersit needs.Creating a Point in this style looks like this:Point new x: 0; y: 0Further flexibility is provided in this approach to half way construct an object in one place, thenpass it off to another to finish construction. This can simplify communications if you don’t have tomodify the design to put all the creation parameters in one place.On the other hand, what is the first thing you want to know about a class, once you’ve decided itmay do what you want it to do? The first question is “what does it take to create an instance?” Asa class provider, you’d like the answer to this question to be as simple as possible. With the styledescribed above, you have to track down references to the class and read the code before you get aninkling of how to create a useable instance. If the code is complex, it may take a while before youfigure out what is required and what is optional in creating an instance.The alternative is to make sure that there is a method to represent each valid way to create aninstance. Does this result in a proliferation of instance creation methods? Almost never. Mostclasses only have a single way to create an instance. Almost all of the exceptions only have ahandful of variations. For the rare case where there really are hundreds or thousands of possiblecorrect combinations of parameters, use Complete Creation Methods for the common cases andprovide Accessor Methods for the remainder.With this style of instance creation, the question “how can I create a valid instance?” can be simplyanswered by looking at the “instance creation” protocol of the class methods. The IntentionRevealing Selectors communicate what the instance will do for you, while the Type SuggestingParameter Names communicate the parameters required.Provide methods that create well-formed instances. Pass all required parameters to them. Point x: 0 y: 0 SortedCollection new/SortedCollection sortBlock: aBlockIf the method takes parameters you will need a Creation Parameter Method. Give you method anIntention Revealing Selector that describes the roles of the parameters, not their type. A CompleteCreation Method that is used extensively may deserve a Constructor Method.<strong>Coding</strong> <strong>Patterns</strong> page 26 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!