+ All Categories
Home > Technology > Stoop sed-sharing ornot

Stoop sed-sharing ornot

Date post: 01-Dec-2014
Category:
Upload: the-world-of-smalltalk
View: 379 times
Download: 0 times
Share this document with a friend
Description:
 
7
Stéphane Ducasse 1 Stéphane Ducasse [email protected] http://stephane.ducasse.fre e.fr/ Elements of Design - Sharing or not
Transcript
Page 1: Stoop sed-sharing ornot

Stéphane Ducasse 1

Stéphane [email protected]://stephane.ducasse.free.fr/

Elements of Design- Sharing or not

Page 2: Stoop sed-sharing ornot

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: #'.')

Page 3: Stoop sed-sharing ornot

S.Ducasse 3

A Case Study: The Scanner class

Class Definition

Object subclass: #ScannerinstanceVariableNames: 'source mark

prevEnd hereChar token tokenType saveComments currentComment buffer typeTable '

classVariableNames: 'TypeTable 'poolDictionaries: ''category: 'System-Compiler-Public Access'

Page 4: Stoop sed-sharing ornot

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

Page 5: Stoop sed-sharing ornot

S.Ducasse 5

Clever Sharing

Page 6: Stoop sed-sharing ornot

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

Page 7: Stoop sed-sharing ornot

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>>initScannerbuffer := WriteStream on: (String new: 40).saveComments := true.typeTable := TypeTable

A subclass just has to specialize initScanner without copying the initialization of the table

MyScanner>>initScannersuper initScannertypeTable := typeTable copy.typeTable at: $) asInteger put: #xDefault.


Recommended