Primer : Object-Oriented Principles (Part 1)
It is impossible to cover the entire spectrum of object-oriented (OO) principles
in this tutorial; the subject is much too broad, very abstract and contains a lot
of theory. What is interesting to note is that many university-level computer
science programs use Smalltalk to teach object-oriented principles because it
provides a concrete example of the theory of OO programming.
This primer aims to provide just enough background information on OO programming
to make you conversant with the terms. As you work with Smalltalk, your understanding
will grow and things will start to make more and more sense. It may get
frustrating at times but hang with it. Programming in OO is a mindset and it
requires, in some cases, a whole new way of thinking about solving the
problem at hand. A traditional programmer, when given an assigment, will ask
the question "How will I do that?" whereas a Smalltalk programmer will ask
the question "What will do that for me?"
What is an Object?The most widely accepted definition for an object is "An object is an instance of a class." So the next logical question is "What is a Class?". A class is a definition or template that describes the characteristics of an object. For example, an Integer would be considered a class whereas the number 1 would be a specific instance of the Integer class. Some people refer to a Class as an "object factory". Classes (factories) know how to produce objects. Other metaphors include rubber stamps and blueprints. So using a factory metaphor, think of them in this way - the factory that makes a car is not a car. Classes are designs and objects are the manifestations of what the factory or design or blueprint says to make.
There are 2 major characteristics of an object.
There is a saying within the Smalltalk community - "Everything is an object." This is a true statement and because of that, the only way to get something to happen within Smalltalk is to send messages to objects which activate their methods. So let's take this theory and apply it to the Smalltalk language.
Below will be a series of Smalltalk "statements" or "expressions" (whichever term make you more comfortable), highlighted in yellow, with an object-oriented explanation of how Smalltalk interprets that statement. It is extremely important that you understand the explanation. It may seem a bit wordy or lengthy, but nowhere else will you get a more thorough explanation for what is really going on.
Four (4) is a number. More specifically, it is a particular type of number called an Integer (integers are whole numbers). In VisualWorks, integers are divided into SmallIntegers and LargeIntegers. So, in OO terms, four (4) is an object and SmallInteger is its class. Another way of saying that would be "4 is an instance of the SmallInteger class". It's like re-wording the sentence "A beagle is a particular type of dog" to "Snoopy is an instance of the Dog class".Is it starting to make some sense? Perhaps another example might help.
The word "pots" is contained within single quotes. To Smalltalk, this is just a sequence of characters otherwise known as a String. String is the class and "pots" is a particular instance of the String class. We could have easily used an example that was not a word, like "wxyz". If you think about it, the word "pots" to Smalltalk is not a "word" at all. To those who speak English, it is a word, but to Smalltalk, it is just a sequence of characters and because it was contained within single quotes, Smalltalk calls it a String. String is the class and "pots" (or "wxyz") is a particular instance of the String class.Hopefully, it's starting to make some more sense. Maybe not TOTAL sense, but at least some. How about one more, just for kicks? This one will really stretch your mind and test your understanding of objects and methods. If nothing else, it is an extremely interesting way of looking at something incredibly simple and explaining it a whole different way.
3 + 4
The 3 and the 4 are objects, instances of the SmallInteger class. So far, so good. Therefore, since every Smalltalk expression must contain at least an object and a message, the plus sign must be the message. Remember, in Smalltalk, every expression must eventually break down to the basic building block of Object - Method. This particular message is called a binary method because it involves 2 objects instead of one (that ought to make some sense, right?). If not, it will be further explained later. Since 3 is an instance of the SmallInteger class (it is called the receiver of the plus message), Smalltalk looks for the plus method for the SmallInteger class. It finds it. The plus method says that it expect a parameter to be passed (sent) to it. That would be the 4. The plus method is now happy and proceeds to execute the instructions it was designed to perform. It took the values of the 3 and 4 objects, added them together and returned another object, which in this case is the SmallInteger instance of 7.This may seem a bit odd. After all, most of your life, you were told that the plus sign was a mathematical operation. Other programming languages treat the plus sign as a command or built-in function. Now you are told that in Smalltalk, the plus sign is a message. Eventually, this will start to make more sense.
The first 2 examples above represent the basic building block of the Smalltalk
language. Just as in English, where the simplest grammatically-correct sentence
contains just two words, a noun and a verb ("He jumped." or "She walks."), so too
is a Smalltalk expression. It contains an object and a method (4 squared).
These are called Unary Messages because they involve one object and one method.
There are two other types of expressions in Smalltalk (which will be covered in a
later primer) but those types will eventually break down to a unary message.
Message versus Method. What's the Difference?If you re-read those explanations above carefully, you will notice a subtle interchangability between the words "message" and "method". Are they the same? Actually, they are not the same and it is very important that you understand why they are not the same. The best way to explain this is with another example.
Remember, the word "pots" to Smalltalk is not a "word" at all. To those who speak English, it is a word, but to Smalltalk, it is just a sequence of characters which Smalltalk calls a String. String is the class and "pots" is a particular instance of the String class.In a real-life analogy, consider this. Walk into a night club where the guy at the piano is taking requests. Ask him to play "Far Far Away". He responds "I don't know that one." Ask him to play "Mack the Knife". He responds "OK" and then proceeds to play it. If the piano player (an instance of the NightClubPianoPlayer class) doesn't know a particular request (which is the message), he will come back and say "I don't know that one." This means that the request (message) is not part of his library of known songs (methods). If the piano player does know a particular request (message), he will come back and say "Here it is." and proceeds to execute the song. This means that the request (message) was part of his library of known songs (methods).
SummaryHopefully, with the explanations and examples presented in this primer, you are beginning think in terms of objects. Keep these examples in mind when you return to the main tutorial lessons.