Docsity

Smalltalk: OOP, Encapsulation, Simulation, and Object Creation, Study notes of Computer Science

Partial preview of the text

Download Smalltalk: OOP, Encapsulation, Simulation, and Object Creation and more Study notes Computer Science in PDF only on Docsity!

Introduction to

Object-oriented Programming

in Smalltalk

15 of 22

CSE 420/520: Object-Oriented Programming

Objects are responsible for their own

actions!

  • In procedural programming, I write code

that reaches into the internals of some data

structure and twiddles with the bits

0 0 1 1 0 0 1 1

process

reads/

writes

process

reads/

writes

process

reads/

writes

string

16 of 22

CSE 420/520: Object-Oriented Programming

  • In O-O programming, I politely request

some other object to perform some work on

my behalf, and it politely answers me

0 0 1 1 0 0 1 1

concat

print

copy

String

Encapsulation

boundary

16 of 22

CSE 420/520: Object-Oriented Programming

  • In O-O programming, I politely request

some other object to perform some work on

my behalf, and it politely answers me

0 0 1 1 0 0 1 1

concat

print

copy

String

Encapsulation

boundary

copy

16 of 22

CSE 420/520: Object-Oriented Programming

  • In O-O programming, I politely request

some other object to perform some work on

my behalf, and it politely answers me

0 0 1 1 0 0 1 1

concat

print

copy

String

Encapsulation

boundary

aNewString

16 of 22

CSE 420/520: Object-Oriented Programming

  • In O-O programming, I politely request

some other object to perform some work on

my behalf, and it politely answers me

0 0 1 1 0 0 1 1

concat

print

copy

String

Encapsulation

boundary

18 of 22

CSE 420/520: Object-Oriented Programming

Programming Philosophy

  • Object-Oriented programming is

programming by simulation.

  • The algorithm is less important than the structure of

the solution.

  • When requirements change:
    • If the structure represented the structure of some

‘reality’, then the new requirements will be consistent

in that reality.

  • Object-oriented design is the search for this

structure: uncover the structure rather than construct

in isolation.

19 of 22

CSE 420/520: Object-Oriented Programming

Shopping vs. Building

  • Constructing an Object-oriented

application is a process of shopping for the

components that one needs

  • occasionally we add a new item to the shelf.
  • usually we can find a component that almost fits.
  • The openness of an OO language allows

the programmer to change the component

that almost fits into one that is a good fit.

  • works only if we have a rich set of components on the

shelf, and if they are open to change.

22 of 22

CSE 420/520: Object-Oriented Programming

Smalltalk

  • Squeak is an open-source version of

Smalltalk.

  • Smalltalk is still the best example of a Pure O-O

language

  • The Squeak workspace is a place in which you can

create and interact with objects.

  • Large and active community of contributors
    • Runs “bit identical” on just about any platform,

including many PDAs

!

"#$!%&'$()!+,-./+0$+*

!

-!604*2)7!5#!)80)193).5!:95;!#<=)25>

!

?

!

@#1A)5!0049259#.>B!$94)>B!2#3094)1>B!C5DDD

? @#2(>!#.!#<=)25>

!

"#$%%&$%'!"()&$*

!

6

!

7#!89.5:!$#1!2488)8;!02+<)8;! etc.

!

=

!

488!21)5?#.!.@!3)5A#@!25)<#1?B5?#.!1)!@#.)!

imperatively

!

!(8?.<!5A)!@)C)4#03).5!5##

!

6

!

DA)!3)5A#@!89.5*:!?8!8?304);!E(5!@?$$)1).

!

+,!$"&-.)/!

!

F-.

!

G)1!GA)5A)1!5A)!1)2)?C)1!8#158!$5)1!#1!)'(4!5#!

&51?.<H!DA)!2#445?#.!#1@)1!?8!8?304)!82??!IG?5A!28)!

@?$$)1).2)8JHF

K!I8)4$!2#301),!8)4$!G?5A,!&51?.<!2#44*5)@,!-82??/1@)1J!LM!N

!

"#$%%&$%'!(!)+!,$-./$.+*

!

786*

!

+5!09:*36;

!

#<

!

=>!)/8()!$9:*36!#<

!

"?@A

!

=>*!!

!

$+68/B!1$8/6!/)49*2!"?@A

"@?A" ;+4!+;!+9$C*

D+!;628/BD +!;628/B

E2();6 6>*!;F49$5! request.

!

G6!8;!)/8()*H!6I$!;F49$5;!

I86>!6>!;+4!/+4!J/$6!6>!;+4!$9:

K2 6>!;8/B5!3>+2+36*2! r

ELMN!#@<N!D+!;628/BDO +/!+22+F!5862+5@!!=>8;!8;!+!>62$B/*$);!+22+F!

3$/6+8/8/B!+/!8/6B2N!+!!$+6N!+/J!+!;628/B

67.18#9)::;)#<$.)#*1;(3).5=>

? :)4)25$1#7:#$.)#$1#5@$#:0)274#2A1*25)1:

B#C#D 3)::*;)# = 5 ##:).5#5$#$EF)25#B

G#H#I 3)::*;)# + 3 ##:).5#5$#$EF)25#G

!B#JJ#I 3)::*;)# // 3 ##:).5#5$#7.5);)1#$EF)25#!B#

<1):(45#7:#D>

!B#J#I 3)::*;)# / 3 ##:).5#5$#7.5);)1#$EF)25#!B#

<1):(45#7:#######>

D##H#G

1)2)7K)

:)4)25$

*1;(3).

i i

67.18#9)::;)#<$.)#*1;(3).5=>

? :)4)25$1#7:#$.)#$1#5@$#:0)274#2A1*25)1:

B#C#D 3)::*;)# = 5 ##:).5#5$#$EF)25#B

G#H#I 3)::*;)# + 3 ##:).5#5$#$EF)25#G

!B#JJ#I 3)::*;)# // 3 ##:).5#5$#7.5);)1#$EF)25#!B#

<1):(45#7:#D>

!B#J#I 3)::*;)# / 3 ##:).5#5$#7.5);)1#$EF)25#!B#

<1):(45#7:#######>

D##H#G

1)2)7K)

:)4)25$

*1;(3).

i i

Not exactly; i is

not an object. It's a

variable that's bound to

an object