concepts in object oriented programming languages
play

Concepts in Object-Oriented Programming Languages Object-oriented - PDF document

CS 242 Outline of lecture Concepts in Object-Oriented Programming Languages Object-oriented design Primary object-oriented language concepts dynamic lookup encapsulation John Mitchell inheritance subtyping Program


  1. CS 242 Outline of lecture Concepts in Object-Oriented Programming Languages � Object-oriented design � Primary object-oriented language concepts • dynamic lookup • encapsulation John Mitchell • inheritance • subtyping � Program organization • Work queue, geometry program, design patterns � Comparison • Objects as closures? Objects What’s interesting about this? � An object consists of � Universal encapsulation construct hidden data • hidden data • Data structure instance variables, also called msg 1 method 1 • File system member data . . . . . . • Database hidden functions also possible msg n method n • Window • public operations • I nteger methods or member functions � Metaphor usefully ambiguous can also have public variables in some languages • sequential or concurrent computation • distributed, sync. or async. communication � Object-oriented program: • Send messages to objects Object-oriented programming Object-oriented Method [Booch] � Programming methodology � Four steps • organize concepts into objects and classes • Identify the objects at a given level of abstraction • build extensible systems • Identify the semantics (intended behavior) of objects � Language concepts • Identify the relationships among the objects • Implement these objects • encapsulate data and functions into objects � Iterative process • subtyping allows extensions of data types • inheritance allows reuse of implementation • Implement objects by repeating these steps � Not necessarily top-down • “Level of abstraction” could start anywhere 1

  2. This Method Example: Compute Weight of Car � Based on associating objects with components or concepts in a system � Car object: � Why iterative? • Contains list of main parts (each an object) – chassis, body, engine, drive train, wheel assemblies • An object is typically implemented using a number of constituent objects • Method to compute weight – sum the weights to compute total • Apply same methodology to subsystems, underlying � Part objects: concepts • Each may have list of main sub-parts • Each must have method to compute weight Comparison to top-down design Object-Orientation � Similarity: � Programming methodology • A task is typically accomplished by completing a • organize concepts into objects and classes number of finer-grained sub-tasks • build extensible systems � Differences: � Language concepts • Focus of top-down design is on program structure • dynamic lookup • OO methods are based on modeling ideas • encapsulation • Combining functions and data into objects makes • subtyping allows extensions of concepts data refinement more natural (I think) • inheritance allows reuse of implementation Dynamic Lookup Example � In object-oriented programming, � Add two numbers x � add (y) object � message (arguments) different add if x is integer, complex code depends on object and message � Conventional programming add (x, y) � In conventional programming, function add has fixed meaning operation (operands) meaning of operation is always the same Very important distinction: Overloading is resolved at compile time, Fundamental difference between abstract data types and objects Dynamic lookup at run time 2

  3. Language concepts Encapsulation � “dynamic lookup” � Builder of a concept has detailed view • different code for different object � User of a concept has “abstract” view • integer “+ ” different from real “+ ” � Encapsulation is the mechanism for separating � encapsulation these two views � subtyping � inheritance message Object Comparison Abstract data types � Traditional approach to encapsulation is through abstype q with abstract data types mk_Queue : unit -> q � Advantage is_empty : q -> bool • Separate interface from implementation insert : q * elem -> q remove : q -> elem � Disadvantage is … • Not extensible in the way that OOP is in program end We will look at ADT’s example to see what problem is Priority Q, similar to Queue Abstract Data Types � Guarantee invariants of data structure abstype pq with mk_Queue : unit -> pq • only functions of the data type have access to the is_empty : pq -> bool internal representation of data � Limited “reuse” insert : pq * elem -> pq remove : pq -> elem • Cannot apply queue code to pqueue, except by is … explicit parameterization, even though signatures identical in • Cannot form list of points, colored points program end � Data abstraction is important part of OOP, But cannot intermix pq’s and q’s innovation is that it occurs in an extensible form 3

  4. Language concepts Subtyping and Inheritance � “dynamic lookup” � Interface • different code for different object • The external view of an object � Subtyping • integer “+ ” different from real “+ ” � encapsulation • Relation between interfaces � subtyping � Implementation � inheritance • The internal representation of an object � Inheritance • Relation between implementations Object Interfaces Subtyping � Interface � If interface A contains all of interface B, then A objects can also be used B objects. • The messages understood by an object � Example: point Point Colored_point • x-coord : returns x -coordinate of a point x-coord x-coord • y -coord : returns y-coordinate of a point y -coord y -coord • move : method for changing location color move � The interface of an object is its type . move change_color � Colored_point interface contains Point • Colored_point is a subtype of Point Inheritance Example � Subtyping � Implementation mechanism class Point � New objects may be defined by reusing private • Colored points can be float x, y used in place of points implementations of other objects public • Property used by client point move (float dx, float dy); program � Inheritance class Colored_point private • Colored points can be float x, y; color c implemented by resuing public point implementation point move(float dx, float dy); • Propetry used by point change_color(color newc); implementor of classes 4

  5. OO Program Structure Example: Geometry Library � Group data and functions � Define general concept shape � Class � Implement two shapes: circle, rectangle � Functions on implemented shapes • Defines behavior of all objects that are instances of the class center, m ove, rotate, print � Subtyping � Anticipate additions to library • Place similar data in related classes � Inheritance • Avoid reimplementing functions that are already defined Shapes Subtype hierarchy � Interface of every shape must include Shape center, m ove, rotate, print � Different kinds of shapes are implemented differently • Square: four points, representing corners Circle Rectangle • Circle: center point and radius � General interface defined in the shape class � Implementations defined in circle, rectangle � Extend hierarchy with additional shapes Code placed in classes Example use: Processing Loop center move rotate print Circle c_center c_move c_rotate c_print Remove shape from work queue Perform action Rectangle r_center r_move r_rotate r_print � Dynamic lookup • circle � move(x,y) calls function c_move Control loop does not know the � Conventional organization type of each shape • Place c_move, r_move in move function 5

  6. Subtyping differs from inheritance Design Patterns � Classes and objects are useful organizing Collection concepts � Culture of design patterns has developed around object-oriented programming Indexed Set • Shows value of OOP for program organization and problem solving Array Dictionary Sorted Set String Subtyping Inheritance What is a design pattern? OOP in Conventional Language � General solution that has developed from � Records provide “dynamic lookup” repeatedly addressing similar problems. � Scoping provides another form of encapsulation � Example: singleton • Restrict programs so that only one instance of a class can be created • Singleton design pattern provides standard solution Try object-oriented programming in ML. � Not a class template Will it work? Let’s see what’s fundamental to OOP • Using most patterns will require some thought • Pattern is meant to capture experience in useful form Standard reference: Gamma, Helm, Johnson, Vlissides Dynamic Lookup (again) Stacks as closures fun create_stack(x) = let val store = ref [x] in receiver � operation (arguments) {push = fn (y) = > store := y::(!store), pop = fn () => code depends on receiver and operation case !store of nil = > raise Empty | This is may be achieved in conventional languages y::m = > (store := m; y) } end; using record with function components val stk = create_stack(1); stk = { pop= fn,push= fn} : { pop:unit -> int , push:int -> unit} 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend