1.44k likes | 1.72k Views
An Introduction to Smalltalk First Pure Object Oriented Language By: Misagh Bagherian S.Jalal Kazemitabar Emad Livani University of Tehran – ECE Department Programming Languages Course, Fall 2004. Smalltalk Quotes. Common languages are tools, Smalltalk is a piece of art. Alan Kay:
E N D
An Introduction to Smalltalk First Pure Object Oriented Language By: Misagh Bagherian S.Jalal Kazemitabar Emad Livani University of Tehran – ECE Department Programming Languages Course, Fall 2004
Smalltalk Quotes • Common languages are tools, Smalltalk is a piece of art. • Alan Kay: • Simple things should be simple. Complex things should be possible
Outline • Motivation to Object-Oriented Programming • Smalltalk History • Structural Organization • Classes and Subclasses • Objects and Message Sending • Implementation: Classes and Objects • Syntax Overview • Conclusions
Part 1 • Structured Programming • Object Orientation • Objects • ADTs • Encapsulation • Language Concepts • Smalltalk History • Why Smalltalk ?
Structured Programming • Definetasks to be performed • Breaktasks into smaller and smaller pieces • Until you reach a size you can implement • Define the data structures to be manipulated • Design how functions interact • What's the input • What's the output • Group functions into components • "units" or “modules“ • Write the code
Program Structure • Program Structure in Structural Approach Main Program Sub-program Sub-program Sub-program Sub-program Sub-program
Abstract Data Types (ADTs) • Main ideas: • Separate interface from implementation • Example: • Sets have empty, insert, union, is_member?, … • Sets implemented as … linked list … • Use type checking to enforce separation • Client program only has access to operations in interface • Implementation encapsulated inside ADT construct
Object Oriented Programming • First goal: Model the objects of the world • Noun-oriented • (in contrast to verb-orientation in Structured Programming) • Focus on the domain of the program • Phases • Object-oriented analysis: Understand the domain • Define an object-based model of it • Object-oriented design: Define an implementation strategy • Design the solution • Object-oriented programming: Build it
Object Oriented Programming • Primary object-oriented language concepts • dynamic lookup • encapsulation • inheritance • sub typing
hidden data msg1 method1 . . . . . . msgn methodn Objects • An object consists of • hidden data instance variables, also called member data hidden functions also possible • public operations methods or member functions can also have public variables in some languages • Object-oriented program: • Send messages to objects
Object Orientation • Programming methodology • organize concepts into objects and classes • build extensible systems • Language concepts • dynamic lookup • encapsulation • subtyping allows extensions of concepts • inheritance allows reuse of implementation
Dynamic Lookup • In object-oriented programming, object message (arguments) code depends on object and message • In conventional programming, operation (operands) meaning of operation is always the same Fundamental difference between abstract data types and objects
Example • Add two numbers x add (y) different add if x is integer, complex, etc. • Conventional programming add (x, y) function add has fixed meaning • Very important distinction: • Overloading is resolved at compile time, • Dynamic lookup at run time
Language concepts • “dynamic lookup” • different code for different object • integer “+” different from real “+” • encapsulation • sub typing • inheritance
How’d we get from there to here? • How did we move from structured to object-oriented? • Key ideas • Master-drawings in Sketchpad • Simulation “objects” in Simula • Alan Kay and his desire to make software better • More robust • More maintainable • More scalable
Encapsulation • Builder of a concept has detailed view • User of a concept has “abstract” view • Encapsulation separates these two views • Implementation code: operate on representation • Client code: operate by applying fixed set of operations provided by implementer of abstraction Object message
Encapsulation • How does it support our objectives? • Models real world • examples: driving a car • Reusability & Ease of use • other users need only know ‘interface’, not the implementation
Comparison with ADTs • Traditional (non-OO) approach to encapsulation is through abstract data types • Advantage • Separate interface from implementation • Disadvantage • Not extensible in the way that OOP is
Abstract Data Types (ADTs) • Guarantee invariants of data structure • only functions of the data type have access to the internal representation of data • Limited “reuse” • Cannot apply queue code to pqueue, except by explicit parameterization, even though signatures identical • Cannot form list of points, colored points • Data abstraction is important part of OOP, innovation is that it occurs in an extensible form
Alan Kay • University of Utah PhD student in 1966 • Read Sketchpad, Ported Simula • Saw “objects” as the future of computer science • His dissertation: Flex, an object-oriented personal computer • A personal computer was a radical idea then • How radical? "There is no reason anyone would want a computer in their home." (Ken Olsen, Digital Equipment Corp, 1977)
Alan Kay • Alan C. Kay: ”In the 1990’s there will be millions of personal computers. They will be the size of notebooks of today, have high-resolution flat-screen reflective displays, weigh less than ten pounds, have ten to twenty times the computing and storage capacity of an Alto. Let’s call them Dynabooks. The purchase price will be about that of a color television set of the era, ...” (1971)
Alan Kay • Alan Kay’s categorization of Programming Languages: • Agglutination of Features • Cobol, PL/1, Ada, etc. • Crystallization of Style • Lisp, APL, and Smalltalk
Alan Kay’s Insights • “Computer” as collection of Networked Computers • All software is simulating the real world • Biology as model for objects • Bacterium has 120M of info, 1/500th of a Cell, and we have 10^13 of these in us • What man-made things can scale like that? • Stick a million dog houses together to get a skyscraper? • Internet does, but how can we make that the norm?
Birth of Objects → Sketchpad • Ivan Sutherland's Sketchpad, 1963
Birth of Objects → Sketchpad • First object-oriented drawing program • Master and instance drawings • Draw a house • Make two instances • Add a chimney to the master • Poof! The instances grow a chimney
Birth of Objects → Simula • Simulation programming language from Norway, 1966 (Kristen Nygaard & Ole-Johan Dahl) • Define an activity which can be instantiated as processes • Each process has its own data and behavior • In real world, objects don't mess with each others' internals directly • (Simulated) Multi-processing • No Universal Scheduler in the Real World
Birth of Objects • Objects as models of real world entities • Objects as Cells • Independent, indivisible, interacting -- in standard ways • Scales well • Complexity: Distributed responsibility, emergent complexity • Robustness: Independent • Supporting growth: Same mechanism everywhere • Reuse: Provide services, just like in real world
A Personal Computer for Children • Flex, an object-oriented personal computer
FLEX • Enabled by Moore's Law • Imagining personal computing in 1969 • Logo, Sketchpad, and Simula • Learning representations and knowledge through programming them • Keyboard and drawing tablet • Computer as meta-medium • The first medium to encompass other media
Xerox PARC (Palo Alto Research Center) • Smalltalk-72 • Alan Kay, • Dan Ingalls, • Adele Goldberg, • Ted Kaehler
Goal: Dynabook • Dynabook: • Small • Handheld • Wireless Networking • A Personal Computer for Children of All Ages Look familiar?
Smalltalk Inventions • Interface: • overlapping Windows • Icons, even iconic programming • Pop-up menus • Mouse as Pointing device • Object-oriented programming • Multimedia authoring environment: Drawing, music, animations
How Smalltalk was Implemented • Byte code compiler • Machine language for a make-believe computer • Virtual machine to create the make-believe computer • Invented years earlier by Burroughs • Used in UCSD Pascal, Java, Python, etc. • Four files needed for this implementation: • VM • Image file (in byte code) • Sources file (all sources always came along) • Changes file (added sources by user)
Tradeoffs of Virtual Machines • Advantages: • Can be easily ported • If VM is well-designed, small amount of code needs to be written for each platform • The VM can be very small • PowerMac 308K, Win32 328K, Linux-Intel 276K, SGI Irix 609K, Solaris 816K • Disadvantages: • Hard to make it efficient
1981: Xerox releases Smalltalk-80 • In 1981: Finally Xerox releases Smalltalk-80
Why Smalltalk ? • Pure object-oriented language • Uniform • Simple • Powerful • Dynamically typed (no type coercion…) • No primitive types • Syntax simple so force to think !!!!! • Discuss design, OO modeling and not syntax!!
Why Smalltalk ? • Object Culture • Environment completely written in Smalltalk • Powerful development tools • Good to model real world applications
Structural Organization Classes and Subclasses Objects and Message Sending Part 2
Smalltalk:Interactive & Interpreted • To satisfy Dynabook’s requirements, it has to be highly interactive. communication with either pointing or typing commands. • Primary ways of defining things: • Bind a name to an object X3 yx+1 • Class definition x*2 => send message ‘*2’ to object x
Objects React to Messages • Smalltalk’s graphical orientation is taken from LOGO. • LOGO introduced a style of graphics: “turtle graphics”. Turtle drawing object . In Smalltalk :pen.
The position of a pen called Scribe Changing position and draw line Scribe goto: 200@400
Draw another line: Scribe penup. Scribe goto:500@100. Scribe pendn. Scribe goto:500@400. Go to a specific direction: Scribe go:300
Turn and draw: Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300. • Control Structures: • Scribe penup;goto:400@800; • pendn;turn 180. • 4 timesRepeat: • [Scribe go:100. Scribe turn:90]
Important Ideas 1-Objects have a behavior. 2-Objects can be made to do things by sending them messages. 3-Repetitive operations can be simplified by using control structures.
Instantiation • A class is just a name for a particular kind of object. • Every object is an instance of some class. • We have to indicate the class of the instance. • The process of creating an object is called instantiation.
Instantiation is also a message sent to some object. • Which object should be sent the message ? • A universal system object responsible for all instantiations • The class relating the object. • For the sake of Information Hiding Principle: Each class is responsible for its own instantiation. • anotherScribe pen newAt: 200@800
Class Definition • To draw another box - Move the pen - Instantiate a new pen • Violates the Abstraction Principle • A better solution: define a class box which can be instantiated any number of times
In defining a class, there exists some instance methods and some class methods which describe the behavior of that class. (ex. Instantiation method newAt) • The instance variable “self”, is implicitly bound to the instance to which it is local.
Example : box class • Class definition written in tabular form class name box loc tilt size scribe instance var names instance messages and methods shape || scribe penup; goto: loc; turnTo: tilt; pendn. 4 timesRepeat: [scribe go: size; turn: 90] erase || scribe color background. self shape
Classes & Subclasses • Smalltalk objects model real-world objects • Many of the ideas in Smalltalk derive from Simula. • Data values inside an object can represent the properties and relations in which that object participates • The behavior of the Smalltalk object can model the behavior of the corresponding object