What’s the difference between an class and instance variable?
A class variable is shared among all instances of that type of class. An instance variable is an attribute of one specific instantiated object.
In the following Squeak code what type of method (class or instance) is "new" and what type is "initialize"?
joe := Box new initialize
Box new calls the class method new on the Box class.
‘initialize’ is an instance method called on some instance of box (that was created from ‘Box new’).
History of Object Systems
What is Sketchpad?
Sketchpad is a drawing program created in the 60s at MIT by Ivan Sutherland. It was one of the first utilizations of an OO paradigm. In a sketchpad, a user could make icon-like objects that could be reused throughout the drawing. Once the user drew a tree, he/she could reuse that tree to make a forest.
What is Simula?
Simula was a ground breaking procedural programming language that implemented the idea of objects and reuse. As the name suggests, Simula was used to write simulations. A programmer could define a class-like activity which could have multiple instances called processes. Each process was its own entity, and all of its attributes were separate from other processes, hence encapsulated. This was an important concept for simulating the real world.
How did OO ideas get out into the mainstream (out of the research lab)?
At Xerox Alan Kay led the creation of Smalltalk – the first modern Object Oriented language. In addition to being the first OO system, Smalltalk also introduced many new graphics concepts such as a bitmapped display, windows and a mouse. Smalltalk made it into the mainstream after being released to major computer companies such as IBM and HP. If you have seen “Pirates of Silicon Valley,” you may also remember that Steve Jobs and Bill Gates used these GUI concepts that were pioneered at Xerox in Smalltalk. Note here that “mainstream” refers to being known in the computer industry rather than usage by the general public.
How are Java and Squeak similar and how are they different?
The most obvious similarity between Java and Squeak is that both languages use the OO paradigm. However, in a technical sense, Smalltalk is considered to be a more pure version of an object oriented language. Everything is Smalltalk is an object; whereas Java contains a set of primitive types. Smalltalk does not contain any control structures like Java’s “if” statements and “do” loops. Instead “if” and “do” area messages, or methods, sent to objects.
| anArray aString |
anArray := #('abc' 'def' 'ghi' 'jkl').
aString := ''.
anArray do: [:each |
aString := aString, each.].
Transcript show: aString.
What appears on the Transcript?
The transcript reads: abcdefghijkl
How many times did the do: loop get executed?
| i test |
i := 1.
test := (i < 10).
[test] whileTrue: [Transcript show: 'hello'.
i := i + 1.].
How many times does hello get printed in this example?
This is an infinite loop. Hello will print forever. Note that if the statement
test := (i < 10). was instead
test := [i < 10]. then the code would execute 9 times because the block
[i < 10] would be evaluated after each iteration of the loop.
Define each and give an example.
The ability of an object to take on the same attributes and methods of a parent class. This allows an object to become a special type of is parent type.
Delegation Delegation occurs when a class leaves the implementation specifics of a method to some other class. The delegate class is treated like a black box in that the user does not need to know its implementation specifics – they only need to know the service it provides. The book defines delegation as asking another object to perform the requested service for the requested object. When the Clock object is asked what is the current hour, the clock asks the Time object to return the hour.
The ability of an object to appear to be another TYPE of object. A Cat is a type of Mammal. Therefore a cat can be treated as a Cat or a Mammal.
Encapsulation The concept that an object is its own separate entity with its own attributes that cannot be changed without its consent. A Color is made up of a Red, Green, and Blue components with values from 0 to 255. However, users only see an electromagnetic wave with frequency between that of an infrared and ultraviolet wave.
“MVC describes a user interface in terms of a model of the real world, which is presented in a view [or user interface], with user input handled by one or more controllers [thus linking the model with the view]” – from the book.
What is wrong with the following and how do you fix it?
1) We decided to have one shape class and have a type attribute which can be “Circle”, “Rectangle”, or “Triangle.” We have to case on the type to figure out what to draw. Later we case on the type again to output to postscript.
Shape knows too much. This design is not object oriented. We should make Shape an abstract class that can draw itself and output to postscript. Circle, Rectangle, and Triangle are should subclass Shape and implement the aforementioned methods. Through polymorphism, circle, rectangle, and triangle can act as a shape.
2) Boxes need to use a pen so we had Box inherit from Pen.
A box is not a type of pen. Being a pen is not the primary purpose of box. Box should have a pen attribute.
3) We got lots of coding done because we were able to copy code to may different classes.
If the code has to change, then it must be changed in many different places. If code is copied to many different classes, the classes that use the same code can most likely be generalized into one super class. The classes that use that code can inherit from the single class.
4) We really only need one “main” class and a couple helper classes.
This is not object oriented code. Segregate the assignment into real-life entities. Each entity should be simple and have a clear responsibility.
5) We call the class that edits the image part of the slide “EditBackground”.
Class names should be a noun. If this “EditBackground” thing is a reusable class then it should be called “BackgroundEditor” or “ImageEditor.”
Part I: In detail, describe a feature of Squeak that you have found particularly useful. What is the feature? Why is it useful? How do you invoke it?
Learning the Squeak object model is one of the more difficult challenges in writing Smalltalk code. One of the more efficient methods of learning about an object is to browse the hierarchy of the object. This is the most organized fashion of viewing the methods and attributes a class.
The interface of the hierarchy browser is similar to the more popular ‘System Browser,’ but the hierarchy browser only segregated into three browsing panes. The pane of the far left has a tree of classes. The two panes on the right are the method category pane and the method pane (just like the system browser). The class pane lists the entire chain of super classes that the current class extends. Additionally, the pane lists all of the subclasses that extend the current class.
The system browser does not list inherited attributes or methods. Using the hierarchy browser these items are easily accessed by looking at the super class.
The hierarchy browser may be opened 2 ways (that I know of):
1) Highlight the desired class in the system browser. Click hierarchy.
2) For browsing a morph:
Windows: Ctrl + right click on the morph – a menu will pop up. Select ‘browse heirarchy’.
Linux : Ctrl + shift + left click on the morph – a menu will pop up. Select ‘browse heirarchy’.
Part 2: Read Other Posts and Pick a Good One
Wes Floyd had a useful post on getting user input. For Milestone 1 I burned a lot of time messing with TextInputMorph. The timer came out looking pretty nice, but not with a large cost in time. If I had known about the fill-in-the blank morph, I would have moved through that milestone more smoothly. Perhapse I will use this morph in the future.
Blake O'Hare wrote about one of my least favorit problems: the cursor focus following the mouse pointer. Going with 21 years of intuition, I always click in the text box I want to type in. I busted a few capillaries in my eye watching the cursor randomly switching text areas - sometimes while I was typing. It took me a week to realize that the cursor will jump to another part of the screen if I move the mouse over another text field. It's to bad it took me so long to figure this out.
Part 1: What are the differences between creational, structural, and behavioral patterns?
Creational design patterns may be used to simplify the instantiation of an object. A few examples of creational design patterns include the singleton pattern and the factory pattern. Factories can relieve a burden on a programmer by automating the selection of a class to be used. For instance, the java BorderFactory allows a programmer to just make borders rather than having to sift through the object model to find out what class he needs to make a specific border.
Structural patterns are used to create an interface between foreign objects. These designs can facilitate the reuse of code using “wrapper” classes and “bridges” to make an object appear to integrate seamlessly into a system. This pattern can be used to create a higher level interface so that users can more readily use more complex, low-level classes.
The basic idea of behavioral design patterns is to enhance communication between multiple type of objects. Many of these patterns separate complex algorithms from classes that initialize the algorithm. The event listener model is a great example of this methodology. Here programmers can increase the flexibility of their program by decoupling the event trigger and the event handler.
Part 2 - Describe an Interesting Pattern
The singleton pattern is used when only one instance of a class will be created during the life of a program. By definition, the pattern will ensure that only one instance is created. Once the programmer has ensured that only a single instance will exist, then he will be able to statically access that instance. In other words, the programmer will not need a pointer to the object he is trying to access.
Singleton classes can be extremely useful. Consider the example of a server application that has a class ClientManager that has a list of all connected clients. Now any class that needs to get a client connection will need an instance of a ClientManager object. This can get messy because pointers to ClientManager are getting thrown all over the place. However, ClientManager can be implemented as a singleton object. In this case, every class can easily access the list of clients. This is much cleaner than the alternative method. I have used and love the singleton design pattern.
Note: the most efficient way to view these references is to click on the first link. The first link is like the parent page to all the subsequent links.
Encapsulation in Object-Oriented Programming:
Comparison & Evaluation
Author: W. Al-Ahmad
The article I read compared the differences in encapsulation between four different programming languages. For simplicity sake, I will only report on the two we are (or will be) familiar with: Java and Smalltalk.
Concerning encapsulation, one of the main characteristics that set Smalltalk apart from Java is the absence of scoped variables. Smalltalk classes do not have public and private variables/methods. Sticking to a simplistic rule set, all class attributes are private and all methods are public. Java’s more traditional approach is of course slightly more complicated with four types of scope (public, private, protected, and package).
This fundamental difference has a few implications on object oriented design. As a general design principle, attributes of a class should never be public. A benefit of Smalltalk is the strict enforcement of this rule; whereas Java’s structure will permit the more “hackish” style of code. From another stand point, method access cannot be restricted in Smalltalk. This can exacerbate the leap from C-like languages to Smalltalk. Scoped methods disallow sub classes from changing the inner workings of their super class. In Java, a public class may call a helper private class. With this same design in Smalltalk, the same “private” class could be called without the proper requisite of the public class. A different mind set will be required when encapsulating data in Smalltalk if you are coming from a Java background.
I reviewed Elizabeth Solomon’s post concerning the qualifications of an object oriented language. The material she reported on claimed Smalltalk as one of the only “pure” object oriented languages because of their strong roots in encapsulation, inheritance, and polymorphism. However, the conclusion of my article states a slightly different case. The author states that, “an OOPL must provide language constructs ... to restrict access to the representation of objects.” In this he refers to having scopes to hide the insides of an object (as desired). In this case, he make the point that Smalltalk does not completely cover this point on the OOPL check list. This argument may be a bit persnickety, but the point I am trying to make is that no language is truly a “pure” OO language.
Links to this Page