1.52k likes | 1.53k Views
Learn about the benefits of object-oriented programming (OOAPL) and how to use it in APL. Discover how OOAPL simplifies code reuse, improves project management, and enhances compatibility with .Net.
E N D
Object Oriented Programming in APL A New Direction Dan Baronet 2008 V1.20
Dyalog V11 was released in 2006. One of the major features was: Object Orientation OOAPL
This presentation is based on two things: • Why should I know about Object Orientation programming? • How do I use it in APL? OOAPL
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
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
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
d a t a code The evolution of data In traditional (procedural) systems, data is separated from code: OOAPL
d a t a code The evolution of data This is the same in traditional APL systems, data is separated from code: OOAPL
The evolution of data In OO systems, data is more tightly coupled with code: private code d a t a OOAPL
The evolution of data Same thing in OO APL systems: private code d a t a OOAPL
The Object Oriented paradigm Definitions OOAPL
OO fundamental concepts The Object Oriented paradigm is based on: • Classes & Interfaces • Instances • Members • Message passing • Inheritance • Encapsulation • Polymorphism OOAPL
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
OO fundamental concepts Classes ex: House blueprint OOAPL
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
OO fundamental concepts Interfaces An interface describes how to communicate with an object. Ex: electrical system & electrical outlets OOAPL
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
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
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
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
OO fundamental concepts 2. Instances New House (Blue): After the creation of the new House the Constructor paints it in blue OOAPL
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
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
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
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
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
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
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
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
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
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
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
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
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
OO fundamental concepts Inheritance: a derived class inherits all the base members C1 memberA memberB C2: C1 memberC memberD OOAPL
OO fundamental concepts Inheritance: a derived class inherits all the base members C1 memberA memberB memberC C2: C1 memberC memberD OOAPL
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
OO fundamental concepts Inheritance Some classes may be sealed to prevent other classes from inheriting them. OOAPL
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
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
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
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
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
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
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
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
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
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
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
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