Metaclasses 4 ◆ Dynamic Binding Implies That Classes Must Have Some ◆ Meta-: Beyond; Transcending; More Comprehensive; E.G., As Memory Representation

Metaclasses 4 ◆ Dynamic Binding Implies That Classes Must Have Some ◆ Meta-: Beyond; Transcending; More Comprehensive; E.G., As Memory Representation

® 1 ® Class Representation in Memory? 2 Case Study: Meta Classes ◆ A class is an abstract entity, so why should it be represented in the runtime environment? ◆ Answer: Dynamic Binding! ◆ Class representation in memory ● The actual method bound to a message is determined at run time based on the type (class) of an object. ◆ Class variables and class methods ● There must be a link between the object and its ◆ Meta Classes methods. ● Since these links are the same for all objects of the ◆ 3 Level System same class, it makes sense to share the representation. ◆ ◆ 4 Level System Schematic diagram, common to all OO languages: Method ◆ 5 Level System 1 Method ◆ 1 Level System Class Rep 2 ◆ Msg Object Methods Infinite Levels System Table Methodn oop Õ Ö oop Õ Ö ® A Class as an Object? 3 ® Metaclasses 4 ◆ Dynamic binding implies that classes must have some ◆ Meta-: Beyond; transcending; more comprehensive; e.g., as memory representation. in metalinguistics. ◆ Why not as an object? ◆ Class: generates instances. ◆ Answer: Let's go for it! ◆ Metaclass: a class whose instances are classes. ● Each object o has a pointer to its class object c which is the ◆ Terminal Instance: cannot be instantiated further. object representing the class to which o belongs. Objects Class Classes Terminal Instances Novel Play Instantiable objects Non instantiable objects Macbeth Othello Metaclasses Instances are classes 1984 War & Peace As you like Main Text Book Reference: G.Masini et al. Oliver Twist Object-Oriented Languages, APIC series, No.34 oop Õ Ö oop Õ Ö ® 5 ® 6 Taxonomy of Metaclass Systems The 3 Level System ◆ 1-Level System: Objects only Object ● Each object describes itself INSTANCE _OF ● No need for classes: objects are ìinstantiatedî or ìinheritedî from SUBCLASS _OF other objects Class Example: Self VARIABLES instance_of ,167$1&(B2) ◆ 2-Level System: Objects, Classes METHODS ... 68%&/$66B2) subclass_of ● Objects: described by classes 9$5,$%/(6 variables ● Classes: need no description--do not exist in run-time. Not first- methods class objects: print 0(7+2'6 Not instances of classes ... Novel Not created by constructors new Cannot receive messages INSTANCE _OF add sub- Examples: C++, Eiffel Oliver Twist SUBCLASS _OF set_author author class INSTANCE _OF ◆ 3-Level System: Objects, Classes, Metaclass VARIABLES get_author year ... AUTHOR : D ICKENS ● Objects: described by classes METHODS set_year ... Other instance ● Classes: described by the metaclass get_year variables ● The Metaclass: describes itself ... Examples: Smalltalk-76, Little-Smalltalk oop Õ Ö oop Õ Ö ® 7 ® 8 One Metaclass Structure The 1 Level System ◆ Simple and elegant model: courtesy of Smalltalk-76, the Object Traits first language to introduce the metaclass concept. method for P RINT printing ● Later adopted by Little-Smalltalk objects ◆ The &ODVV metaclass: Novel Traits ● The only metaclass in the system PARENETPARENT method for ● STORE storing in Instance of itself library Avoids the infinite regress of the instantiation relationship Oliver Twist ● Inherits from 2EMHFW PARENETPARENT Oliver ● Holds behaviour common to all class objects: TITLE Twist How to add methods Novel Prototype 1984 TITLE: How to instantiate a class PARENETPARENT PARENETPARENT AUTHOR D ICKENS ● Holds structure common to all class objects: TITLE ??? TITLE 1984 AUTHOR : Super class TITLE: TITLE: Instance variables AUTHOR ??? AUTHOR Orwell List of methods AUTHOR : AUTHOR : ● Uniform behaviour and structure of all classes: Different classes cannot have different QHZ methods Clone QHZ &ODVV QHZ The defined in class calls defined in the objectís Clone class oop Õ Ö oop Õ Ö ® Singleton Classes 9 ® Class Variables and Class Methods 10 ◆ In a 1-level system, each object describes itself. ◆ If a class is an object, then it makes sense to ask ● A bit awkward for classes with many similar objects what are the variables and methods of that object. ● Very elegant for Singleton Classes ◆ Simple answer (Objective-C approach): ◆ Examples: Classes with only one instance ● Class Methods: ● True: true Constructor! Send a message to the class, requesting it ● UndefinedObject: nil to create a new instance. ● ● Smalltalk: smalltalk Class Variables: ● TApplication: application The mould: ● Simulation: ï Data structure defining the structure of instances. ï Data structure determining the behaviour of instances. Clock Other shared variables. Simulator ◆ The more sophisticated answer: ◆ In class based OOP, double check that the appropriate abstraction was used. ● Smalltalk-80 (a 5 level system): ● Use a module? Class specific variables ● Use a global function/procedure? Class specific methods ● C++: Static data and function members. oop Õ Ö oop Õ Ö ® 11 ® 12 A 4 Level System Properties of the 4 Level System Object ◆ Each object: is an instance of a class INSTANCE_OF ◆ Each class: is an instance of a metaclass SUBCLASS_OF Meta Class ● May have its own singleton metaclass VARIABLES instance_of INSTANCE_OF ● May share a metaclass with other classes METHODS Meta Novel subclass_of ... ● SUBCLASS_OF May use the default metaclass Meta Object variables INSTANCE_OF VARIABLES ◆ Each metaclass: is an instance of a meta-metaclass. methods SUBCLASS_OF METHODS ◆ One meta-metaclass: VARIABLES print ● Novel new Instance of itself METHODS ... ● Inherits from Object (or Class) INSTANCE_OF ... SUBCLASS_OF ◆ Examples: (with minor variations) Oliver Twist VARIABLES ● LOOPS: Lisp Object and data Oriented Programming author n_novels INSTANCE_OF System METHODS add_novel set_author year ... ● ObjVlisp: OBJect Virtual extension of LISP AUTHOR: D ICKENS N_NOVELS: 1 get_author del_novel ... A model more than a real language Other instance Other class ... variables variables ... oop Õ Ö oop Õ Ö ® 13 ® 14 Infinite Object-Class Regression ... A 5 Level System Three Kinds of Entities: ◆ 3-KRS: an infinite number of levels system ● Objects Metaclass ● Objects: instances of ìmetaobjectsî (aka classes). ● Classes: each class X has an object X describing it. x class ● Classes: instances of metaclasses. Integer Object ● Metaclasses: instances of meta-metaclasses. Class: the class of all class objects ● Meta-metaclasses: instances of meta-meta- Metaclass: the class of all metaclass objects metaclasses. x Metaclass ● Metaclasses: the object X describing class X ● ... is an instance of a singleton metaclass: ìX class” describing it. ◆ For each object X, there is a meta-object X Integer class: the class of ìIntegerî ● x Integer Object Lazy creation of meta-objects enables the system Class class: the class of ìClassî to work in a finite world. class class Metaclass Class: the class of ìMetaclassî x The metaclass ìNovel classî is also the result of evaluating the Smalltalk Integer Object expression: x 1RYHOFODVV 12 13 oop Õ Ö oop Õ Ö ® 15 Inheritance in the 5 Level System A metaclass object ìX classî: Object ● is an instance of ìMetaclassî ● inherits from ìClassî Integer Class: Metaclass Class ● an abstract class ● parent of all ìX classî Class objects Hierarchy Metaclasses hierarchy: ● Instance Of X inherits from Y Object ìX classî inherits class from ìY classî Integer Metaclass Class class class class Meta Class Hierarchy oop Õ Ö.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us