Stéphane Ducasse«ChapterNr».1 Basic Objects, Conditionals and Loops Booleans Basic Loops Overview of the Collection hierarchy— more than 80 classes: (Bag,

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Generics, Lists, Interfaces
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Stéphane Ducasse«ChapterNr».1 The Design in Question The Basic Idea behind Frameworks Subclassing vs SubTyping Design Heuristics Design Symptoms.
Stéphane Ducasse 1 Design Points - Subclassing vs Subtyping Stéphane Ducasse
11. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 A bit of Smalltalk Overview  Some history  Smalltalk syntax & object model  The.
Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
Principles of Object-Oriented Software Development The language Smalltalk.
Stéphane Ducasse«ChapterNr».1 Abstract Classes Should not be instantiated (abstract in Java) But can defined complete methods Defines a protocol common.
Stéphane Ducasse«ChapterNr».1 Arthur Riel Design Heuristics from Object-Oriented Design Heuristics by Arthur Riel Read the Heuristics… –Find reasons why.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
6. Debugging. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 It can be easier to talk to objects than to read classes — The system is alive.
Stéphane Ducasse 1 The Taste of Smalltalk.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Streams  Allows the traversal of a collection  Associated with a collection —If the collection is a Smalltalk collection: InternalStream —If the collection.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
Some Basic Points on Classes
Stéphane Ducasse 1 Syntax and Messages.
Stéphane Ducasse 1 Objects to the Roots: Learning from beauty.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Squeak Collections The Squeak collection hierarchy. Some collection operators. Working with collections. For-loops.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
3. Standard Classes. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify everything — by reifying its entire implementation model, Smalltalk.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
S.Ducasse Stéphane Ducasse 1 Smalltalk Coding Idioms.
3E-1 Collections in Squeak What are they? Kinds of collections. Basic Operations. Usage of Inheritance in the Collections Library. Roman numbers example.
CSE 131 Computer Science 1 Module 1: (basics of Java)
111 © 2002, Cisco Systems, Inc. All rights reserved.
Object-oriented Programming and Design 1 Polymorphism Poly => many Morph => shape Variables take on many shapes, or many classes of objects.
Stéphane Ducasse 1 Elements of Design - Simple Smells.
Chapter 18 Java Collections Framework
Centralized vs. Decentralized Interpreter Pattern Visitor Pattern.
Built-in Data Structures in Python An Introduction.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
Object-oriented programming and design 1 Object Identity = vs. == copying objects Value Object ValueWithHistory.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Smalltalk in a.NET World How to write a Smalltalk compiler without writing a VM John Brant
Stéphane Ducasse 1 Singleton.
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
3C-1 Identifiers Variables Literal Objects  Numbers  Characters  Strings Control Structure  Blocks  Conditionals  Loops Messages Defining New Classes.
1 Advanced Object Oriented Systems (CM0318) Lecture 8 (Last updated 15th February 2002)
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
Stéphane Ducasse 1 Elements of Design - Unit of Reuse.
Collections Dwight Deugo Nesa Matic
Stéphane Ducasse 1 Abstract Classes.
Patterns Protect from Change Rule: if something is going to change, make it an object. Strategy: make algorithm an object so it can change State: make.
S.Ducasse 1 Instance Initialization How to ensure that an instance is well initialized? Automatic initialize Lazy initialization Proposing the right interface.
Feedback from Assignment 1
Smalltalk – a Pure (and first) OOPL
Chapter 2 Squeak: Object-oriented design with multimedia applications
Python Primer 1: Types and Operators
Copyright 2000, Georgia Tech
Introduction to Smalltalk
CSE 3302 Programming Languages
Smalltalk on a Dime.
Presentation transcript:

Stéphane Ducasse«ChapterNr».1 Basic Objects, Conditionals and Loops Booleans Basic Loops Overview of the Collection hierarchy— more than 80 classes: (Bag, Array, OrderedCollection, SortedCollection, Set, Dictionary...) Loops and Iteration abstractions Common object behavior

Stéphane Ducasse«ChapterNr».2 Booleans

Stéphane Ducasse«ChapterNr».3 Boolean Objects false and true are objects described by classes Boolean, True and False Uniform, but optimized and inlined (macro expansion at compile time) Logical Comparisons &, |, xor:, not aBooleanExpr comparison anotherBooleanExpr (1 isZero) & false

Stéphane Ducasse«ChapterNr».4 Boolean Hierarchy Please open your browser and analyse it How to implement in OO true and false without conditional? Booleean not ifTrue: False not ifTrue: True not ifTrue:

Stéphane Ducasse«ChapterNr».5 Boolean Lazy Logical Operators Lazy Logical operators aBooleanExpr and: andBlock andBlock will only be valued if aBooleanExpression is true aBooleanExpression or: orBlock orBlock will only be valued if aBooleanExpression is false false and: [1 error: 'crazy'] PrIt-> false and not an error

Stéphane Ducasse«ChapterNr».6 Conditional: Messages to Boolean aBoolean ifTrue: aTrueBlock ifFalse: aFalseBlock aBoolean ifFalse: aFalseBlock ifTrue: aTrueBlock aBoolean ifTrue: aTrueBlock aBoolean ifFalse: aFalseBlock Hint: Take care — true is the boolean value and True is the class of true, its unique instance!

Stéphane Ducasse«ChapterNr».7 Why Block Use in Conditional Why do conditional expressions use blocks? Because, when a message is sent, the receiver and the arguments of the message are evaluated. Blocks are necessary to avoid evaluating both branches.

Stéphane Ducasse«ChapterNr».8 Some Basic Loops aBlockTest whileTrue aBlockTest whileFalse aBlockTest whileTrue: aBlockBody aBlockTest whileFalse: aBlockBody anInteger timesRepeat: aBlockBody [x<y] whileTrue: [x := x + 3] 10 timesRepeat: [ Transcript show: 'hello'; cr]

Stéphane Ducasse«ChapterNr».9 For the Curious... BlockClosure>>whileTrue: aBlock ^ self value ifTrue: [aBlock value. self whileTrue: aBlock] BlockClosure>>whileTrue ^ [self value] whileTrue:[]

Stéphane Ducasse«ChapterNr».10 For the Curious… Integer>>timesRepeat: aBlock "Evaluate the argument, aBlock, the number of times represented by the receiver." | count | count := 1. [count <= self] whileTrue: [aBlock value. count := count + 1]

Stéphane Ducasse«ChapterNr».11 Collections

Stéphane Ducasse«ChapterNr».12 Collections Some criteria to identify them –Access: indexed, sequential or key-based. –Size: fixed or dynamic. –Element type: any or well-defined type. –Order: defined, defineable or none. –Duplicates: possible or not

Stéphane Ducasse«ChapterNr».13 Essential Collection Sequenceable ordered ArrayedCollectionfixed size + key = integer Arrayany kind of elements CharacterArrayelements = character String IntegerArray Intervalarithmetique progression LinkedListdynamic chaining of the element OrderedCollectionsize dynamic + arrival order SortedCollection explicit order Bagpossible duplicate + no order Setno duplicate + no order IdentitySetidentification based on identity Dictionary element = associations + key based IdentityDictionary key based on identity

Stéphane Ducasse«ChapterNr».14 Essential Collections: Another View

Stéphane Ducasse«ChapterNr».15 Some Collection Methods Will be defined, redefined, optimized or forbidden in the subclasses Accessing: #size, #capacity, #at: anInteger, #at: anInteger put: anElement Testing: #isEmpty, #includes: anElement, #contains: aBlock, occurencesOf: anElement Adding: #add: anElement, #addAll: aCollection Removing: #remove: anElement, #remove:anElement ifAbsent: aBlock, #removeAll: aCollection Enumerating (See generic enumerating): #do: aBlock, #collect: aBlock, #select: aBlock, #reject: aBlock, #detect:, #detect: aBlock ifNone: aNoneBlock, #inject: avalue into: aBinaryBlock Converting: #asBag, #asSet, #asOrderedCollection, #asSortedCollection, #asArray, #asSortedCollection: aBlock Creation: #with: anElement, #with:with:, #with:with:with:, #with:with:with:with:, #with:All: aCollection

Stéphane Ducasse«ChapterNr».16 Sequenceable Specific (Array) |arr| arr := #(calvin hates suzie). arr at: 2 put: #loves. arr PrIt-> #(#calvin #loves #suzie) Accessing: #first, #last, #atAllPut: anElement, #atAll: anIndexCollection: put: anElement Searching (*: + ifAbsent:): #indexOf: anElement, #indexOf: anElement ifAbsent: aBlock Changing: #replaceAll: anElement with: anotherElement Copying: #copyFrom: first to: last, copyWith: anElement, copyWithout: anElement

Stéphane Ducasse«ChapterNr».17 KeyedCollection Specific (Dictionary) |dict| dict := Dictionary new. dict at: 'toto' put: 3. dict at: 'titi' ifAbsent: [4]. -> 4 dict at: 'titi' put: 5. dict removeKey: 'toto'. dict keys -> Set ('titi') Accessing: #at: aKey, #at: aKey ifAbsent: aBlock, #at: aKey ifAbsentPut: aBlock, #at: aKey put: aValue, #keys, #values, #associations Removing: #removeKey: aKey, #removeKey: aKey ifAbsent: aBlock Testing: #includeKey: aKey Enumerating: #keysAndValuesDo: aBlock, #associationsDo: aBlock, #keysDo: aBlock

Stéphane Ducasse«ChapterNr».18 Choose your Camp! To get all the absolute values of numbers you could write: absolute: aCol |result| result := aCol species new: aCol size. 1 to: aCollection size do: [ :each | result at: each put: (aCol at: each) abs]. ^ result

Stéphane Ducasse«ChapterNr».19 Choose your Camp You could also write: absolute: aCollection ^ aCollection collect: [:each| each abs] Really important: Contrary to the first solution, the second solution works well for indexable collections and also for sets.

Stéphane Ducasse«ChapterNr».20 Iteration Abstraction: do:/collect: aCollection do: aOneParameterBlock aCollection collect: aOneParameterBlock aCollection with: anotherCollection do: aBinaryBlock #( ) do: [:i | Transcript show: i printString ; cr ] #( ) collect: [:i | i odd ] PrIt-> #(true false true false) #(1 2 3) with: #( ) do: [:x :y| Transcript show: (y ** x) printString ; cr ]

Stéphane Ducasse«ChapterNr».21 select:/reject:/detect: aCollection select: aPredicateBlock aCollection reject: aPredicateBlock aCollection detect: aOneParameterPredicateBlock aCollection detect: aOneParameterPredicateBlock ifNone: aNoneBlock #( ) select: [:i|i odd] -> #(15 19) #( ) reject: [:i|i odd] -> #(10 68) #( ) detect: [:i|i odd] PrIt-> 19 #( ) detect: [:i|i odd] ifNone:[1] PrIt-> 1

Stéphane Ducasse«ChapterNr».22 inject:into: aCollection inject: aStartValue into: aBinaryBlock |acc| acc := 0. #( ) do: [:element | acc := acc + element]. acc -> 15 Is equivalent to #( ) inject: 0 into: [:acc :element| acc + element] -> 15 Do not use it if the resulting code is not crystal clear!

Stéphane Ducasse«ChapterNr».23 Other Collections Important Methods aCollection includes: anElement aCollection size aCollection isEmpty aCollection contains: aBooleanBlock #( ) includes: 4 -> true #( ) size -> 5 #( ) isEmpty -> false #( ) contains: [:each | each isOdd] -> true

Stéphane Ducasse«ChapterNr».24 Common Shared Behavior

Stéphane Ducasse«ChapterNr».25 Common Shared Behavior Object is the root of the inheritance tree Defines the common and minimal behavior for all the objects in the system. Comparison of objects: #==, #~~, #=, #=~, #isNil, #notNil

Stéphane Ducasse«ChapterNr».26 Identity vs. Equality = anObject returns true if the structures are equivalent (the same hash number) (Array with: 1 with: 2) = (Array with:1 with:2) PrIt-> true == anObject returns true if the receiver and the argument point to the same object. #== should never be overridden. On Object>>= anObject ^ self == anObject ~= is not = ~~ is not == (Array with: 1 with: 2 ) == (Array with: 1 with:2) PrIt-> false (Array with: 1 with: 2 ) = (Array with: 1 with:2) PrIt-> true Take care when redefining #=. One should override #hash too!

Stéphane Ducasse«ChapterNr».27 Common Behavior: Printing Print and store objects: #printString, #printOn: aStream. #printString calls printOn: aStream #( ) printString -> '#( )' Date today printString -> 'October 5, 1997'

Stéphane Ducasse«ChapterNr».28 Storing #storeString, #storeOn: aStream. #storeString calls storeOn: aStream Date today storeString -> '(Date readFromString: ''10/5/1997'')' OrderedCollection new add: 4 ; add: 3 ; storeString -> '((OrderedCollection new) add: 4; add: 3; yourself)’ You need the compiler, so for a deployment image this is not convenient

Stéphane Ducasse«ChapterNr».29 readFromString: recreating Objects Create instances from stored objects: class methods readFrom: aStream, readFromString: aString Object readFromString: '((OrderedCollection new) add: 4; yourself)' -> OrderedCollection (4)

Stéphane Ducasse«ChapterNr».30 Notifying the programmer #error: aString, #doesNotUnderstand: aMessage, #halt, #halt: aString, To invoke the debugger Input defaultState ifTrue:[self halt] #shouldNotImplement Bad sign: subclassing #subclassResponsibility Abstract method

Stéphane Ducasse«ChapterNr».31 Copying Copying of objects: #shallowCopy, #copy #shallowCopy : the copy shares instance variables with the receiver. default implementation of #copy is #shallowCopy

Stéphane Ducasse«ChapterNr».32 Copying in VW Object>>copy ^ self shallowCopy postCopy Object>>postCopy postCopy is a hook method copy is a template method