SlideShare una empresa de Scribd logo
1 de 53
Descargar para leer sin conexión
9. Understanding Classes and Metaclasses
Birds-eye view
© Oscar Nierstrasz
ST — Introduction
1.2
Reify your metamodel — A fully reflective
system models its own metamodel.
Reify your metamodel — A fully reflective
system models its own metamodel.
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.3
Roadmap
> Metaclasses in 7 points
> Indexed Classes
> Class Instance Variables
> Class Variables
> Pool Dictionaries
Selected material courtesy Stéphane Ducasse
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.4
Roadmap
> Metaclasses in 7 points
> Indexed Classes
> Class Instance Variables
> Class Variables
> Pool Dictionaries
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.5
Metaclasses in 7 points
1. Every object is an instance of a class
2. Every class eventually inherits from Object
3. Every class is an instance of a metaclass
4. The metaclass hierarchy parallels the class hierarchy
5. Every metaclass inherits from Class and Behavior
6. Every metaclass is an instance of Metaclass
7. The metaclass of Metaclass is an instance of Metaclass
Adapted from Goldberg & Robson, Smalltalk-80 — The Language
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.6
Metaclasses in 7 points
1. Every object is an instance of a class
2. Every class eventually inherits from Object
3. Every class is an instance of a metaclass
4. The metaclass hierarchy parallels the class hierarchy
5. Every metaclass inherits from Class and Behavior
6. Every metaclass is an instance of Metaclass
7. The metaclass of Metaclass is an instance of Metaclass
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.7
1. Every object is an instance of a class
Remember the Snakes and Ladders Board Game …
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.8
Metaclasses in 7 points
1. Every object is an instance of a class
2. Every class eventually inherits from Object
3. Every class is an instance of a metaclass
4. The metaclass hierarchy parallels the class hierarchy
5. Every metaclass inherits from Class and Behavior
6. Every metaclass is an instance of Metaclass
7. The metaclass of Metaclass is an instance of Metaclass
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.9
2. Every class inherits from Object
> Every object is-an Object
=
— The class of every object
ultimately inherits from Object
Caveat: in Pharo, Object has a superclass called ProtoObject
aSnakeSquare is-a SnakeSquare
and is-a BoardSquare
and is-an Object
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.10
The Meaning of is-a
> When an object receives a message, the method is
looked up in the method dictionary of its class, and, if
necessary, its superclasses, up to Object
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.11
Responsibilities of Object
> Object
— represents the common object behavior
– error-handling, halting …
— all classes should inherit ultimately from Object
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.12
Metaclasses in 7 points
1. Every object is an instance of a class
2. Every class eventually inherits from Object
3. Every class is an instance of a metaclass
4. The metaclass hierarchy parallels the class hierarchy
5. Every metaclass inherits from Class and Behavior
6. Every metaclass is an instance of Metaclass
7. The metaclass of Metaclass is an instance of Metaclass
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.13
3. Every class is an instance of a
metaclass
> Classes are objects too!
— Every class X is the unique instance of its metaclass, called X
class
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.14
Metaclasses are implicit
> There are no explicit metaclasses
— Metaclasses are created implicitly when classes are created
— No sharing of metaclasses (unique metaclass per class)
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.15
Metaclasses by Example
BoardSquare allSubclasses
SnakeSquare allSubclasses
SnakeSquare allInstances
SnakeSquare instVarNames
SnakeSquare back: 5
SnakeSquare selectors
SnakeSquare canUnderstand: #new
SnakeSquare canUnderstand: #setBack:
a Set(SnakeSquare FirstSquare LadderSquare)
a Set()
an Array(<-2[6] <-4[11] <-6[11])
#('back')
<-5[nil]
an IdentitySet(#setBack:
#printOn: #destination)
false
true
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.16
Metaclasses in 7 points
1. Every object is an instance of a class
2. Every class eventually inherits from Object
3. Every class is an instance of a metaclass
4. The metaclass hierarchy parallels the class
hierarchy
5. Every metaclass inherits from Class and Behavior
6. Every metaclass is an instance of Metaclass
7. The metaclass of Metaclass is an instance of Metaclass
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.17
4. The metaclass hierarchy parallels the
class hierarchy
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.18
Uniformity between Classes and Objects
> Classes are objects too, so …
— Everything that holds for objects holds for classes as well
— Same method lookup strategy
– Look up in the method dictionary of the metaclass
back: is a Snake constructor method
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.19
About the Buttons
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.20
Metaclasses in 7 points
1. Every object is an instance of a class
2. Every class eventually inherits from Object
3. Every class is an instance of a metaclass
4. The metaclass hierarchy parallels the class hierarchy
5. Every metaclass inherits from Class and
Behavior
6. Every metaclass is an instance of Metaclass
7. The metaclass of Metaclass is an instance of Metaclass
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.21
5. Every metaclass inherits from Class and
Behavior
Every class is-a Class
=
—The metaclass of every
class inherits from Class
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.22
Where is new defined?
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.23
Responsibilities of Behavior
> Behavior
— Minimum state necessary for objects that have instances.
— Basic interface to the compiler.
— State:
– class hierarchy link, method dictionary, description of instances
(representation and number)
— Methods:
– creating a method dictionary, compiling method
– instance creation (new, basicNew, new:, basicNew:)
– class hierarchy manipulation (superclass:, addSubclass:)
– accessing (selectors, allSelectors, compiledMethodAt: )
– accessing instances and variables (allInstances, instVarNames)
– accessing class hierarchy (superclass, subclasses)
– testing (hasMethods, includesSelector, canUnderstand:,
inheritsFrom:, isVariable)
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.24
Responsibilities of ClassDescription
> ClassDescription
— adds a number of facilities to basic Behavior:
– named instance variables
– category organization for methods
– the notion of a name (abstract)
– maintenance of Change sets and logging changes
– most of the mechanisms needed for fileOut
— ClassDescription is an abstract class: its facilities are
intended for inheritance by the two subclasses, Class and
Metaclass.
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.25
Responsibilities of Class
> Class
— represents the common behavior of all classes
– name, compilation, method storing, instance variables …
— representation for classVariable names and shared pool
variables (addClassVarName:, addSharedPool:,
initialize)
— Class inherits from Object because Class is an Object
– Class knows how to create instances, so all metaclasses should
inherit ultimately from Class
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.26
Metaclasses in 7 points
1. Every object is an instance of a class
2. Every class eventually inherits from Object
3. Every class is an instance of a metaclass
4. The metaclass hierarchy parallels the class hierarchy
5. Every metaclass inherits from Class and Behavior
6. Every metaclass is an instance of Metaclass
7. The metaclass of Metaclass is an instance of Metaclass
6. Every metaclass is an instance of
Metaclass
© Oscar Nierstrasz
LECTURE TITLE
27
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.28
Metaclass Responsibilities
> Metaclass
— Represents common metaclass Behavior
– instance creation (subclassOf:)
– creating initialized instances of the metaclass’s sole instance
– initialization of class variables
– metaclass instance protocol (name:inEnvironment:subclassOf:....)
– method compilation (different semantics can be introduced)
– class information (inheritance link, instance variable, ...)
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.29
Metaclasses in 7 points
1. Every object is an instance of a class
2. Every class eventually inherits from Object
3. Every class is an instance of a metaclass
4. The metaclass hierarchy parallels the class hierarchy
5. Every metaclass inherits from Class and Behavior
6. Every metaclass is an instance of Metaclass
7. The metaclass of Metaclass is an instance of
Metaclass
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.30
7. The metaclass of Metaclass is an
instance of Metaclass
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.31
Navigating the metaclass hierarchy
MetaclassHierarchyTest>>testHierarchy
"The class hierarchy"
self assert: SnakeSquare superclass = BoardSquare.
self assert: BoardSquare superclass = Object.
self assert: Object superclass superclass = nil.
"The parallel metaclass hierarchy"
self assert: SnakeSquare class name = 'SnakeSquare class'.
self assert: SnakeSquare class superclass = BoardSquare class.
self assert: BoardSquare class superclass = Object class.
self assert: Object class superclass superclass = Class.
self assert: Class superclass = ClassDescription.
self assert: ClassDescription superclass = Behavior.
self assert: Behavior superclass = Object.
"The Metaclass hierarchy"
self assert: SnakeSquare class class = Metaclass.
self assert: BoardSquare class class = Metaclass.
self assert: Object class class = Metaclass.
self assert: Class class class = Metaclass.
self assert: ClassDescription class class = Metaclass.
self assert: Behavior class class = Metaclass.
self assert: Metaclass superclass = ClassDescription.
"The fixpoint"
self assert: Metaclass class class = Metaclass
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.32
Roadmap
> Metaclasses in 7 points
> Indexed Classes
> Class Instance Variables
> Class Variables
> Pool Dictionaries
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.33
Two ways to represent objects
> Named or indexed instance variables
— Named: name of GamePlayer
— Indexed: #(Jack Jill) at: 1
> 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.
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.34
Different methods to create classes
Indexed Named Instance
Variables Definition Method
No Yes #subclass: …
Yes Yes #variableSubclass: …
Yes No #variableByteSubclass: …
> See the subclass creation protocol of Class
> Constraints
— Pointer classes defined using #subclass: support any kind of subclasses
— Byte classes defined using #variableSubclass: can only have:
variableSubclass: or variableByteSubclass: subclasses
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.35
Testing methods
> See testing protocols of Behavior:
— #isPointers, #isBits, #isBytes, #isFixed, #isVariable
— #kindOfSubclass
Object allSubclasses select: [:class | class isBytes]
a Set(ByteArray MwSynchronizationWrapper
MwBlockMethodWrapper ExternalAddress MCMockClassH
LargeNegativeInteger LargePositiveInteger
ByteSymbol MwCountMethodWrapper MwTimeMethodWrapper
MwMethodWrapper MwBlockHandlerMethodWrapper
ByteString MwCalledMethodWrapper UUID
CompiledMethod)
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.36
Defining Indexed Classes
> Example — instantiating an Array:
ArrayedCollection variableSubclass: #Array
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Collections-Arrayed'
Array new: 4
#(nil nil nil nil)
#(1 2 3 4) class isVariable
true
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.37
Defining an Indexed Class
Object variableSubclass: #IndexedObject
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: ''
(IndexedObject new: 2)
at: 1 put: 'Jack';
at: 2 put: 'Jill';
at: 1
'Jack'
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.38
Indexed Classes / Instance Variables
> An indexed variable is implicitly added to the list of
instance variables
— Only one indexed instance variable per class
— Access with at: and at:put:
– NB: answers the value, not the receiver
> Subclasses should also be indexed
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.39
Roadmap
> Metaclasses in 7 points
> Indexed Classes
> Class Instance Variables
> Class Variables
> Pool Dictionaries
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.40
Class Instance Variables
> Class are objects too
— Instances of their metaclass
– Methods looked up in the method dictionary of their metaclass
— Can also define instance variables
> When a metaclass defines a new instance variable, then
its instance (a Class) gets a new variable
— I.e., in addition to subclass, superclasses, methodDict…
> Use class instance variables to represent the private
state of the class
— E.g., number of instances, superclass etc.
– Not to represent information shared by all instances!
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.41
Example: the Singleton Pattern
> A class with only one instance
— We keep the unique instance created in an instance variable
WebServer class
instanceVariableNames: 'uniqueInstance’
WebServer class>>new
self error: 'Use uniqueInstance to get the unique instance'
WebServer class>>uniqueInstance
uniqueInstance isNil
ifTrue: [uniqueInstance := self basicNew initialize].
^ uniqueInstance
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.42
Roadmap
> Metaclasses in 7 points
> Indexed Classes
> Class Instance Variables
> Class Variables
> Pool Dictionaries
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.43
Class Variable = Shared Variable
> To share information amongst all instances of a class,
use a “class variable”
— Shared and directly accessible by all the instances of the class
and subclasses
— Accessible to both instance and class methods
— Begins with an uppercase letter
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.44
Initializing class variables
> Class variables should be initialized by an initialize
method on the class side, or by lazy initialization
Magnitude subclass: #DateAndTime
instanceVariableNames: 'seconds offset jdn nanos'
classVariableNames: 'LocalTimeZone'
poolDictionaries: 'ChronologyConstants'
category: 'Kernel-Chronology’
Date class>>localTimeZone
"Answer the local time zone"
^ LocalTimeZone ifNil: [ LocalTimeZone := TimeZone default ]
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.45
ClassVariables vs. Instance Variables
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.46
Roadmap
> Metaclasses in 7 points
> Indexed Classes
> Class Instance Variables
> Class Variables
> Pool Dictionaries
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.47
Pool Dictionaries
> A Pool Dictionary is a shared variable
— Begins with a uppercase letter.
— Shared by a group of classes not linked by inheritance.
> Each class possesses its own pool dictionary
(containing pool variables).
— They are not inherited.
> Don’t use them!
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.48
Examples of Pool Dictionaries
ArrayedCollection 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....
> Hint: You can inspect any Pool
Dictionary
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.49
Smalltalk System Dictionary
> Pool Dictionaries are stored in the Smalltalk system
dictionary
Smalltalk inspect
(Smalltalk at: #TextConstants) at: #ESC $
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.50
Accessing globals
> Use message-sending instead of directly accessing pool
variables
stream nextPut: Lf "A pool variable visible to the class"
stream nextPut: Character lfvs.
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.51
What you should know!
What does is-a mean?
What is the difference between sending a message to
an object and to its class?
What are the responsibilities of a metaclass?
What is the superclass of Object class?
Where is new defined?
What is the difference between class variables and
class instance variables?
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
9.52
Can you answer these questions?
Why are there no explicit metaclasses?
When should you override new?
Why don’t metaclasses inherit from Class?
Are there any classes that don’t inherit from Object?
Is Metaclass a Class? Why or why not?
Where are the methods class and superclass defined?
When should you define an indexed class?
Are Java static variables just like class variables or class instance
variables?
Where is the SystemDictionary Smalltalk defined?
© Oscar Nierstrasz
ST — Introduction
1.53
Attribution-ShareAlike 3.0 Unported
You are free:
to Share — to copy, distribute and transmit the work
to Remix — to adapt the work
Under the following conditions:
Attribution. You must attribute the work in the manner specified by the author or
licensor (but not in any way that suggests that they endorse you or your use of the
work).
Share Alike. If you alter, transform, or build upon this work, you may distribute the
resulting work only under the same, similar or a compatible license.
For any reuse or distribution, you must make clear to others the license terms of this work.
The best way to do this is with a link to this web page.
Any of the above conditions can be waived if you get permission from the copyright holder.
Nothing in this license impairs or restricts the author's moral rights.
License
http://creativecommons.org/licenses/by-sa/3.0/

Más contenido relacionado

Destacado (20)

Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
14 - Exceptions
14 - Exceptions14 - Exceptions
14 - Exceptions
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
7 - OOP - OO Concepts
7 - OOP - OO Concepts7 - OOP - OO Concepts
7 - OOP - OO Concepts
 
Stoop 400 o-metaclassonly
Stoop 400 o-metaclassonlyStoop 400 o-metaclassonly
Stoop 400 o-metaclassonly
 
Stoop 430-design patternsintro
Stoop 430-design patternsintroStoop 430-design patternsintro
Stoop 430-design patternsintro
 
Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop 390-instruction stream
Stoop 390-instruction streamStoop 390-instruction stream
Stoop 390-instruction stream
 
Stoop 431-visitor
Stoop 431-visitorStoop 431-visitor
Stoop 431-visitor
 
01 intro
01 intro01 intro
01 intro
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop 439-decorator
Stoop 439-decoratorStoop 439-decorator
Stoop 439-decorator
 
11 - OOP - Numbers
11 - OOP - Numbers11 - OOP - Numbers
11 - OOP - Numbers
 
Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 

Similar a 09 metaclasses

The Anatomy of Analysis Tools (EVO 2008)
The Anatomy of Analysis Tools (EVO 2008)The Anatomy of Analysis Tools (EVO 2008)
The Anatomy of Analysis Tools (EVO 2008)Tudor Girba
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09Ayesha ch
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorialGhulam Abbas Khan
 
Object-Oriented Programming.pptx
Object-Oriented Programming.pptxObject-Oriented Programming.pptx
Object-Oriented Programming.pptxssusereae59d
 
encapsulation and abstraction
encapsulation and abstractionencapsulation and abstraction
encapsulation and abstractionALIZAPARVIN
 
Franz Et Al. Using ASP to Simulate the Interplay of Taxonomic and Nomenclatur...
Franz Et Al. Using ASP to Simulate the Interplay of Taxonomic and Nomenclatur...Franz Et Al. Using ASP to Simulate the Interplay of Taxonomic and Nomenclatur...
Franz Et Al. Using ASP to Simulate the Interplay of Taxonomic and Nomenclatur...taxonbytes
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspectionadil raja
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.pptAshwathGupta
 

Similar a 09 metaclasses (20)

Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop 304-metaclasses
Stoop 304-metaclassesStoop 304-metaclasses
Stoop 304-metaclasses
 
04 idioms
04 idioms04 idioms
04 idioms
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Basic concept of Object Oriented Programming
Basic concept of Object Oriented Programming Basic concept of Object Oriented Programming
Basic concept of Object Oriented Programming
 
Java session2
Java session2Java session2
Java session2
 
The Anatomy of Analysis Tools (EVO 2008)
The Anatomy of Analysis Tools (EVO 2008)The Anatomy of Analysis Tools (EVO 2008)
The Anatomy of Analysis Tools (EVO 2008)
 
Week3 dq1
Week3 dq1Week3 dq1
Week3 dq1
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Object-Oriented Programming.pptx
Object-Oriented Programming.pptxObject-Oriented Programming.pptx
Object-Oriented Programming.pptx
 
encapsulation and abstraction
encapsulation and abstractionencapsulation and abstraction
encapsulation and abstraction
 
06 debugging
06 debugging06 debugging
06 debugging
 
ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
 
Franz Et Al. Using ASP to Simulate the Interplay of Taxonomic and Nomenclatur...
Franz Et Al. Using ASP to Simulate the Interplay of Taxonomic and Nomenclatur...Franz Et Al. Using ASP to Simulate the Interplay of Taxonomic and Nomenclatur...
Franz Et Al. Using ASP to Simulate the Interplay of Taxonomic and Nomenclatur...
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspection
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
 

Más de The World of Smalltalk (12)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 

Último

What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
Protein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxProtein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxvidhisharma994099
 
Vani Magazine - Quarterly Magazine of Seshadripuram Educational Trust
Vani Magazine - Quarterly Magazine of Seshadripuram Educational TrustVani Magazine - Quarterly Magazine of Seshadripuram Educational Trust
Vani Magazine - Quarterly Magazine of Seshadripuram Educational TrustSavipriya Raghavendra
 
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Dr. Asif Anas
 
EBUS5423 Data Analytics and Reporting Bl
EBUS5423 Data Analytics and Reporting BlEBUS5423 Data Analytics and Reporting Bl
EBUS5423 Data Analytics and Reporting BlDr. Bruce A. Johnson
 
Over the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxOver the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxraviapr7
 
A gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceA gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceApostolos Syropoulos
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxAditiChauhan701637
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17Celine George
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsEugene Lysak
 
How to Send Emails From Odoo 17 Using Code
How to Send Emails From Odoo 17 Using CodeHow to Send Emails From Odoo 17 Using Code
How to Send Emails From Odoo 17 Using CodeCeline George
 
Work Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashaWork Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashasashalaycock03
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 

Último (20)

What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
Protein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxProtein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptx
 
Vani Magazine - Quarterly Magazine of Seshadripuram Educational Trust
Vani Magazine - Quarterly Magazine of Seshadripuram Educational TrustVani Magazine - Quarterly Magazine of Seshadripuram Educational Trust
Vani Magazine - Quarterly Magazine of Seshadripuram Educational Trust
 
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
 
EBUS5423 Data Analytics and Reporting Bl
EBUS5423 Data Analytics and Reporting BlEBUS5423 Data Analytics and Reporting Bl
EBUS5423 Data Analytics and Reporting Bl
 
Over the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxOver the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptx
 
A gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceA gentle introduction to Artificial Intelligence
A gentle introduction to Artificial Intelligence
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptx
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George Wells
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
How to Send Emails From Odoo 17 Using Code
How to Send Emails From Odoo 17 Using CodeHow to Send Emails From Odoo 17 Using Code
How to Send Emails From Odoo 17 Using Code
 
Work Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashaWork Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sasha
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 

09 metaclasses

  • 1. 9. Understanding Classes and Metaclasses
  • 2. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify your metamodel — A fully reflective system models its own metamodel. Reify your metamodel — A fully reflective system models its own metamodel.
  • 3. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.3 Roadmap > Metaclasses in 7 points > Indexed Classes > Class Instance Variables > Class Variables > Pool Dictionaries Selected material courtesy Stéphane Ducasse
  • 4. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.4 Roadmap > Metaclasses in 7 points > Indexed Classes > Class Instance Variables > Class Variables > Pool Dictionaries
  • 5. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.5 Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass Adapted from Goldberg & Robson, Smalltalk-80 — The Language
  • 6. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.6 Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass
  • 7. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.7 1. Every object is an instance of a class Remember the Snakes and Ladders Board Game …
  • 8. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.8 Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass
  • 9. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.9 2. Every class inherits from Object > Every object is-an Object = — The class of every object ultimately inherits from Object Caveat: in Pharo, Object has a superclass called ProtoObject aSnakeSquare is-a SnakeSquare and is-a BoardSquare and is-an Object
  • 10. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.10 The Meaning of is-a > When an object receives a message, the method is looked up in the method dictionary of its class, and, if necessary, its superclasses, up to Object
  • 11. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.11 Responsibilities of Object > Object — represents the common object behavior – error-handling, halting … — all classes should inherit ultimately from Object
  • 12. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.12 Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass
  • 13. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.13 3. Every class is an instance of a metaclass > Classes are objects too! — Every class X is the unique instance of its metaclass, called X class
  • 14. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.14 Metaclasses are implicit > There are no explicit metaclasses — Metaclasses are created implicitly when classes are created — No sharing of metaclasses (unique metaclass per class)
  • 15. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.15 Metaclasses by Example BoardSquare allSubclasses SnakeSquare allSubclasses SnakeSquare allInstances SnakeSquare instVarNames SnakeSquare back: 5 SnakeSquare selectors SnakeSquare canUnderstand: #new SnakeSquare canUnderstand: #setBack: a Set(SnakeSquare FirstSquare LadderSquare) a Set() an Array(<-2[6] <-4[11] <-6[11]) #('back') <-5[nil] an IdentitySet(#setBack: #printOn: #destination) false true
  • 16. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.16 Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass
  • 17. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.17 4. The metaclass hierarchy parallels the class hierarchy
  • 18. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.18 Uniformity between Classes and Objects > Classes are objects too, so … — Everything that holds for objects holds for classes as well — Same method lookup strategy – Look up in the method dictionary of the metaclass back: is a Snake constructor method
  • 19. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.19 About the Buttons
  • 20. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.20 Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass
  • 21. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.21 5. Every metaclass inherits from Class and Behavior Every class is-a Class = —The metaclass of every class inherits from Class
  • 22. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.22 Where is new defined?
  • 23. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.23 Responsibilities of Behavior > Behavior — Minimum state necessary for objects that have instances. — Basic interface to the compiler. — State: – class hierarchy link, method dictionary, description of instances (representation and number) — Methods: – creating a method dictionary, compiling method – instance creation (new, basicNew, new:, basicNew:) – class hierarchy manipulation (superclass:, addSubclass:) – accessing (selectors, allSelectors, compiledMethodAt: ) – accessing instances and variables (allInstances, instVarNames) – accessing class hierarchy (superclass, subclasses) – testing (hasMethods, includesSelector, canUnderstand:, inheritsFrom:, isVariable)
  • 24. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.24 Responsibilities of ClassDescription > ClassDescription — adds a number of facilities to basic Behavior: – named instance variables – category organization for methods – the notion of a name (abstract) – maintenance of Change sets and logging changes – most of the mechanisms needed for fileOut — ClassDescription is an abstract class: its facilities are intended for inheritance by the two subclasses, Class and Metaclass.
  • 25. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.25 Responsibilities of Class > Class — represents the common behavior of all classes – name, compilation, method storing, instance variables … — representation for classVariable names and shared pool variables (addClassVarName:, addSharedPool:, initialize) — Class inherits from Object because Class is an Object – Class knows how to create instances, so all metaclasses should inherit ultimately from Class
  • 26. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.26 Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass
  • 27. 6. Every metaclass is an instance of Metaclass © Oscar Nierstrasz LECTURE TITLE 27
  • 28. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.28 Metaclass Responsibilities > Metaclass — Represents common metaclass Behavior – instance creation (subclassOf:) – creating initialized instances of the metaclass’s sole instance – initialization of class variables – metaclass instance protocol (name:inEnvironment:subclassOf:....) – method compilation (different semantics can be introduced) – class information (inheritance link, instance variable, ...)
  • 29. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.29 Metaclasses in 7 points 1. Every object is an instance of a class 2. Every class eventually inherits from Object 3. Every class is an instance of a metaclass 4. The metaclass hierarchy parallels the class hierarchy 5. Every metaclass inherits from Class and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass
  • 30. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.30 7. The metaclass of Metaclass is an instance of Metaclass
  • 31. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.31 Navigating the metaclass hierarchy MetaclassHierarchyTest>>testHierarchy "The class hierarchy" self assert: SnakeSquare superclass = BoardSquare. self assert: BoardSquare superclass = Object. self assert: Object superclass superclass = nil. "The parallel metaclass hierarchy" self assert: SnakeSquare class name = 'SnakeSquare class'. self assert: SnakeSquare class superclass = BoardSquare class. self assert: BoardSquare class superclass = Object class. self assert: Object class superclass superclass = Class. self assert: Class superclass = ClassDescription. self assert: ClassDescription superclass = Behavior. self assert: Behavior superclass = Object. "The Metaclass hierarchy" self assert: SnakeSquare class class = Metaclass. self assert: BoardSquare class class = Metaclass. self assert: Object class class = Metaclass. self assert: Class class class = Metaclass. self assert: ClassDescription class class = Metaclass. self assert: Behavior class class = Metaclass. self assert: Metaclass superclass = ClassDescription. "The fixpoint" self assert: Metaclass class class = Metaclass
  • 32. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.32 Roadmap > Metaclasses in 7 points > Indexed Classes > Class Instance Variables > Class Variables > Pool Dictionaries
  • 33. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.33 Two ways to represent objects > Named or indexed instance variables — Named: name of GamePlayer — Indexed: #(Jack Jill) at: 1 > 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.
  • 34. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.34 Different methods to create classes Indexed Named Instance Variables Definition Method No Yes #subclass: … Yes Yes #variableSubclass: … Yes No #variableByteSubclass: … > See the subclass creation protocol of Class > Constraints — Pointer classes defined using #subclass: support any kind of subclasses — Byte classes defined using #variableSubclass: can only have: variableSubclass: or variableByteSubclass: subclasses
  • 35. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.35 Testing methods > See testing protocols of Behavior: — #isPointers, #isBits, #isBytes, #isFixed, #isVariable — #kindOfSubclass Object allSubclasses select: [:class | class isBytes] a Set(ByteArray MwSynchronizationWrapper MwBlockMethodWrapper ExternalAddress MCMockClassH LargeNegativeInteger LargePositiveInteger ByteSymbol MwCountMethodWrapper MwTimeMethodWrapper MwMethodWrapper MwBlockHandlerMethodWrapper ByteString MwCalledMethodWrapper UUID CompiledMethod)
  • 36. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.36 Defining Indexed Classes > Example — instantiating an Array: ArrayedCollection variableSubclass: #Array instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Collections-Arrayed' Array new: 4 #(nil nil nil nil) #(1 2 3 4) class isVariable true
  • 37. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.37 Defining an Indexed Class Object variableSubclass: #IndexedObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: '' (IndexedObject new: 2) at: 1 put: 'Jack'; at: 2 put: 'Jill'; at: 1 'Jack'
  • 38. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.38 Indexed Classes / Instance Variables > An indexed variable is implicitly added to the list of instance variables — Only one indexed instance variable per class — Access with at: and at:put: – NB: answers the value, not the receiver > Subclasses should also be indexed
  • 39. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.39 Roadmap > Metaclasses in 7 points > Indexed Classes > Class Instance Variables > Class Variables > Pool Dictionaries
  • 40. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.40 Class Instance Variables > Class are objects too — Instances of their metaclass – Methods looked up in the method dictionary of their metaclass — Can also define instance variables > When a metaclass defines a new instance variable, then its instance (a Class) gets a new variable — I.e., in addition to subclass, superclasses, methodDict… > Use class instance variables to represent the private state of the class — E.g., number of instances, superclass etc. – Not to represent information shared by all instances!
  • 41. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.41 Example: the Singleton Pattern > A class with only one instance — We keep the unique instance created in an instance variable WebServer class instanceVariableNames: 'uniqueInstance’ WebServer class>>new self error: 'Use uniqueInstance to get the unique instance' WebServer class>>uniqueInstance uniqueInstance isNil ifTrue: [uniqueInstance := self basicNew initialize]. ^ uniqueInstance
  • 42. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.42 Roadmap > Metaclasses in 7 points > Indexed Classes > Class Instance Variables > Class Variables > Pool Dictionaries
  • 43. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.43 Class Variable = Shared Variable > To share information amongst all instances of a class, use a “class variable” — Shared and directly accessible by all the instances of the class and subclasses — Accessible to both instance and class methods — Begins with an uppercase letter
  • 44. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.44 Initializing class variables > Class variables should be initialized by an initialize method on the class side, or by lazy initialization Magnitude subclass: #DateAndTime instanceVariableNames: 'seconds offset jdn nanos' classVariableNames: 'LocalTimeZone' poolDictionaries: 'ChronologyConstants' category: 'Kernel-Chronology’ Date class>>localTimeZone "Answer the local time zone" ^ LocalTimeZone ifNil: [ LocalTimeZone := TimeZone default ]
  • 45. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.45 ClassVariables vs. Instance Variables
  • 46. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.46 Roadmap > Metaclasses in 7 points > Indexed Classes > Class Instance Variables > Class Variables > Pool Dictionaries
  • 47. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.47 Pool Dictionaries > A Pool Dictionary is a shared variable — Begins with a uppercase letter. — Shared by a group of classes not linked by inheritance. > Each class possesses its own pool dictionary (containing pool variables). — They are not inherited. > Don’t use them!
  • 48. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.48 Examples of Pool Dictionaries ArrayedCollection 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.... > Hint: You can inspect any Pool Dictionary
  • 49. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.49 Smalltalk System Dictionary > Pool Dictionaries are stored in the Smalltalk system dictionary Smalltalk inspect (Smalltalk at: #TextConstants) at: #ESC $
  • 50. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.50 Accessing globals > Use message-sending instead of directly accessing pool variables stream nextPut: Lf "A pool variable visible to the class" stream nextPut: Character lfvs.
  • 51. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.51 What you should know! What does is-a mean? What is the difference between sending a message to an object and to its class? What are the responsibilities of a metaclass? What is the superclass of Object class? Where is new defined? What is the difference between class variables and class instance variables?
  • 52. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 9.52 Can you answer these questions? Why are there no explicit metaclasses? When should you override new? Why don’t metaclasses inherit from Class? Are there any classes that don’t inherit from Object? Is Metaclass a Class? Why or why not? Where are the methods class and superclass defined? When should you define an indexed class? Are Java static variables just like class variables or class instance variables? Where is the SystemDictionary Smalltalk defined?
  • 53. © Oscar Nierstrasz ST — Introduction 1.53 Attribution-ShareAlike 3.0 Unported You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. License http://creativecommons.org/licenses/by-sa/3.0/

Notas del editor

  1. NB: canUnderstand: tells you which messages *instances* can understand. SnakeSquare class canUnderstand: #new -&amp;gt; true