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.

Extending SuperYou are using Super.How do you add to a superclass’ implementation of a method?Any use of super reduces the flexibility of the resulting code. You now have a method that assumesnot just that there is an implementation of a particular method somewhere, but that theimplementation has to exist somewhere in the superclass chain above the class that contains themethod. This assumption is seldom a big problem, but you should be aware of the tradeoff you aremaking.If you are avoiding duplication of code by using super, the tradeoff is quite reasonable. Forinstance, if a superclass has a method that initializes some instance variables, and your class wantsto initialize the variables it has introduced, super is the right solution. Rather than have code like:Class: SuperSuperclass: ObjectVariables: aSuper class>>new^self basicNew initializeSuper>>initializea := self defaultAClass: SubSuperclass: SuperVariables: bSub class>>new^self basicNewinitialize;initializeBSub>>initializeBb := self defaultBwhere the subclass has to invoke both initializations explicitly, using super you can implement:Sub>>initializesuper initialize.b := self defaultBand not have Sub override “new” at all. The result is a more direct expression of the intent of thecode- make sure Supers are initialized when they are created, and extend the meaning ofinitialization in Sub.When you want to extend the meaning of a superclass method, override the method andinvoke “super” in the new method.<strong>Coding</strong> <strong>Patterns</strong> page 48 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!