+ All Categories
Home > Documents > 1 Object Oriented Programming Daniel Albu Based on a lecture by John Mitchell Reading: Chapter 10.

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

Date post: 20-Dec-2015
Category:
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
40
1 Object Oriented Programming Daniel Albu Daniel Albu Based on a lecture by John Mitchell Reading: Chapter 10
Transcript
Page 1: 1 Object Oriented Programming Daniel Albu Based on a lecture by John Mitchell Reading: Chapter 10.

1

Object Oriented Programming

Daniel AlbuDaniel AlbuBased on a lecture by John Mitchell

Reading: Chapter 10

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

Object-oriented programming

Primary object-oriented language concepts• Dynamic Lookup • Encapsulation• Inheritance • Subtyping

Program organization• Work queue, Geometry program, Design Patterns

Comparison• Objects as closures?

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

Objects

An object consists of • hidden data

instance variables, also called member datahidden functions also possible

• public operationsmethods or member functionscan also have public variables in some languages

Object-oriented program:• Send messages to objects

hidden data

method1msg1

. . .. . .

methodnmsgn

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

Object-Orientation

Programming methodology• Organize concepts into objects and classes • Build extensible systems

Language concepts• dynamic lookup• encapsulation• subtyping allows extensions of concepts• inheritance allows reuse of implementation

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

Dynamic Lookup

In object-oriented programming,object message (arguments)

code depends on object and message

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.

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

Example

Add two numbers x add (y)different add if x is integer, complex

Conventional programming add (x, y)function add has fixed meaning

Very important distinction:Overloading is resolved at compile time, Dynamic lookup at run time

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

Language concepts

“dynamic lookup”• different code for different object• integer “+” different from real “+”

encapsulation subtyping inheritance

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

"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)

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

Encapsulation

Builder of a concept has detailed view User of a concept has “abstract” view 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

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

Language concepts

“Dynamic lookup”• different code for different object• integer “+” different from real “+”

Encapsulation• Implementer of a concept has detailed view• User has “abstract” view• Encapsulation separates these two views

Subtyping Inheritance

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

Subtyping and Inheritance

Interface• The external view of an object

Subtyping• Relation between interfaces

Implementation• The internal representation of an object

Inheritance• Relation between implementations

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

Object Interfaces

Interface• The messages understood by an object

Example: point• x-coord : returns x-coordinate of a point• y-coord : returns y-coordinate of a point• move : method for changing location

The interface of an object is its type.

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

Subtyping

If interface A contains all of interface B, then A objects can also be used B objects.

Colored_point interface contains Point• Colored_point is a subtype of Point

Pointx-coordy-coordmove

Colored_pointx-coordy-coordcolormovechange_color

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

Inheritance

Implementation mechanism New objects may be defined by reusing

implementations of other objects Due to inheritance, if we update / fix /

change the original object, the changes are also rolled to objects inheriting from it.

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

Example

class Point private

float x, y

public

point move (float dx, float dy);

class Colored_pointprivate

float x, y; color c

public

point move(float dx, float dy);

point change_color(color newc);

Subtyping• Colored points can be

used in place of points

• Property used by client program

Inheritance• Colored points can be

implemented by reusing point implementation

• Technique used by implementer of classes

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

OO Program Structure

Group data and functions Class

• Defines behavior of all objects that are instances of the class

Subtyping• Place similar data in related classes

Inheritance• Avoid reimplementing functions that are

already defined

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

Example: Geometry Library

Define general concept shape Implement two shapes: circle, rectangle Functions on implemented shapes

center, move, rotate, print

Anticipate additions to library

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

Shapes

Interface of every shape must includecenter, move, rotate, print

Different kinds of shapes are implemented differently• Square: four points, representing corners• Circle: center point and radius

• Things like size and circumference are calculated ina different way in these 2 shapes and thus need to be

implemented separately and cannot be inherited.

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

Subtype hierarchy

Shape

Circle Rectangle

General interface defined in the shape class Implementations defined in circle, rectangle Extend hierarchy with additional shapes

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

Code placed in classes

Dynamic lookup• circle move(x,y) calls function c_move

Conventional organization• Place c_move, r_move in move function

center move rotate print

Circle c_center

c_move c_rotate c_print

Rectangle

r_center r_move r_rotate r_print

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

Example use: Processing Loop

Remove shape from work queuePerform 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

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

Example use: Processing Loop

Remove shape from work queuePerform 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)

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

Example use: Processing Loop

Remove shape from work queuePerform 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)

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

Example use: Processing Loop

Remove shape from work queuePerform 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)

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

Example use: Processing Loop

Remove shape from work queuePerform 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)

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

Example use: Processing Loop

Remove shape from work queuePerform 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)

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

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

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

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.

So what’s the difference?

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

Subtyping differs from inheritance

Collection

Set

Sorted Set

Indexed

Array Dictionary

StringSubtyping

Inheritance

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

Design Patterns

Classes and objects are useful organizing concepts

Culture of design patterns has developed around object-oriented programming• Shows value of OOP for program organization

and problem solving

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

What is a design pattern?

General solution that has developed from repeatedly addressing similar problems.

Example: singleton• Restrict programs so that only one instance of a class

can be created• Singleton design pattern provides standard solution

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

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

Example Design Patterns

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:SingletonLock){}

public static function getInstance():Singleton{

if (_instance == null) _instance = new Singleton(new SingletonLock()); return _instance;

} }} class SingletonLock{}

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

Example Design Patterns

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

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

Taken from: http://imgs.g4estatic.com/designpattern/Visitor.png

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

Façade Design Pattern

is a structural object pattern, which means it is a pattern related to composing objects into larger structures containing man objects.

Implementation: relatively little actual code

Example: A compiler

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

Closures as objects?public function Main(){

init();}

public function init():void{

//create a countervar counter1:Function = newCounter();trace( counter1() );//1trace( counter1() );//2trace( counter1() );//3var counter2:Function = newCounter();trace( counter2() );//1trace( counter2() );//2trace( counter1() );//4 --> scope of i is still with counter1

}

public function newCounter():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 functioni=i+1;return i;

}}

}

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

History of class-based languages

Simula 1960’s• Object concept used in simulation

Smalltalk 1970’s• Object-oriented design, systems

C++ 1980’s • Adapted Simula ideas to C

Java 1990’s• Distributed programming, internet

ActionScript 3.0 2000’s• Adapted Java ideas into Flash

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

Varieties of OO languages

class-based languages (C++, Java, …)• behavior of object determined by its class

object-based (Self, JavaScript)• objects defined directly

multi-methods (CLOS)• operation depends on all operands

(Common Lisp Object System)

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

Summary

Object-oriented design Primary object-oriented language concepts

• dynamic lookup • encapsulation• inheritance • subtyping

Program organization• Work queue, geometry program, design patterns

Comparison• Objects as closures?

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

http://www.danielalbu.com/oop.pptThis presentation can be downloaded from:This presentation can be downloaded from:


Recommended