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.

Pluggable SelectorYou need simple Pluggable Behavior.How do you implement simple instance specific behavior?The simplest way to implement Pluggable Behavior is to store a selector to be performed.Let’s say we have implemented a ListPane. We create a method that takes one of the elements ofthe collection to be displayed and returns a String:ListPane>>printElement: anObject^anObject printStringAfter awhile, we notice that there are many subclasses of ListPane that only override this onemethod:DollarListPane>>printElement: anObject^anObject asDollarFormatStringDescriptionListPane>>printElement: anObject^anObject descrptionIt hardly seems worth the cost of all these subclasses if all they are going to do is override onemethod. A simpler solution is to make ListPane itself a little more flexible, so it different instancessend different messages to their elements. We add a variable called “printMessage” and modify#printElement:ListPane>>printElement: anObject^anObject perform: printMessageTo preserve the previous behavior, we would have to initialize the printMessage:ListPane>>initializeprintMessage := #printStringPluggable Selector meets the Pluggable Behavior criteria as follows:Readability- Pluggable Selector is harder to follow than simple class-based behavior. By looking atan object with an inspector you can tell how it will behave. You don’t necessarily have to singlestep through the code.Flexibility- The methods for the Pluggable Selectors must be implemented in the receiving object.The set of possible methods to be invoked should change at the same rate as the rest of the object.Extent- Pluggable selectors should be used no more than twice per object. Any more than that andyou risk obscuring the intent of the program. Use State Object if you need more dimensions ofvariability.Add a variable that contains a selector to be performed. Append “Message” to the rolesuggestingname of the variable. Create a Composed Method that simply performs theselector.<strong>Coding</strong> <strong>Patterns</strong> page 55 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!