Stéphane Ducasse 1 Some Advanced Points on Classes.

Slides:



Advertisements
Similar presentations
Learn all about the year.
Advertisements

S.Ducasse 1 Metaclasses in 7 Steps Classes are objects too... Classes are instances of other classes... One model applied twice.
Chubaka Producciones Presenta :.
The Months and The Seasons Prepared by Claudia Doria and Terra Myers.
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213
2012 CALENDAR. JANUARY 2012 Sunday 日 Monday 月 Tuesday 火 Wednesday 水 Thursday 木 Friday 金 Saturday 土
Principles of Object-Oriented Software Development The language Smalltalk.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
7. Understanding Classes and Metaclasses. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.2 Roadmap  Metaclasses in 7 points  Indexed.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Stéphane Ducasse8.1 Syntax and Messages The syntax of Smalltalk is simple and uniform, but it can look strange at first sight! Literals: numbers, strings,
Stéphane Ducasse 1 The Taste of Smalltalk.
January 2012 Monday Tuesday Wednesday Thursday Friday Sat/ Sun / /8 14/15 21/22 28/
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Some Basic Points on Classes
S.Ducasse Stéphane Ducasse 1 A Taste of Smalltalk.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
9. Understanding Classes and Metaclasses. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify your metamodel — A fully reflective system models.
© Oscar Nierstrasz ST — xxx X.1 Class Methods - Class Instance Variables  Classes (Packet class) represents class (Packet).  Class instance variables.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Stéphane Ducasse«ChapterNr».1 Advanced Classes Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
Stéphane Ducasse4.1 A Taste of Smalltalk Two examples: –‘hello world’ –A Tomagoshi To give you an idea of: –the syntax –the elementary objects and classes.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAYSUNDAY WEEK WEEK WEEK WEEK WEEK CALENDAR PROJECT.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
What day is today? What`s the date?. Sunday Monday Tuesday Wednesday Thursday Friday Saturday What day is today?
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
CS 598 Scripting Languages Design and Implementation 7. Smalltalk 80.
S.Ducasse Stéphane Ducasse 1 Classes and Metaclasses - an Analysis.
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Stéphane Ducasse 1 Singleton.
S.Ducasse 1 7. Understanding Metaclasses. S.Ducasse 2 Roadmap Metaclasses in 7 points Indexed Classes Class Instance Variables Class Variables Pool Dictionaries.
By Monica Hilgemann Second Grade Teacher
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Stéphane Ducasse7.1 Let’s Play Objects Simulate a LAN physically Set up a context for –future chapters –Exercises Some forward references to intriguate.
1 Advanced Object Oriented Systems (CM0318) Lecture 8 (Last updated 15th February 2002)
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
Stéphane Ducasse 1 Abstract Classes.
The Calendar.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
S.Ducasse Stéphane Ducasse 1 Some Advanced Points on Classes.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
Smalltalk Meta-Programming Programming Languages HUJI 2010 Yardena Meymann.
S.Ducasse 1 Instance Initialization How to ensure that an instance is well initialized? Automatic initialize Lazy initialization Proposing the right interface.
Let’s Play Objects.
2.3 Collaboration Contracts
McDonald’s calendar 2007.
Introduction to Smalltalk
Introduction to Smalltalk
McDonald’s calendar 2007.
CSE 3302 Programming Languages
2015 January February March April May June July August September
Presentation transcript:

Stéphane Ducasse 1 Some Advanced Points on Classes

S.Ducasse 2 Outline Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries

S.Ducasse 3 Variable size instance How do we represent objects whose size is variable such an array Array new: 10 Array new: 15

S.Ducasse 4 Two Views on Classes Named or indexed instance variables Named: ‘addressee’ of Packet Indexed: Array Or looking at them in another way: Objects with pointers to other objects Objects with arrays of bytes (word, long) Difference for efficiency reasons: arrays of bytes (like C strings) are faster than storing an array of pointers, each pointing to a single byte.

S.Ducasse 5 Types of Classes Indexed Named Definition Method Examples NoYes #subclass:... Packet YesYes#variableSubclass:Array YesNo#variableByteSubclassString Method related to class types: #isPointers, #isBits, #isBytes, #isFixed, #isVariable, #kindOfSubclass

S.Ducasse 6 Constraints Classes defined using #subclass: support any kind of subclasses Classes defined using #variableSubclass: can only have: variableSubclass: or variableByteSubclass: subclasses pointer classes and byte classes don’t mix: e.g. only byte subclasses of byte classes.

S.Ducasse 7 Indexed Classes For classes that need a variable number of instance variables ArrayedCollection variableSubclass: #Array instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Collections-Arrayed' Array new: 4 -> #(nil nil nil nil) #( ) class isVariable -> true

S.Ducasse 8 Indexed Classes Indexed variable is implicitly added to the list of instance variables Only one indexed instance variable per class Access with #at: and #at:put: (#at:put: answers the value, not the receiver) Subclasses should also be indexed

S.Ducasse 9 Index access First access: anInstance at: 1 #size returns the number of indexed instance variables Instantiated with #new: max |t| t := (Array new: 4). t at: 2 put: 'lulu'. t at: 1 -> nil

S.Ducasse 10 Roadmap Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries

S.Ducasse 11 The Meaning of is-a A class defines the structure and the behavior of all its instances. Each instance possesses its own set of values. Instances share the behavior defined in their class with other instances via the instance of link.

S.Ducasse 12 The Meaning of Is-a Every object is an instance of a class. When anObject receives a message, the method is looked up in its class And it continues possibly in its superclasses Every class is ultimately a subclass of Object (except Object).

S.Ducasse 13 Lookup...

S.Ducasse 14 Remember: … Example: macNode name macNode is an instance of Workstation => name is looked up in the class Workstation name is not defined in Workstation => lookup continues in Node name is defined in Node => lookup stops + method executed

S.Ducasse 15 Roadmap Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries

S.Ducasse 16 Classes and Objects Classes are objects too The same principle is true for objects and classes Same lookup strategy Everything that works at instance levels works at class level In some language classes are not objects, still understanding it in Smalltalk will force you to really understand what instance/inheritance means

S.Ducasse 17 Class Responsibilities instance creation class information (inheritance link, instance variables, method compilation...) Examples: Node allSubclasses -> OrderedCollection (WorkStation OutputServer Workstation File) LanPrinter allInstances -> #() Node instVarNames -> #('name' 'nextNode') Workstation withName: #mac -> aWorkstation Workstation selectors -> IdentitySet (#accept: #originate:) Workstation canUnderstand: #nextNode -> true

S.Ducasse 18 A Class is an Object too… Every class (X) is the unique instance of its associated metaclass named X class Example: Node is the unique instance of “Node class” Point is the unique instance of “Point class”

S.Ducasse 19 A Class is an Object too… So messages sent to a class are looked up into the class of the class Node withName: #node1 Node is an instance of “Node class” withName: is looked up in the class “Node class” withName: defined in “Node class” lookup stops + method executed

S.Ducasse 20 Class Parallel Inheritance

S.Ducasse 21 Lookup and Class Methods

S.Ducasse 22 Class Parallel inheritance Workstation withName: #mac Workstation is an instance of Workstation class => withName: is looked up in the class Workstation class withName: is not defined in Workstation class => lookup continues in the superclass of Workstation class = Node class withName: is defined in Node class => lookup stops + method executed

S.Ducasse 23 About the Buttons Node class new withName: aString Instance of Node accept: aPacket

S.Ducasse 24 Where is new defined? Node new: #node1 – Node is an instance of Node class => new: is looked up in the class Node class – new: is not defined in Node class => lookup continues in the superclass of Node class = Object class – new: is not defined in Object class => lookup continues in the superclass of Object class....Class, ClassDescription, Behavior – new: is defined in Behavior => lookup stops + method executed. This is the same for Array new: 4 – new: is defined in Behavior (the ancestor of Array class) Hint: Behavior is the essence of a class. ClassDescription represents the extra functionality for browsing the class. Class supports poolVariable and classVariable.

S.Ducasse 25 Recap Everything is an object Each object is instance of one class A class (X) is also an object, the sole instance of its associated metaclass named X class An object is a class if and only if it can create instances of itself. A Metaclass is just a class whose instances are classes – Point class is a metaclass as its instance is the class Point

S.Ducasse 26 Roadmap Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries

S.Ducasse 27 Class Methods As any object a (meta)class can have methods that represent the behavior of its instance: a class Uniformity => Same rules as for normal classes No constraint: just normal methods Can only access instance variable of the class:

S.Ducasse 28 Class Method Examples NetworkManager class>>new can only access uniqueInstance class instance variable and not instance variables (like nodes). Default Instance Creation class method: new/new: and basicNew/basicNew: (see Direct Instance Creation) Packet new Specific instance creation method Packet send: ‘Smalltalk is fun’ to: #lpr

S.Ducasse 29 Class Instance Variables Like any object, a class is an instance of a class that can have instance variables that represent the state of a class. When Point defines the new instance variable z, the instances of Point have 3 value (one for x, one for y, and one for z) When a metaclass defines a new instance variable, then its instance (a Class) gets a new value in addition to subclass, superclasses, methodDict…

S.Ducasse 30 The Singleton Pattern A class having only one instance We keep the instance created in an instance variable WebServer class instanceVariableNames: 'uniqueInstance’ WebServer class>>new self error: 'You should use uniqueInstance to get the unique instance' WebServer class>>uniqueInstance uniqueInstance isNil ifTrue: [ uniqueInstance := self basicNew initialize]. ^ uniqueInstance

S.Ducasse 31 Singleton WebServer being an instance of WebServer class has an instance variable named uniqueInstance. WebServer has a new value that is associated with uniqueInstance

S.Ducasse 32 Design Implications An instance variable of a class can be used to represent information shared by all the instances of the class. However, you should use class instance variables to represent the state of the class (like the number of instances,...) and not information of its instance. Should use shared Variable instead (next Section).

S.Ducasse 33 Advanced Classes Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries

S.Ducasse 34 classVariable = Shared Variables How to share state between all the instances of a class: Use a classVariable a classVariable is shared and directly accessible by all the instances of the class and subclasses A pretty bad name: should have been called Shared Variables (now fixed in VW) Shared Variable => begins with an uppercase letter a classVariable can be directly accessed in instance methods and class methods

S.Ducasse 35 classVariable = shared Variab. (Sq) Magnitude subclass: #Date instanceVariableNames: 'julianDayNumber ' classVariableNames: 'DaysInMonth FirstDayOfMonth MonthNames SecondsInDay WeekDayNames ' poolDictionaries: '' category: 'Kernel-Magnitudes'

S.Ducasse 36 Date class>>initialize "Initialize class variables representing the names of the months and days and the number of seconds, days in each month, and first day of each month." MonthNames := #(January February March April May June July August September October November December ). SecondsInDay := 24 * 60 * 60. DaysInMonth := #( ). FirstDayOfMonth := #( ). WeekDayNames := #(Monday Tuesday Wednesday Thursday Friday Saturday Sunday ).

S.Ducasse 37 ClassVariable vs. Instance Variables

S.Ducasse 38

S.Ducasse 39 Class Instance Variables vs classVariables a classVariable is shared and directly accessible by all the instances and subclasses Class instance variables, just like normal instance variables, can be accessed only via class message and accessors: – an instance variable of a class is private to this class. Take care: when you change the value of a classVariable the whole inheritance tree is impacted!

S.Ducasse 40 Summary of Variable Visibility

S.Ducasse 41 ClassVariables... ClassVariables can be used in conjunction with instance variables to cache some common values that can be changed locally in the classes.

S.Ducasse 42 Example in the Scanner class a table describes the types of the characters (strings, comments, binary....). The original table is stored into a classVariable, its value is loaded into the instance variable. It is then possible to change the value of the instance variable to have a different scanner. Object subclass: #Scanner instanceVariableNames: 'source mark prevEnd hereChar token tokenType buffer typeTable ' classVariableNames: 'TypeTable ' category: 'System-Compiler-Public Access'

S.Ducasse 43 Roadmap Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries

S.Ducasse 44 poolVariables Shared variable => begins with a uppercase letter. Variable shared by a group of classes not linked by inheritance. Each class possesses its own pool dictionary (containing poolVariables). They are not inherited. DON’T USE THEM!

S.Ducasse 45 Examples of PoolDictionaries from the System: the class Text CharacterArray subclass: #Text instanceVariableNames: 'string runs ' classVariableNames: '' poolDictionaries: 'TextConstants ' category: 'Collections-Text' Elements stored into TextConstants like Ctrl, CR, ESC, Space can be directly accessed from all the classes like ParagraphEditor....

S.Ducasse 46 Example of PoolVariables Smalltalk at: #NetworkConstant put: Dictionary new. NetworkConstant at: #rates put: Packet>>computeAverageSpeed... NetworkConstant at: #rates Equivalent to : Object subclass: #Packet instanceVariableNames: 'contents addressee originator ' classVariableNames: ‘Domain’ poolDictionaries: 'NetworkConstant' Packet>>computeAverageSpeed... … rates rates is directly accessed in the global dictionary NetworkConstant.

S.Ducasse 47 What you should know Classes are objects too Class methods are just methods on objects that are classes Classes are also represented by instance variables (class instance variables) (Shared Variables) ClassVariables are shared among subclasses and classes (metaclass)