Docstoc

oo

Document Sample
oo Powered By Docstoc
					CSE-321 Programming Languages

Object Orientation

          박성우



       POSTECH
      June 2, 2009
 High productivity in programming
• Ultimate goal of SE/PL research
• Key enabling technologies
   – Modularity
       • divide the code into modules.
       • Without modularity, code maintenance becomes a
         nightmare.
   – Code reuse
       • reuse existing code without duplicating it.
       • No code reuse means code size explosion.
   – Abstraction
       • hide the details, and increase confidence.
       • Without abstraction, reliability is at stake.
                                                          2
                       Java
• Class/Object for modularity
   – A class definition bundles all the data and
     methods related together.
• Inheritance for code reuse
   – The code in the superclass can be reused.




                                                   3
                             Class Exp
abstract class Exp {
  Exp subst(Exp v, Int x);
                                all data and methods common to Exp
  ...
}

class Apply extends Exp {
   Apply(Exp e1, Exp 2);
                                    all data and methods specific to Apply
   ...
}

class Fun extends Exp {
   Fun(Exp e);
                                 all data and methods specific to Fun
   ...
}

                                                                             4
               Extending Exp
abstract class Exp
  ...

class Pair extends Exp {
  Pair(Exp e1, Exp e2);    all data and methods specific to Pair
  ...
}


   Data extension is easy in OO.
                                                               5
             Adding a new method
abstract class Exp {
  Exp subst(Exp v, Int x);
                             need to revisit Exp
  Exp eval();
}

class Apply extends Exp {
   Apply(Exp e1, Exp 2);
                                need to revisit Apply
   Exp eval();
}

class Fun extends Exp {
   Fun(Exp e);
                             need to revisit Fun
   Exp eval();
}

                                                        6
                      Oops!
abstract class Exp
  ...

class Pair extends Exp {
  Pair(Exp e1, Exp e2);
                           forgot to revisit Pair
}




                                                    7
      Functional extension in OO
• We need to keep track of all subclasses.
• We need to make changes in scattered files.

• What if we forget to revisit some classes?
• What if the source code cannot be located?
• What if the source code for some class does not even exist?
  – available only as part of a library
  – declined by the author

) We lose functional modularity in OO.
   – the related methods are scattered thought different
     classes residing in different files.
                                                            8
                           ML
datatype exp =
  Apply of exp * exp
| Fun of exp

val subst (e : exp, v : exp) = ...

val eval (e : exp) = ...        new function



    Functional extension is easy in ML.
                                               9
           Data extension in ML
datatype exp =
  Apply of exp * exp
| Fun of exp
| Pair of exp * exp

val subst (e : exp, v : exp) = ...
  ... Pair (x1, x2) => ...                need to revisit subst



val eval (e : exp) = ...
                                     need to revisit eval
  ... Pair (x1, x2) => ...
                                                                  10
                    Modules in ML
structure Exp =
struct
   datatype exp =
       Apply of exp * exp
   |   Fun of exp
                                       all related datatypes and functions
   |   Pair of exp * exp                                in the same module


  val subst (e : exp, v : exp) = ...

  val eval (e : exp) = ...
end
                                                                         11
    Modular Programming in ML
• Module
   – basic unit of modular programming
• Signature
   – hides the details of modules
   – achieves abstraction
• Functor
   – function over modules
   – helps reduce the code size of modules


                                             12

				
DOCUMENT INFO