UIUC CS 497 - A Framework for Language Implementations

Unformatted text preview:

1Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 725ReflectionA Framework for LanguageImplementationsObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 726ReflectionThe ability of a program to examine andcontrol its own implementation.Computational reflection is good for• programming environments• extending a language• concurrent programming2Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 727Disadvantages of ReflectionReflection is usually hard to understand.Meta is good. “Meta” is bad.Reflection can make language hard toimplement.Reflection can make language hard toanalyze.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 728Reflection in SmalltalkObjectClassesMethod lookupBlocks, Contexts3Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 729Object• basicSize gives number of instancevariables of any object• instVarAt: and instVarAt:put: lets theinspector access any instancevariable• used by inspector, storeOn:, copyObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 730CopyingshallowCopy| newObject |newObject := self class basicNew.1 to: self basicSize do:[ :i | newObjectinstVarAt: iput: (self instVarAt: i) ].^newObject4Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 731Classes as objectsPoint of reflectionClasses are implementationMakes system more dynamicObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 732What is a Class?1) A factory for instances.• new2) The category to which an objectbelongs.• name• superclass5Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 733Using classes as categoriesByteCharacterEncoder= anEncoder^self class == anEncoder classand: [self decoder = anEncoderdecoder]Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 734What is a Class?3) The definition (implementation) of anobject.• compiledMethodAt:• instVarNames• compile:classified:notifying:4) A set of instances.• allInstances6Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 735Classes as ImplementationObjectEmployeeRalph Johnsonclass, printString,=, shallowCopyname, transactionsMethodDictionaryObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 736"Class" HierarchyBehavior (’superclass’ ’methodDict’’format’ ’subclasses’ )ClassDescription (’instanceVariables’’organization’ )Class (’name’ ’classPool’’sharedPools’ )Metaclass (’thisClass’ )7Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 737Implementing VariablesInstance variablesslots of an objectGlobal and class variablesAssociations - key/value pairsClass variables in class pool dictionaryGlobal variables in “Smalltalk”Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 738Class VariablesSingle variable accessible by all instancesof class and subclasses.Implemented like a global variable.Subclasses share the same location.Usually used for constants. Famouscounterexample - the cut buffer.8Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 739Class Instance VariablesInstance variable of object representingclass.Not accessible by instances.Each subclass has its own copy of thevariable.Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 740Class VariablesApplicationModel subclass: #BrowserinstanceVariableNames: ’organizationcategory className ...’classVariableNames: ’CategoryMenuClassMenu ...’poolDictionaries: ’’category: ’Tools-Programming’9Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 741Class Instance VariablesBrowser classinstanceVariableNames: ’’Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 742The Metaclass MysteryAssumptionEvery object has a class.Everything is an object.ConclusionA class is an object.A class has a class.10Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 743The Metaclass MysteryMysteryWhat is the class of a class of a class... ?Other factsObject has no superclassObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 744MetaclassesThe class of a class is a metaclass.Name of "SmallInteger class" is"SmallInteger class".Metaclasses form a hierarchy that mirrorsthe regular class hierarchy. This allowssubclasses to inherit class methodsfrom superclasses.11Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 745MetaclassesObjectNumberObject class29SmallIntegerNumber classSmallInteger classInteger class IntegerObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 746More Metaclass MysteryIf X is a Y, then X is an instance of Y orof a subclass of Y.Thus, since Object is a class, Object is aninstance of Class or of a subclass ofClass.Object is an instance of Object class, thusObject class is a subclass of Class.12Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 747MetaclassesBehaviorBehavior classObjectObject classClass classClassObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 748Minus Metaclasses?Metaclasses are not necessary.Every class could be an instance ofClass. Each class would then have thesame behavior, so no special instancecreation methods.ObjectClass13Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 749Metaclass MadnessEvery metaclass is an instance ofMetaclass.Classdesc.Classdesc. classMetaclass classMetaclassObject-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 750Creating New ClassesClass creation template in the browser isjust a regular Smalltalk message that issent to a class.Classes either have no indexible part, aneindexible part that contains uninterpretedbytes, or an indexible part that containspointers to objects.14Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 751Creating New ClassesMethods defined in Class for creatingsubclasses:subclass: t instanceVariableNames: fclassVariableNames: dpoolDictionaries: s category: catvariableByteSubclass: t ...variableSubclass: t ...Object-oriented Programming and Design - Copyright 1998 by Ralph E. Johnson 752Builder PatternObject for creating complex object.Builder supports set of operations


View Full Document

UIUC CS 497 - A Framework for Language Implementations

Download A Framework for Language Implementations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A Framework for Language Implementations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Framework for Language Implementations 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?