Stoop sed-sharing ornot

420 views

Published on

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
420
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Stoop sed-sharing ornot

  1. 1. Stéphane Ducasse 1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Elements of Design - Sharing or not - Sharing or not
  2. 2. S.Ducasse 2 Case Study: Scanner Scanner new scanTokens: 'identifier keyword: 8r31 ''string'' embedded.period key:word: . ' > #(#identifier #keyword: 25 'string' 'embedded.period' #key:word: #'.')
  3. 3. S.Ducasse 3 A Case Study:The Scanner class Class Definition Object subclass: #Scanner instanceVariableNames: 'source mark prevEnd hereChar token tokenType saveComments currentComment buffer typeTable ' classVariableNames: 'TypeTable ' poolDictionaries: '' category: 'System-Compiler-Public Access'
  4. 4. S.Ducasse 4 Scanner enigma Why having an instance variable and a classVariable denoting the same object (the scanner table)? TypeTable is used to initialize once the table. typeTable is used by every instance and each instance can customize the table (copying). All methods only accessed the instance variable
  5. 5. S.Ducasse 5 Clever Sharing
  6. 6. S.Ducasse 6 A Case Study: Scanner (II) Scanner class>>initialize "Scanner initialize" | newTable | newTable := ScannerTable new: 255 withAll: #xDefault. "default" newTable atAllSeparatorsPut: #xDelimiter. newTable atAllDigitsPut: #xDigit. newTable atAllLettersPut: #xLetter. '!%&*+,-/<=>?@~' do: [:bin | newTable at: bin asInteger put: #xBinary]. "Other multi-character tokens" newTable at: $" asInteger put: #xDoubleQuote. "Single-character tokens" newTable at: $( asInteger put: #leftParenthesis. newTable at: $^ asInteger put: #upArrow. "spacing circumflex, arrow" newTable at: $| asInteger put: #verticalBar. TypeTable := newTable 6
  7. 7. S.Ducasse 7 A Case Study: Scanner (III) Instances only access the type table via the instance variable that points to the table that has been initialized once. Scanner class>> new ^super new initScanner Scanner>>initScanner buffer := WriteStream on: (String new: 40). saveComments := true. typeTable :=TypeTable A subclass just has to specialize initScanner without copying the initialization of the table MyScanner>>initScanner super initScanner typeTable := typeTable copy. typeTable at: $) asInteger put: #xDefault.

×