Object Oriented Programming Daniel Albu Based on a

Object Oriented Programming Daniel Albu Based on a lecture by John Mitchell Reading: Chapter 10 1

Object-oriented programming u. Primary object-oriented language concepts • • Dynamic Lookup Encapsulation Inheritance Subtyping u. Program organization • Work queue, Geometry program, Design Patterns u. Comparison • Objects as closures?

Objects u. An object consists of hidden data • hidden data instance variables, also called member data hidden functions also possible • public operations methods or member functions can also have public variables in some languages u. Object-oriented program: • Send messages to objects msg 1 method 1 . . . msgn methodn

Object-Orientation u. Programming methodology • Organize concepts into objects and classes • Build extensible systems u. Language concepts • • dynamic lookup encapsulation subtyping allows extensions of concepts inheritance allows reuse of implementation

Dynamic Lookup u In object-oriented programming, object message (arguments) code depends on object and message u. In conventional programming, operation (operands) meaning of operation is always the same If we need a certain operation to be able to perform various commands according to its input, we need to use, for example, the switch-case structure.

Example u. Add two numbers x add (y) different add if x is integer, complex u. Conventional programming add (x, y) function add has fixed meaning Very important distinction: Overloading is resolved at compile time, Dynamic lookup at run time

Language concepts u“dynamic lookup” • different code for different object • integer “+” different from real “+” uencapsulation usubtyping uinheritance

"Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object. . . encapsulation focuses upon the implementation that gives rise to this behavior. . . encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its essential characteristics. " -Grady Booch in Object Oriented Analysis and Design (one of the developers of UML)

Encapsulation u. Builder of a concept has detailed view u. User of a concept has “abstract” view u. Encapsulation separates these two views • Implementation code: operate on representation • Client code: operate by applying fixed set of operations provided by implementer of abstraction message Object

Language concepts u“Dynamic lookup” • different code for different object • integer “+” different from real “+” u. Encapsulation • Implementer of a concept has detailed view • User has “abstract” view • Encapsulation separates these two views u. Subtyping u. Inheritance

Subtyping and Inheritance u. Interface • The external view of an object u. Subtyping • Relation between interfaces u. Implementation • The internal representation of an object u. Inheritance • Relation between implementations

Object Interfaces u. Interface • The messages understood by an object u. Example: point • x-coord : returns x-coordinate of a point • y-coord : returns y-coordinate of a point • move : method for changing location u. The interface of an object is its type.

Subtyping u. If interface A contains all of interface B, then A objects can also be used B objects. Point x-coord y-coord move Colored_point x-coord y-coord color move change_color u. Colored_point interface contains Point • Colored_point is a subtype of Point

Inheritance u. Implementation mechanism u. New objects may be defined by reusing implementations of other objects u. Due to inheritance, if we update / fix / change the original object, the changes are also rolled to objects inheriting from it.

Example class Point private float x, y public point move (float dx, float dy); class Colored_point private float x, y; color c public point move(float dx, float dy); point change_color(color newc); u. Subtyping • Colored points can be used in place of points • Property used by client program u. Inheritance • Colored points can be implemented by reusing point implementation • Technique used by implementer of classes

OO Program Structure u. Group data and functions u. Class • Defines behavior of all objects that are instances of the class u. Subtyping • Place similar data in related classes u. Inheritance • Avoid reimplementing functions that are already defined

Example: Geometry Library u. Define general concept shape u. Implement two shapes: circle, rectangle u. Functions on implemented shapes center, move, rotate, print u. Anticipate additions to library

Shapes u. Interface of every shape must include center, move, rotate, print u. Different kinds of shapes are implemented differently • Square: four points, representing corners • Circle: center point and radius • Things like size and circumference are calculated in a different way in these 2 shapes and thus need to be implemented separately and cannot be inherited.

Subtype hierarchy Shape Circle Rectangle u. General interface defined in the shape class u. Implementations defined in circle, rectangle u. Extend hierarchy with additional shapes

Code placed in classes center move rotate print Circle c_center c_move c_rotate c_print Rectangle r_center r_move r_rotate r_print u. Dynamic lookup • circle move(x, y) calls function c_move u. Conventional organization • Place c_move, r_move in move function

Example use: Processing Loop Remove shape from work queue Perform action Control loop does not know the type of each shape e. g. we want to reduce the size of all the shapes currently on screen

resize(0. 5) Example use: Processing Loop Remove shape from work queue Perform action Control loop does not know the type of each shape e. g. we want to reduce the size of all the shapes currently on screen

Example use: Processing Loop resize(0. 5) Remove shape from work queue Perform action Control loop does not know the type of each shape e. g. we want to reduce the size of all the shapes currently on screen

Example use: Processing Loop resize(0. 5) Remove shape from work queue Perform action Control loop does not know the type of each shape e. g. we want to reduce the size of all the shapes currently on screen

Example use: Processing Loop Remove shape from work queue Perform action resize(0. 5) Control loop does not know the type of each shape e. g. we want to reduce the size of all the shapes currently on screen

Example use: Processing Loop Remove shape from work queue Perform action Control loop does not know the type of each shape e. g. we want to reduce the size of all the shapes currently on screen resize(0. 5)

Just make sure that every object you run the resize function on, has it implemented in it

So what’s the difference? Subtyping - refers to compatibility of interfaces. A type B is a subtype of A if every function that can be invoked on an object of type A can also be invoked on an object of type B. Inheritance - refers to reuse of implementations. A type B inherits from another type A if some functions for B are written in terms of functions of A.

Subtyping differs from inheritance Collection Indexed Array String Set Dictionary Sorted Set Subtyping Inheritance

Design Patterns u. Classes and objects are useful organizing concepts u. Culture of design patterns has developed around object-oriented programming • Shows value of OOP for program organization and problem solving

What is a design pattern? u. General solution that has developed from repeatedly addressing similar problems. u. Example: singleton • Restrict programs so that only one instance of a class can be created • Singleton design pattern provides standard solution u. Not a class template • Using most patterns will require some thought • Pattern is meant to capture experience in useful form Standard reference: Gamma, Helm, Johnson, Vlissides

Example Design Patterns u Singleton pattern • There should only be one object of the given class package { public class Singleton { private static var _instance: Singleton; public function Singleton(lock: Singleton. Lock) { } public static function get. Instance(): Singleton { if (_instance == null) _instance = new Singleton(new Singleton. Lock()); return _instance; } } } class Singleton. Lock { }

Example Design Patterns u. Visitor design pattern • Apply an operation to all parts of structure • Generalization of maplist, related functions • Standard programming solution: – Each element classes has accept method that takes a visitor object as an argument – Visitor is interface with visit() method for each element class – The accept() method of an element class calls the visit() method for its class

Taken from: http: //imgs. g 4 estatic. com/designpattern/Visitor. png

Façade Design Pattern uis a structural object pattern, which means it is a pattern related to composing objects into larger structures containing man objects. u. Implementation: relatively little actual code u. Example: A compiler

Closures as objects? public function Main() { init(); } public function init(): void { //create a counter var counter 1: Function = new. Counter(); trace( counter 1() ); //1 trace( counter 1() ); //2 trace( counter 1() ); //3 var counter 2: Function = new. Counter(); trace( counter 2() ); //1 trace( counter 2() ); //2 trace( counter 1() ); //4 --> scope of i is still with counter 1 } public function new. Counter(): Function { var i: int = 0; //variable i gets bound into returned anonymous function return function(): int { //i is available to the scope of the anonymous function i=i+1; return i; } } }

History of class-based languages u. Simula 1960’s • Object concept used in simulation u. Smalltalk 1970’s • Object-oriented design, systems u. C++ 1980’s • Adapted Simula ideas to C u. Java 1990’s • Distributed programming, internet u. Action. Script 3. 0 2000’s • Adapted Java ideas into Flash

Varieties of OO languages uclass-based languages (C++, Java, …) • behavior of object determined by its class uobject-based (Self, Java. Script) • objects defined directly umulti-methods (CLOS) • operation depends on all operands (Common Lisp Object System)

Summary u. Object-oriented design u. Primary object-oriented language concepts • • dynamic lookup encapsulation inheritance subtyping u. Program organization • Work queue, geometry program, design patterns u. Comparison • Objects as closures?

This presentation can be downloaded from: http: //www. danielalbu. com/oop. ppt
- Slides: 40