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.

Factored SuperclassHow do you design inheritance hierarchies?Of the three pillars of objects- encapsulation, polymorphism, and inheritance, inheritance provokesthe most controversy. Opinions of what it is and how it should be used are legion. For some reason,inheritance debates seem to generate intense emotions. This makes any concrete statement abouthow to use inheritance a dangerous proposition. I’m bound to offend someone, or at least challengetheir current thinking.The purpose of patterns is to demystify, to reduce to repeatable practice. Ignoring or waffling oninheritance would be a breach of my responsibility as an author. Buckle your seat belts, here comesheresy!The inheritance debate is clouded by the many legitimate uses to which it is put in differentcontexts. As originally conceived in artificial intelligence, subclasses are similar to superclasses.Thus, mammal is subclass of animal and employee is a subclass of person. This relationship oftendescribed as “is-a”. A mammal “is-an” animal and an employee “is-a” person. Knowledgerepresentation languages use inheritance to represent conceptual classification.As implemented in object programming languages, from Simula on down, inheritance doessomething quite different. Inheritance shares implementation. An instance of a subclass with nomethods responds to all messages exactly as an instance of the superclass would. As the subclassoverrides or extends superclass methods, its implementation begins to diverge. This use ofinheritance can be summarized as “is-implemented-like”. Thus, a code editor “is-implemented-like”a text editor and a socket stream “is-implemented-like” a serial stream.C++ and Eiffel further cloud the picture by using inheritance to specify types. Sets of messages arerepresented as classes, often with no implementation. If an object is to be used in a context wherethose messages will be sent, it must inherit from one of these “mix-in” classes. In these languages,inheritance means both “is-implemented-like” and “responds-to-the-same-messages-as”.Just to be sure the picture isn’t at all clear, communicating protocols (sets of messages that oneobject expects of another) is very important. Often, protocols are really the heart of how a design isto be reused. If you can create an Impostor for an object in a framework, you can suddenly unlock awhole world of functionality.Well, then, how should <strong>Smalltalk</strong> inheritance be used?Make all of your classes subclasses of Object at first. Create a superclass to hold commoncode of two or more existing classes.<strong>Coding</strong> <strong>Patterns</strong> page 125 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!