Smalltalk -- a radical change in programming

A



  1972  Dynabook -- Alan Kay
  1976  SmallTalk76
  1980  SmallTalk80
  1990  ObjectWorks/SmallTalk -- VisualWorks
  

Design principles -- rapid prototyping

  • uniform object model -- control
  • dynamic typing -- flexible
  • standard libraries -- functionality

slide: The language Smalltalk


Terminology

Smalltalk



slide: Smalltalk -- terminology


Literal constants


slide: Smalltalk -- expressions (1)


Assignment

Variables

Block expressions


slide: Smalltalk -- expressions (2)


Message expressions

  • unary, binary, keyword

Unary

  • 1.0 sin , Random new

Binary

  • arithmetic -- ctr + 1
  • comparison -- aVar >= 200
  • combination -- 100 @ 200
  • association -- # Two -> 2

slide: Smalltalk -- expressions (3)


Keyword methods


    (i <= 7) 
          ifTrue: [ m:= "oke" ]
          ifFalse: [ ... ]
  

Control structures

  • conditional -- ifTrue: ifFalse:
  • iteration -- whileTrue:
  • looping -- to: by: do: [ :i | ... ]

slide: Smalltalk -- control


Object -- behavior

Class -- description

  • class variables, class methods
  • Self -- self reference


    slide: Smalltalk -- objects (1)


    Example -- class

    
      Behavior subclass: #Ctr
      instanceVariableNames: 'value'
      Ctr methodsFor: 'initialization'
         initialize
      	value := 0.
      Ctr methodsFor: 'modifications'
         add: aValue
      	value := value + aValue.
      Ctr methodsFor: 'inspection'
         value
      	^value
      

    slide: Smalltalk -- objects (2)


    Class description -- meta class

    
      Ctr class
      instanceVariableNames: ''
      Ctr class methodsFor: 'instance creation'
      	new
      		^super new initialize
      

    slide: Smalltalk -- objects (3)


    Inheritance

    
        Object
          Magnitude
             ArithmeticValue
                Number
                   Integer
      

    slide: Smalltalk -- inheritance


    
      Model subclass: #Ctr 
    Model
    ... initialize value := 0. TV open: self. ... add: anInt value := value + anInt. self changed: #value.

    slide: Smalltalk -- technology (1)


    
      View subclass: #TV 
    View
    instanceVariableNames: '' TV methodsFor: 'updating' update: aValue Transcript show: 'ok'; cr . TV class instanceVariableNames: '' TV class methodsFor: 'instance creation' open: aCtr self new model: aCtr

    slide: Smalltalk -- technology (2)


    The language Smalltalk

    A



    slide: Smalltalk -- summary


    Eiffel -- a language with assertions

    B


    • bottom-up development -- class design
    • contracts -- specify client/server relationships

    Design principles -- correctness

    • static typing -- type secure
    • multiple inheritance -- polymorphism
    • dynamic binding -- refinement
    • generic classes -- abstraction

    slide: The language Eiffel


    The language Eiffel -- keywords


    slide: Eiffel -- terminology


    Type expressions -- conformance

    • basic types -- Boolean, Integer
    • formal parameter types -- Array[T], List[T]
    • class types -- user-defined
    • anchored types -- like current

    Value expressions

    • arithmetic, comparison, method evaluation -- o.m(...)

    Assignment

    • var := expression

    slide: Eiffel -- type expressions


    Control -- method refinement


    slide: Eiffel -- control


    
        class  counter  export  inc val  feature 
       count : Integer
       create  is do  count := 0  end 
       inc( n : Integer )  is 
      	 require  n > 0  do 
      	count := count + n
      	 ensure  count =  old  count + n
      	 end 
       val : Integer  is do   Result  := count  end 
        invariant  count >= 0
        end  -- class counter
      

    slide: Eiffel -- objects


    Multiple inheritance

    
      class Fixed_List[T] export ...
      inherit
      	List[T]
      	Array[T]
      feature
       ...
      end
      

    slide: Eiffel -- inheritance


    Rename and/or redefine

    
      class C export ... inherit
        A rename   m   as m1 redefine p
        B rename   m   as m2 redefine q
      feature
      ...
      end
      

    slide: Eiffel -- techniques


    The language Eiffel

    B



    slide: Eiffel -- summary


    C++ -- is much more than a better C

    C


    
       1972   C  Kernigan and Ritchi (Unix) 
       1983   C++   (Simula 1967)
       1985   ANSI/ISO C
       1996   ANSI/ISO C++
      

    Design principles -- the benefits of efficiency

    • superset of C -- supporting OOP
    • static typing -- with user-defined exceptions
    • explicit -- no default virtual functions
    • extensible -- libraries in C and C++

    slide: The language C++


    Keywords:

    C++ overview


    • inline, new, delete, private, protected, public

    Language features:

    • constructors -- to create and initialize
    • destructors -- to reclaim resources
    • virtual functions -- dynamic binding
    • (multiple) inheritance -- for refinement
    • type conversions -- to express relations between types
    • private, protected, public -- for access protection
    • friends -- to allow for efficient access

    slide: C++ -- terminology (1)


    Some basic terminology

    name -- denotes an object, a function, set of functions, enumerator, type, class member, template, a value or a label

    object -- region of storage


    slide: C++ -- terminology (2)


    Type expressions


    slide: C++ -- expressions (1)


    Expressions

    • operators -- + , - ,.., < , <= ,.., == , != ,.., && , ||
    • indexing -- o[ e ]
    • application -- o(...)
    • access -- o.m(...)
    • dereference -- p->m(...)
    • in/decrement -- o++, o--
    • conditional -- b?e1:e2

    Assignment

    • var = expression
    • modifying -- +=. -=, ...

    slide: C++ -- expressions (2)


    Control


    slide: C++ -- control


    ADT in C style

    
      struct ctr { int n; }
      
      void ctr_init(ctr& c) { c.n = 0; }
      void ctr_add(ctr& c, int i) { c.n = c.n + i; }
      int ctr_val(ctr& c) { return c.n; }
      

    Usage

    
      ctr c; ctr_init(c); ctr_add(c,1); 
      ctr* p = new ctr; ctr_init(*p); ctr_add(*p);  
      

    slide: C++ -- objects (1)


    ADT in C++

    
      class ctr {
      public:
         ctr() { n = 0; }  // constructor
         ~ctr() { cout << "bye"; }; // destructor
         void add( int i = 1) { n = n + i; }
         int val( ) { return n; }
      private:
      int n;
      };
      

    Usage

    
      ctr c; c.add(1); cout << c.val(); 
      ctr* p = new ctr(); c->add(1); ...
      

    slide: C++ -- objects (2)


    Inheritance

    
      class A { 
    ancestor
    public: A() { n = 0; } void add( int i ) { n = n + i; } virtual int val() { return n; } protected: // private would deny access to D int n; }; class D : public A {
    descendant
    public: D() : A() { } int val() { return n % 2; } };

    slide: C++ -- inheritance


    Techniques


    slide: C++ -- techniques (1)


    
      class ctr { 
    C++
    public: ctr(int i = 0, char* x = "ctr") { n = i; strcpy(s,x); } ctr& operator++(int) { n++; return *this; } int operator()() { return n; } operator int() { return n; } operator char*() { return s; } private: int n; char s[64]; };

    Usage

    
      ctr c; cout << (char*) c++ << "=" << c();
      

    slide: C++ - techniques (2)


    The language C++

    C



    slide: C++ -- summary


    Java -- the dial-tone of the Internet

    D


    
       1995   Introduction at WWW3
       1996   1.0 with AWT
       1997   1.1.x with modified event handling
       1998   version 1.2 (beta) with Swing
      

    Design principles -- safety

    • a modern programming language
    • C++ syntax, no pointers
    • virtual machine (runs on many platforms)
    • libraries: threads, networking, AWT
    • downloadable classes
    • support for applets
    • extensions and APIs: Beans, Swing, MEDIA, 3D

    See: www.javasoft.com
    and java.sun.com/docs/books/tutorial


    slide: The language Java


    Keywords:

    Java overview


    • new, finalize, extends, implements, synchronized

    Language features:

    • no pointers -- references only simplify life
    • garbage collection -- no programmers' intervention
    • constructors -- to create and initialize
    • single inheritance -- for refinement
    • interfaces -- abstract classes
    • synchronized -- to prevent concurrent invocation
    • private, protected, public -- for access protection

    slide: Java -- terminology (1)


    Type expressions


    slide: Java -- expressions (1)


    Expressions

    • operators -- + , - ,.., < , <= ,.., == , ! = ,.., && , ||
    • indexing -- o[ e ]
    • access -- o.m(...)
    • in/decrement -- o++, o--
    • conditional -- b?e1:e2

    Assignment

    • var = expression
    • modifying -- +=. -=, ...

    slide: Java -- expressions (2)


    Control


    slide: Java -- control


    Hello World -- Java (1)

    
      
      public class HelloWorld {
      public static void main(String[] args) {
             System.out.println("Hello World");
             }
      };
      

    slide: Java -- objects (1)


    Hello World - interface

    
      public interface World {
      public void hello();
      };
      

    Hello World - class

    
      public class HelloWorld implements World {
      public void hello() {
             System.out.println("Hello World");
             }
      };
      

    slide: Java -- objects (2)


    Hello World -- Java (2)

    
      import java.awt.Graphics; 
      public class HelloWorld extends java.applet.Applet { 
      
      public void init() { resize(150,50); } 
      
      public void paint(Graphics g) { 
             g.drawString("Hello, World!", 50, 25); 
             } 
      }; 
      

    slide: Java -- inheritance


    Java -- techniques


    slide: Java -- techniques


    The language Java

    D



    slide: Java -- summary


    DLP -- introduces logic into object orientation

    E


    • DLP = LP + OO + ||

    Design principles -- support for knowledge-intensive applications

    • objects -- dynamic states
    • processes -- communication by rendezvous
    • distributed backtracking -- exhaustive search

    slide: The language DLP


    Objects -- a labeled set of clauses

    DLP


    
      object name {
      var variables.
      
      constructor clauses
      method clauses
      }
      

    slide: DLP -- terminology


    Expressions -- terms

    Prolog


    Unification -- bi-directional parameter passing


    slide: DLP -- expressions


    Prolog

    
      p(X,Y) :- r(Z), b(X). // clauses
      p(X,Y) :- q(Y), b(X).
      b(X) :- a(X).
      b(0).  // facts
      a(1).
      q(2).
      

    Query

    
      ?- p(X,Y). // results in (X = 1,Y = 2)
                  // and (X = 0, Y = 2)  
      

    slide: DLP -- control (1)


    List processing -- backtracking

    
      member(X,[X|_]).
      member(X,[_|T]) :- member(X,T).
      
      append([],L,L).
      append([ H | T ],L,[ H | R ]):- append(T,L,R).
      

    slide: DLP -- control (2)


    Additional statements

    DLP



    slide: DLP -- objects (1)


    Computation model -- distributed logic

    objects -- state + methods
    processes -- to evaluate goals
    communication -- backtrackable rendezvous


    slide: DLP -- objects (2)


    Object

    
      object travel { 
    travel
    var cities = [amsterdam, paris, london]. travel() :- accept( all ), travel(). reachable(X) :- member(X, cities). add(X) :- append( cities, [X], R), cities := R. }

    Usage

    
       ?- O = new travel(), O!reachable(X), write(X).
      

    slide: DLP -- objects (3)


    Inheritance

    
      object agency : travel { 
    agency
    agency() :- accept( any ), agency(). book(X,Y) :- reachable(X), price(X,Y). price(amsterdam,5). ... }

    slide: DLP -- inheritance


    Techniques -- logic


    slide: DLP -- technology


    The language DLP

    E



    slide: DLP -- summary

    The Unified Modeling Language (UML) resulted from a joint effort of leading experts in object-oriented analysis and design, Grady Booch, Jim Rumbaugh and Ivar Jacobson, also known as the three amigos, all currently employees of Rational.


    Unified Modeling Language

    UML


    • class diagrams -- conceptual structure
    • use cases -- functional requirements
    • interaction diagrams -- operational characteristics
    • package and deployment diagrams -- implementation
    • state and activity diagrams -- dynamic behavior

    See www.rational.com/uml and UML Distilled,  [Fowler97]


    slide: UML



    slide: Class diagrams



    slide: Use cases



    slide: Interaction diagrams



    slide: Package and deployment diagrams



    slide: State diagrams

    
        event(arguments)[conditions]/action
      

    The UML toolbox is very rich. It allows you to model every conceivable aspect of the system. Nevertheless, to my mind, graphical models are not always appropriate. But, on the other hand, most people like them and they often make a good impression, suggesting clarity ...


    Interface Definition Language

    IDL


    • modules -- for clustering
    • interfaces -- correspond with objects
    • attributes -- may be read only
    • operations -- best effort invocation
    • exceptions -- for handling failure

    Language bindings

    • C, Smalltalk, C++, Java

    See www.infosys.tuwien.ac.at/Research/Corba/OMG


    slide: Interface Definition Language -- IDL


    
      module universe { 
    universe
    interface world { void hello(); void ask(in string x); string tell(); oneway void halt(); }; };

    slide: universe.idl


    Basic types

    Constructed types

    Object references


    slide: Types and values


    Operations

    Exceptions

    
      exception out_of_range { long count; }
      
      interface writer {
        void position(in long pos) raises(out_of_range);
        }
      

    slide: Operations and exceptions


    Interfaces and inheritance

    Multiple (interface) inheritance

    >
      interface A {  };
      interface B {  };
      interface C : A { };
      interface D : B, A { };
      

    slide: Interfaces and inheritance

    
      interface iterator { 
    iterator
    Object next(); };

    Language bindings

    The Object interface

    
      interface Object { 
    PIDL
    InterfaceDef get_interface(); Object duplicate(); ... }

    slide: Language bindings


    Hello (CORBA) Universe

    H



    slide: Hello (CORBA) Universe

    
      module universe { 
    universe.idl
    interface world {
    world
    void hello(); void ask(in string x); string tell(); oneway void halt(); }; };

    C++ realization


    slide: C++ realization


    C++ broker -- class

    
      class broker { 
    broker.h
    public: broker(); void init(int& argc, char**& argv, int fl=0); // 1 = client int operator()(); // to run the server char* ref(corba::object* x); // object_to_string corba::object* ref(const char* s); // string_to object corba::object* object(const char* file); // get object IOR file void refout(const char* f, corba::object* x); // IOR to file };

    slide: C++ broker -- class


    C++ broker -- implementation

    
      broker::broker() { }  
    broker.c
    void broker::init(int& argc, char**& argv, int fl=0) { _orb = CORBA_ORB_init(argc, argv); if (!fl) _boa = _orb -> BOA_init(argc, argv); } } int broker::operator()() { _boa -> impl_is_ready(CORBA_ImplementationDef::_nil()); }

    slide: C++ broker -- implementation


    C++ client

    
          int main(int argc, char* argv[], char*[]) { 
    client.c
    broker* _broker = new broker(); // get yourself a broker try { _broker->init(argc,argv); // initialize broker corba::object* obj = _broker->object("world.ref"); universe_world_var world = universe_world::_narrow(obj); { // some object invocations world -> hello(); world -> ask("How are Clinton's affairs?"); cout << "client:" << world->tell() << endl; } } catch(corba::exception ex) { ... } }

    slide: client


    C++ server

    
      int main(int argc, char* argv[], char*[]) 
    server.c
    { broker* _broker = new broker(); // get yourself a broker try { _broker->init(argc,argv); // initialize orb and boa universe_world_var p = new universe_world_srv; _broker->refout("world.ref", p); // write identity _broker->operator()(); // run the the world } catch(corba::exception& ex) { ... } }

    slide: server


    C++ world implementation

    
      void universe_world_srv::hello() {  
    world_srv.c
    cout << "Hello World!" << endl; } void universe_world_srv::ask(const char* s) { cout << "Asking: " << s << endl; } char* universe_world_srv::tell() { char result[] = "ok"; CORBA_String_var s = (const char*) result; return s._retn(); } void universe_world_srv::halt() { exit(0); }

    slide: world-srv.c


    Java realization


    slide: Java realization


    
      import org.omg.CORBA.*; 
    broker.java
    import java.io.*; public class broker { boolean _fl = false; java.applet.Applet _applet = null; public broker() { } public broker(boolean fl) { _fl = fl; } public broker(java.applet.Applet x) { _applet = x; init(null); } public void init(String[] args) { if (_applet == null) { _orb = ORB.init(args,new java.util.Properties()); if (!_fl) _boa = _orb.BOA_init(args,new java.util.Properties()); } else _orb = ORB.init(_applet, null); } public int operator() {
    run -- server-only
    if (!_fl) _boa.impl_is_ready(null); return 0; // OK } .... };

    slide: Java -- broker


    Java -- client

    
      package universe;
      
      import org.omg.CORBA.*; 
    client.java
    import java.io.*; import hush.broker; // see broker.java public class client { public static void main(String args[]) { broker _broker = new broker(true); // true means client only try { _broker.init(args); // init orb org.omg.CORBA.Object obj = _broker.object("world.ref"); world world = worldHelper.narrow(obj); if (world == null) throw new RuntimeException(); System.out.println("Enter 'h' (hello) or 'x' (exit):"); ... // do some requests to the world } catch(...) { ... } } };

    slide: Java -- client

    Java -- server

    
      package universe; 
    server.java
    import org.omg.CORBA.*; import java.io.*; import hush.broker; // see broker.java public class server { public static void main(String args[]) { broker _broker = new broker(); try { _broker.init(args); // create orb en boa; world_srv p = new world_srv(); // create impl object _broker.refout("world.ref",p); // write ref _broker.html("world.htm",p, // create world.htm " code=universe/applet.class " + "width=500 height=300"); _broker.operator(); // run == boa.impl_is_ready(null); } catch(SystemException ex) { _broker.print(ex); System.exit(1); } System.exit(0); } }

    Java -- server implementation

    
      package universe; 
    world_srv.java
    import org.omg.CORBA.*; public class world_srv extends _worldImplBase { public void hello() { System.out.println("Hello World!"); } public void ask(String msg) { System.out.println(msg); } public String tell() { String s = new String("ok"); return s; } public void halt() { System.exit(0); } }

    Prolog realization


    slide: Prolog realization


    
      broker(client(M:F)) :- 
    broker.pl
    ... corba_initialize_orb([], _), factory(F). // gives a handle to the server object broker(server(M:I)) :- ... corba_initialize_server([server,server(test), timeout(infinite)],Server), ... // create server object G =.. [_Create,_Server,Self], call(G), corba_object_to_string(Self,IOR), open(IORFile,write,Fd), // write reference to file format(Fd,'string',[IOR]), close(Fd), corba_main_loop(Server). // enter main loop

    slide: Prolog -- broker


    
      
    client.pl
    :- use_module(universe). // see universe.pl :- [broker]. // include broker main :- broker(client(universe:F)), // initialize the broker assert(client(factory(F))), run. run :- h, ask('What is the state of Clinton s affairs?'), write('Type h to say hallo, x to quit.'),nl. ask(X) :- client(factory(F)), write(client(ask(X))),nl, world_ask(F,X), world_tell(F,R), write(client(ans(R))),nl. h :- client(factory(F)), world_hello(F). q :- client(factory(F)), world_halt(F), halt. x :- halt.

    slide: Prolog -- client


    
      :- [broker]. 
    server.pl
    main :- broker(server(universe:world)).

    slide: Prolog -- server


    Prolog -- implementation

    
      :- module('universe',[]). 
    universe.pl
    world_hello(_Self) :- write('Hello World'),nl. world_ask(_Self, X) :- write(asking(X)), nl. world_tell(_Self,Y) :- Y = 'logically, ok', write(telling(Y)),nl. world_halt(_Self) :- halt.

    slide: Prolog -- implementation


    Configure, make and test


    slide: Configure, make and test


    Conclusions


    slide: Conclusions


    Software development projects

    I



    slide: Software development projects