Object Oriented Programming in APL - PowerPoint PPT Presentation

object oriented programming in apl n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Object Oriented Programming in APL PowerPoint Presentation
Download Presentation
Object Oriented Programming in APL

play fullscreen
1 / 152
Object Oriented Programming in APL
120 Views
Download Presentation
cleta
Download Presentation

Object Oriented Programming in APL

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Object Oriented Programming in APL A New Direction Dan Baronet 2008 V1.20

  2. Dyalog V11 was released in 2006. One of the major features was: Object Orientation OOAPL

  3. This presentation is based on two things: • Why should I know about Object Orientation programming? • How do I use it in APL? OOAPL

  4. Reasons: why do this? • Because Object Orientation is a Valuable Tool of Thought! • To be able to Share Tools and Components more Easily • To help us Manage Complexity in all Project Phases • To use .Net more easily OOAPL

  5. Another tool Object Orientation is another tool for programmers. Users won’t see any difference. It is a way for programmers to simplify their lives by supplying each other with already made objects (almost) ready to use. OOAPL

  6. The evolution of data • Elementary data elements (like int) and arrays of them (all the same) • Structures (like {i: int; d: date}), many elementary elements or structures (recursive) • Namespaces (structures whose elements are referred to by name). They include code. • Classes (namespaces with initializers & cleaners) OOAPL

  7. d a t a code The evolution of data In traditional (procedural) systems, data is separated from code: OOAPL

  8. d a t a code The evolution of data This is the same in traditional APL systems, data is separated from code: OOAPL

  9. The evolution of data In OO systems, data is more tightly coupled with code: private code d a t a OOAPL

  10. The evolution of data Same thing in OO APL systems: private code d a t a OOAPL

  11. The Object Oriented paradigm Definitions OOAPL

  12. OO fundamental concepts The Object Oriented paradigm is based on: • Classes & Interfaces • Instances • Members • Message passing • Inheritance • Encapsulation • Polymorphism OOAPL

  13. OO fundamental concepts 1. Classes A class defines the abstract characteristics of some object, akin to a blueprint. It describes a collection of members (data and code) Classes can be nested OOAPL

  14. OO fundamental concepts Classes ex: House blueprint OOAPL

  15. OO fundamental concepts Classes A classcan implement one or more interfaces. An interface describes what it should do. The class describes how it should be done. If a class implements an interface it should do it completely, no partial implementation is allowed. OOAPL

  16. OO fundamental concepts Interfaces An interface describes how to communicate with an object. Ex: electrical system & electrical outlets OOAPL

  17. Interfaces - example The following maneuvering interface describes what HAS to be done with a machine: Method Steer (direction); // where to go Method Accellerate (power); // go faster Method SlowDown (strength); // go slower It does NOT specify HOW it should be done. OOAPL

  18. Interfaces - example This car object implements maneuvering: Class car : maneuvering; Method SteeringWheel: implements maneuvering.Steer (turn); ... Method GasPedal: implements maneuvering.Accellerate (push); ... Method BreakPedal: implements maneuvering.SlowDown (hit); OOAPL

  19. Interfaces - example This plane object implements maneuvering: Class plane : maneuvering; Method Yoke: implements maneuvering.Steer (move); ... Method Handle: implements maneuvering.Accellerate (pp); ... Method Flaps: implements maneuvering.SlowDown (degree); OOAPL

  20. OO fundamental concepts 2. Instances Those are tangible objects created from a specific class. Upon creation any specific action is carried out by the constructor (code) of the class if some sort of initialization is required. Upon destruction, the destructor is responsible for cleaning up. OOAPL

  21. OO fundamental concepts 2. Instances New House (Blue): After the creation of the new House the Constructor paints it in blue OOAPL

  22. OO fundamental concepts 3. Members This is the code and the data of a class. The code is divided into Methods (functions) and data is divided into Fields (variables). There are also Properties which are Fields implemented via functions to read/set them. OOAPL

  23. OO fundamental concepts Members These can reside inside the class if shared or in the instance. Ex: the .Net classDateTime has a member Now that does not require an instance to be called. e.g. DateTime.Now ⍝ straight from the class 2007/10/21 9:12:04 OOAPL

  24. OO fundamental concepts Shared vs Instance Shared members remain in the class S1 S2 S3 I1 I2 I3 I4 Instance members are created in the instance S1 S2 S3 I1 I2 I3 I4 S1 S2 S3 I1 I2 I3 I4 S1 S2 S3 I1 I2 I3 I4 OOAPL

  25. OO fundamental concepts Members: Methods Those can either reside in the class (sharedmethod) or in the instance (instancemethod). There are also abstract methods (e.g. in interfaces) with no code, only calling syntax. Constructors and destructors are methods. OOAPL

  26. OO fundamental concepts Members: Fields Those can either reside in the class (sharedfield) or in the instance (instancefield). They can also be read only. OOAPL

  27. OO fundamental concepts Members: Properties Those are Fieldsimplemented by accessing methods, i.e. PropX←value ⍝ is implemented by SET value They can either reside in the class (sharedproperty) or in the instance (instanceproperty). OOAPL

  28. OO fundamental concepts 4. Message passing This is the (usually asynchronous) sending (often by copy) of a data item to a communication endpoint (process, thread, socket, etc.). In procedural languages like Dyalog, it corresponds to a call made to a routine. OOAPL

  29. OO fundamental concepts 5. Inheritance This is a way to avoid rewriting code by writing general classes and classes that derive from them and inherit their members. This helps achieve reusability, a cornerstone of OO by avoiding to rewrite code and use what already exists. OOAPL

  30. OO fundamental concepts Inheritance We say that a (derived) class is based on another one. All classes (but one) are derived from another one or by System.Object (the default) OOAPL

  31. Great Dane: Dog Instances Dog: Animal Fido (Collie) Animal Classes Collie: Dog Fish: Animal Rex (Collie) OO fundamental concepts Inheritance: Based classes (reusability) A class can be based on another based class. See classCollie based on Dog based on Animal OOAPL

  32. Horse Mule Donkey OO fundamental concepts Inheritance: multiple inheritance A class can be based on several other classes Here class Mule is made out of 2 different classes. OOAPL

  33. OO fundamental concepts Inheritance: simulate multiple inheritance A class can implement several interfaces. Here class Penguin implements 2 different behaviours (interfaces): Fish (swim,eggs) • Penguin: • Swims • Flies not • 1 egg/yr • Croaks Bird (fly,eggs,sing) OOAPL

  34. OO fundamental concepts Inheritance When an instance is created from a class based on another one it inherits all its members automatically. Members can be redefined but subroutines must be specifically set to override base class subroutines. OOAPL

  35. OO fundamental concepts Inheritance: a derived class inherits all the base members C1 memberA memberB C2: C1 memberC memberD OOAPL

  36. OO fundamental concepts Inheritance: a derived class inherits all the base members C1 memberA memberB memberC C2: C1 memberC memberD OOAPL

  37. OO fundamental concepts Inheritance Initialization is performed by the constructor. If a class is derived, the base class' constructor will also be called if there is one. OOAPL

  38. OO fundamental concepts Inheritance Some classes may be sealed to prevent other classes from inheriting them. OOAPL

  39. Override concept Class B Class A M1 M2 M1 M2 M2 ‘I am B’ M2 ‘I am A’ // M1 calls B’s M2: (NEW B).M1 I am B // M1 calls A’s M2: (NEW A).M1 I am A OOAPL

  40. Based classes Class B: A Class A There is no M1 in B so A’s (on which B is based) M1 is used M1 M2 M2 ‘I am A’ M2 ‘I am B’ // M1 calls A’s M2: (NEW B).M1 I am A // M1 calls A’s M2: (NEW A).M1 I am A OOAPL

  41. Based classes Class B: A Class A There is no M1 in B so A’s (on which B is based) M1 is used M1 M2 M2: Override ‘I am B’ M2 ‘I am A’ // M1 calls A’s M2: (NEW B).M1 I am A // M1 calls A’s M2: (NEW A).M1 I am A A:M2 does not allow to be overridden OOAPL

  42. Based classes Class B: A Class A There is no M1 in B so A’s (on which B is based) M1 is used M1 M2: Overridable M2: Override ‘I am B’ M2 ‘I am A’ // M1 calls B’s M2: (NEW B).M1 I am B // M1 calls A’s M2: (NEW A).M1 I am A A:M2 does allow to be overridden AND B wants to override it OOAPL

  43. OO fundamental concepts 6. Encapsulation • Hides the underlying functionality. • It conceals the exact details of how a particular class works from objects (external or internal) that use its code. • This allows to modify the internal implementation of a class without requiring changes to its services (i.e. methods). OOAPL

  44. Example: a car Consider a car: the functionality is hidden, it is encapsulated, you don’t see all the wiring inside, you’re only given specific controls to manipulate the car. OOAPL

  45. Encapsulation (access) To allow access to a member there are various level definitions. For example, to expose a member you must use some syntax that will declare it public. By default, private is the norm. OOAPL

  46. OO fundamental concepts Encapsulation: private A private member cannot be seen from anywhere. Methods can only access it from inside the class. Nested or derived objects can't see it either. M is only seen in the white area sub1 sub2 OOAPL

  47. OO fundamental concepts Encapsulation: protected A protected member can only be seen from within the instance of from nested or derived objects. M is only seen in the white area sub1 sub2 OOAPL

  48. OO fundamental concepts Encapsulation: public A public member can be seen from anywhere. All Methods can access it from anywhere whether it is public shared or public instance. M is seen in all the pale area sub1 sub2 OOAPL

  49. OO fundamental concepts Encapsulation: friend A class declared friend can see the offerer’s private and public members. Class B Class A Private Apv Public Apu Private Bpv Public Bpu Friend A Here A sees B’s members as its own members OOAPL

  50. OO fundamental concepts 7. Polymorphism It is behavior that allows a single definition to be used with different types of data. This is a way to call various sections of code using the same name. Typed languages can tell the difference by looking at the “signature” of the functions (their number of arguments and their types) OOAPL