object oriented programming in apl a new direction dan baronet 2011 v1.24
TRANSCRIPT
![Page 1: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/1.jpg)
Object Oriented Programming in APL
A New Direction
Dan Baronet2011
V1.24
![Page 2: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/2.jpg)
Oct 2011 OOAPL 2
Object Orientation
Dyalog V11 was released in 2006.One of the major features was:
![Page 3: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/3.jpg)
Oct 2011 OOAPL 3
This presentation is based on two things:
1.What is Object Orientation programming?
2.How do I use it in APL?
![Page 4: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/4.jpg)
Oct 2011 OOAPL 7
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.
![Page 5: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/5.jpg)
What is an object?
• An object contains data and code to handle it
• e.g. Name and surname and a function to show them nicely
Oct 2011 OOAPL 8
![Page 6: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/6.jpg)
Concepts of the OO paradigmThe namespace From the beginning APL had this
kind of notion in the form of the workspace
Namespace = Workspace?
Oct 2011 OOAPL 9
![Page 7: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/7.jpg)
Concepts of the OO paradigmThe workspace - It contains everything needed to
run an application
Oct 2011 OOAPL 10
![Page 8: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/8.jpg)
Concepts of the OO paradigmThe workspace - It contains everything needed to
run an application- It can be initialized (via []LX)
Oct 2011 OOAPL 11
![Page 9: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/9.jpg)
Concepts of the OO paradigmThe workspace - It can be initialized (via []LX)- It can be viewed as an object
Oct 2011 OOAPL 12
![Page 10: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/10.jpg)
Concepts of the OO paradigmThe workspace - It can serve as base and be copied
into another workspace
Oct 2011 OOAPL 13
CurrentWorkspace
Inmemory
FilesUtils)COPY FilesUtils FilesUtils
![Page 11: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/11.jpg)
Concepts of the OO paradigmThe workspace - It contains everything needed to
run an application- It can be initialized (via []LX)- It can be viewed as an object- It can serve as base and be copied
into another workspace- It can be modifiedOct 2011 OOAPL 14
![Page 12: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/12.jpg)
Concepts of the OO paradigmThe workspace problems- Name clashing (using )COPY)
Oct 2011 OOAPL 15
Fn1Fn3Var1varx
Fn2varx
)COPY FilesUtils Fn2varx
![Page 13: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/13.jpg)
Concepts of the OO paradigmThe workspace problems- Name clashing (using )COPY)- They cannot be stored as a unit on
file- Only 1 workspace item (variable or
function) per component in APL file- Need to cleanup when finished
Oct 2011 OOAPL 16
![Page 14: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/14.jpg)
Concepts of the OO paradigmThe workspace (solutions)The package or overlay- They group items (vars & fns)
together- They act as a (static) unit- They can be stored on file- They can be copied, like wss, into
existing code and dispersed in itOct 2011 OOAPL 17
![Page 15: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/15.jpg)
Concepts of the OO paradigmThe workspace – packagesThey were only a partial solution to
a more general problem- Name clashing still happened- Clean up was still needed
Oct 2011 OOAPL 18
![Page 16: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/16.jpg)
Concepts of the OO paradigmThe workspace – namespace In the 80s a Dyalog came up with
the concept of namespace, probably from other languages like C++ and Java
Namespaces are like workspaces but nested.
Oct 2011 OOAPL 19
![Page 17: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/17.jpg)
DOS V1.0 APL V1.0File1,ext var1File2.ext fn1Abc.dws var2Note.txt function_2Etc.doc etc
flat structure for files and a workspace
Oct 2011 OOAPL 20
Concepts of the OO paradigm
![Page 18: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/18.jpg)
DOS V2.0 APL V8.0Dir1 ns1
File2.ext var1Abc.dws var2
Note.txt function_2Folder2 namespace2
Etc.doc fnasubf subns
file1 fnafilex opb
Fich.log objxv
Tree (nested) structureOct 2011 OOAPL 21
Concepts of the OO paradigm
![Page 19: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/19.jpg)
Each namespace is similar to a workspace- It contains functions, variables (including
some system variables)- It can contain an entire application- It acts as a unit: it can be put as a single
item on file- It is dynamic: it can be moved into to
execute code, no need to disperse contents
Oct 2011 OOAPL 22
Concepts of the OO paradigm
![Page 20: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/20.jpg)
Each namespace is similar to a workspaceLike a workspace, if you need to tweak
one you take a copy and modify it:Workspace: Namespace:)LOAD myApp newApp<-[]NS []OR
’myApp’A<-3 newApp.A<-3)SAVE newApp
Oct 2011 OOAPL 23
Concepts of the OO paradigm
![Page 21: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/21.jpg)
Reusability This is one of the key conceptsFrom a template you create a new
version possibly modifying it 1 namespace many possibly
different versions
Oct 2011 OOAPL 24
Concepts of the OO paradigm
![Page 22: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/22.jpg)
Classes Classes are very much like
namespaces From a class you create a new version
possibly modifying it 1 class many possibly different
versions
Oct 2011 OOAPL 25
Concepts of the OO paradigm
![Page 23: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/23.jpg)
.Net and OO
.Net is based on namespaces and supports many OO concepts.
Dyalog APL is based on namespaces and supports many OO concepts.
They go well hand in hand together.
Oct 2011 OOAPL 26
![Page 24: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/24.jpg)
Oct 2011 OOAPL 27
.Net and DyalogThis is not new! The ability to use .Net
and create classes was already in V10 but it was more difficult to use.
![Page 25: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/25.jpg)
Oct 2011 OOAPL 34
The Object Oriented paradigm
![Page 26: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/26.jpg)
Oct 2011 OOAPL 35
OO fundamental concepts
The Object Oriented paradigm is based on:
1.Classes & Interfaces2.Instances 3.Members4.Message passing5.Inheritance6.Encapsulation7.Polymorphism
![Page 27: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/27.jpg)
Oct 2011 OOAPL 36
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
![Page 28: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/28.jpg)
Oct 2011 OOAPL 37
OO fundamental concepts
Classes
ex: House blueprint
![Page 29: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/29.jpg)
Oct 2011 OOAPL 38
OO fundamental concepts
ClassesA class can 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.
![Page 30: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/30.jpg)
Oct 2011 OOAPL 39
OO fundamental concepts
InterfacesAn interface describes how to
communicate with an object.Ex: electrical system & electrical
outlets
![Page 31: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/31.jpg)
Oct 2011 OOAPL 40
Interfaces - exampleThe 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.
![Page 32: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/32.jpg)
Oct 2011 OOAPL 41
Interfaces - exampleThis 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);
![Page 33: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/33.jpg)
Oct 2011 OOAPL 42
Interfaces - exampleThis 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);
![Page 34: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/34.jpg)
Oct 2011 OOAPL 43
OO fundamental concepts
2. InstancesThose 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.
![Page 35: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/35.jpg)
Oct 2011 OOAPL 44
OO fundamental concepts
2. InstancesNew House (Blue):
After the creation of the new House the Constructor paints it in blue
![Page 36: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/36.jpg)
Oct 2011 OOAPL 45
OO fundamental concepts
3. MembersThis 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.
![Page 37: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/37.jpg)
Oct 2011 OOAPL 46
OO fundamental concepts
MembersThese can reside inside the class if shared
or in the instance otherwise.Ex: the .Net class DateTime 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
![Page 38: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/38.jpg)
Oct 2011 OOAPL 47
OO fundamental concepts
Shared vs Instance
S1 S2 S3I1 I2 I3 I4
Shared membersremain in the class
S1 S2 S3I1 I2 I3 I4
S1 S2 S3I1 I2 I3 I4
S1 S2 S3I1 I2 I3 I4
Instance members are created in theinstance
![Page 39: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/39.jpg)
Oct 2011 OOAPL 48
OO fundamental conceptsMembers: MethodsThose can either reside in the class
(shared method) or in the instance (instance method).
There are also abstract methods (e.g. in interfaces) with no code, only calling syntax.
Constructors and destructors are methods.
![Page 40: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/40.jpg)
Oct 2011 OOAPL 49
OO fundamental conceptsMembers: FieldsThose can either reside in the class
(shared field) or in the instance (instance field).
They can also be read only.
![Page 41: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/41.jpg)
Oct 2011 OOAPL 50
OO fundamental conceptsMembers: PropertiesThose are Fields implemented by
accessing methods, i.e. PropX←value ⍝ is implemented by
SET value
They can either reside in the class (shared property) or in the instance (instance property).
![Page 42: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/42.jpg)
Oct 2011 OOAPL 51
OO fundamental concepts
4. Message passingThis is the (usually asynchronous)
sending (often by copy) of a data item to a communication endpoint (process, thread, socket, etc.).
In a procedural language (like Dyalog), it corresponds to a call made to a routine.
![Page 43: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/43.jpg)
Oct 2011 OOAPL 52
OO fundamental concepts
5. InheritanceThis 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.
![Page 44: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/44.jpg)
Oct 2011 OOAPL 53
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 from System.Object (the default)
![Page 45: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/45.jpg)
Oct 2011 OOAPL 54
OO fundamental concepts
Inheritance: Based classes (reusability)
A class can be based on another based class. See class Collie based on Dog based on Animal
Animal
Dog: Animal
Rex (Collie)Fish: Animal
Fido (Collie)
Great Dane: Dog
Collie: Dog
Instances
Classes
![Page 46: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/46.jpg)
Oct 2011 OOAPL 55
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.
Horse
Donkey
Mule
![Page 47: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/47.jpg)
Oct 2011 OOAPL 56
OO fundamental concepts
Inheritance: simulate multiple inheritance
A class can implement several interfaces. Here class Penguin implements 2 different behaviours (interfaces):
Fish(swim,eggs)
Bird(fly,eggs,sing)
Penguin:-Swims -Flies not-1 egg/yr-Croaks
![Page 48: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/48.jpg)
Oct 2011 OOAPL 57
OO fundamental concepts
InheritanceWhen 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.
![Page 49: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/49.jpg)
Oct 2011 OOAPL 58
OO fundamental concepts
Inheritance: a derived class inherits all the base members
C1memberAmemberB
C2: C1
memberCmemberD
![Page 50: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/50.jpg)
Oct 2011 OOAPL 59
OO fundamental concepts
Inheritance: a derived class inherits all the base members
C1memberAmemberBmemberC
C2: C1
memberCmemberD
![Page 51: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/51.jpg)
Oct 2011 OOAPL 60
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.
![Page 52: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/52.jpg)
Oct 2011 OOAPL 61
OO fundamental concepts
InheritanceSome classes may be sealed to prevent
other classes from inheriting them.
![Page 53: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/53.jpg)
Oct 2011 OOAPL 62
Override concept
// M1 calls A’s M2:(NEW A).M1
I am A
// M1 calls B’s M2:(NEW B).M1
I am B
M1
M2 M2‘I am A’
M1
M2 M2‘I am B’
Class A Class B
![Page 54: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/54.jpg)
Oct 2011 OOAPL 63
Based classes
// M1 calls A’s M2:(NEW A).M1
I am A
// M1 calls A’s M2:(NEW B).M1
I am A
M1
M2 M2‘I am A’
There is no M1 in Bso A’s (on which B is based) M1 is used
M2‘I am B’
Class A Class B: A
![Page 55: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/55.jpg)
Oct 2011 OOAPL 64
Based classes
M1
M2 M2‘I am A’
There is no M1 in Bso A’s (on which B is based) M1 is used
M2: Override‘I am B’
Class A Class B: A
// M1 calls A’s M2:(NEW A).M1
I am A
// M1 calls A’s M2:(NEW B).M1
I am A
A:M2 does not allow to be overridden
![Page 56: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/56.jpg)
Oct 2011 OOAPL 65
M2: Override‘I am B’
Based classes
M1
M2: M2: Overridable‘I am A’
Class A Class B: A
There is no M1 in Bso A’s (on which B is based) M1 is used
// M1 calls A’s M2:(NEW A).M1
I am A
// M1 calls B’s M2:(NEW B).M1
I am B
A:M2 does allow to be overridden AND B wants to override it
![Page 57: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/57.jpg)
Oct 2011 OOAPL 66
OO fundamental concepts6. 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).
![Page 58: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/58.jpg)
Oct 2011 OOAPL 67
Example: a carConsider 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.
![Page 59: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/59.jpg)
Oct 2011 OOAPL 68
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.
![Page 60: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/60.jpg)
Oct 2011 OOAPL 69
OO fundamental concepts
Encapsulation: privateA 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 onlyseen in thewhite area
sub1
sub2
![Page 61: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/61.jpg)
Oct 2011 OOAPL 70
OO fundamental conceptsEncapsulation: private
Analogy names are private in a D-fn: a←{z←99 ⋄ b ⍵}
b←{z+⍵} a 2VALUE ERRORb[0] b←{z+⍵} ∧
![Page 62: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/62.jpg)
Oct 2011 OOAPL 71
OO fundamental concepts
Encapsulation: protectedA protected member can only be seen
from within the instance of from nested or derived objects.
M is onlyseen in thewhite area
sub1
sub2
![Page 63: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/63.jpg)
Oct 2011 OOAPL 72
OO fundamental conceptsEncapsulation: protected
Analogy names are protected in a T-function: ∇r←a x;z[1] z←99 ⋄ r←b x ∇ ∇r←b x[1] r←z+x ∇ a 2101
![Page 64: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/64.jpg)
Oct 2011 OOAPL 73
OO fundamental concepts
Encapsulation: publicA public member can be seen from
anywhere. All Methods can access it from anywhere whether it is public shared or public instance.
M is seenin all thepale area
sub1
sub2
![Page 65: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/65.jpg)
Oct 2011 OOAPL 74
OO fundamental concepts
Encapsulation: friendA class declared friend can see the
offerer’s private and public members.
Private Bpv
Public Bpu
Friend A
Class A Class B
Private ApvPublic Apu
Here A sees B’s membersas its own members
![Page 66: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/66.jpg)
Oct 2011 OOAPL 75
OO fundamental concepts
7. PolymorphismIt 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)
![Page 67: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/67.jpg)
Oct 2011 OOAPL 76
OO fundamental conceptsPolymorphism example
Foo (integer arg){// fn called with// an integer argPrint 42 //“Int arg”}
Foo (string arg){// fn called with// a string argPrint “String arg”}
Same function name, different code
![Page 68: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/68.jpg)
Oct 2011 OOAPL 77
OO fundamental concepts
PolymorphismA program that accepts multiple definitionsis said to be overloaded.
Basic language example: + is used to add and catenate
Print 2+24
Print ‘aaa’+’bbb’aaabbb
![Page 69: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/69.jpg)
Oct 2011 OOAPL 78
OO fundamental concepts
More concepts
There are many more concepts.Those presented here are the most common ones.
For example, Casting is not an OO concept but it is often used.
![Page 70: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/70.jpg)
Oct 2011 OOAPL 79
OO Concepts Summary• OO is based on +-7 key concepts• Classes are blueprints• Instances are objects created from
classes• They contain members (data & code)• Their access is restricted• The relation between classes is based
on inheritance and/or specific access
![Page 71: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/71.jpg)
Oct 2011 OOAPL 80
End of OO basics
QUESTIONS
?
![Page 72: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/72.jpg)
Oct 2011 OOAPL 81
![Page 73: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/73.jpg)
Oct 2011 OOAPL 82
Implementation• Dyalog follows C# rules closely• The extensions to the language
have been made in following with the :Keywords scheme introduced in the 90s (All new keywords start with ‘:’ )
• Some new system functions have been added and
• Some new system commands
![Page 74: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/74.jpg)
Oct 2011 OOAPL 83
ImplementationLet’s see how Dyalog APL does this:
1.Classes and interfaces2.Instances 3.Members4.Message passing5.Inheritance6.Encapsulation7.Polymorphism
![Page 75: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/75.jpg)
Oct 2011 OOAPL 84
Implementation: classesA class can be defined using the )EDitor
as in )ED ○ MyClass
Upon exiting, MyClass will be defined in the current namespace (e.g. #)
![Page 76: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/76.jpg)
Oct 2011 OOAPL 85
Implementation: classesIn the workspace MyClass appears
as a namespace:⎕NC ‘MyClass’
9)CLASSES
MyClass
![Page 77: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/77.jpg)
Oct 2011 OOAPL 86
Implementation: membersThe members in a class are:• Field: same as a variable• Method: same as a function• Property: looks and feels like a variable
but implemented as a (pair of) functions
All can be either public or private (the default)
![Page 78: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/78.jpg)
Oct 2011 OOAPL 87
Implementation: members
The class to the right shows a few fields (vars) and their access.
The access is the same for methods (fns) and properties.
![Page 79: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/79.jpg)
Oct 2011 OOAPL 88
Implementation: membersThe class to the
right shows a few methods (fns) and their access.
<mon1> is used as a constructor. It MUST be public.
![Page 80: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/80.jpg)
Oct 2011 OOAPL 89
Implementation: membersThe class to the
right shows 2 properties and their access.
The 1st one is private and readonly (it has no set fn)
The 2nd one is public read/write
![Page 81: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/81.jpg)
Oct 2011 OOAPL 90
Classes are like blueprints: they define how things should be.
⎕NEW is a new system function that creates an object using a class as an instance.
Whatever is in the class will appear in the instance as a copy, not a reference
Implementation: instances
![Page 82: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/82.jpg)
Oct 2011 OOAPL 91
⎕NEW accepts a one or two elements argument.
The 1st one is the class (not in quotes)The 2nd, if present, is what to
start/initialize the instance with. It can only be supplied if the class allows it.
Implementation: NEW⎕
![Page 83: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/83.jpg)
Oct 2011 OOAPL 92
Example:myInstance← ⎕NEW MyClass
The display form includes [brackets] by defaultmyInstance
#.[MyClass]It is another type of namespace⎕NC ‘myInstance’
9
Implementation: NEW⎕
![Page 84: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/84.jpg)
Oct 2011 OOAPL 93
⎕DF allows to change the default Display Form of a namespace.
Example:⎕←obj←⎕NEW MyClass
#.[MyClass]obj.⎕DF ‘OO class’
objOO class
Implementation: DF⎕
![Page 85: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/85.jpg)
Oct 2011 OOAPL 94
Classes may require something to initialize the instance. Ex:)ED ○ Animal
Implementation: constructors
Here an animal musthave at least twocharacteristics:1.# of legs2. the sounds it makes
![Page 86: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/86.jpg)
Oct 2011 OOAPL 95
Several instances can be made up of the same Animal class:
Implementation: constructors
After running thisfunction in a cleanspace we have:
)obsfish monkey snake tiger Animal
⎕NC ‘animals’ this is a variable holding 4 instances⍝2
animals.Nlegs how many legs for each animal?⍝4 0 0 2
![Page 87: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/87.jpg)
Oct 2011 OOAPL 96
Implementation: message passing
In APL this merely consists in calling the methods directly either from the instance or from the class if shared, e.g.
Myclass.sharedFn argsor
myInstance.Fn args args if required⍝
![Page 88: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/88.jpg)
Oct 2011 OOAPL 97
Implementation: inheritanceInheritance: Based classes
A class can be based on another based class. See class Collie based on Dog based on Animal
Animal
Dog: Animal
Rex (Collie)Fish: Animal
Fido (Collie)
Collie: Dog
Instances
Classes
Great Dane: Dog
![Page 89: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/89.jpg)
Oct 2011 OOAPL 98
Example: a bird’s generic description is ‘plain Bird’
Birdy← NEW Bird⎕
Birdy.Desc
plain Bird
Implementation: inheritance
Bird Birdy (Bird)
When Birdy is created, Bird’s constructor is called
![Page 90: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/90.jpg)
Oct 2011 OOAPL 99
If a base class has a constructor it must be called.
Base constructors are called when the :Implements constructor statement is reached.
If several classes are based one on another their constructors’ call are made in the same fashion.
Implementation: inheritance
![Page 91: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/91.jpg)
Oct 2011 OOAPL 100
Example: a parrot is a bird
Coco← NEW Parrot⎕Coco.Desc
Not a plain Bird, a Parrot
Implementation: inheritance
Bird
Birdy (Bird)
Parrot: Bird Coco (Parrot)
![Page 92: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/92.jpg)
Oct 2011 OOAPL 101
We can create classes and instances at all levels
Implementation: inheritance
Bird
Parrot: Bird
Tweety (Macaw)
Macaw: Parrot
InstancesClasses
Birdy (Bird)
Coco (Parrot)
![Page 93: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/93.jpg)
Oct 2011 OOAPL 102
Implementation: inheritanceInheritance: constructors with arguments
Using the animal kingdom as example.
Animal
Dog: Animal
Rex (Collie)Fish: Animal
Fido (Collie)
Big Dane: Dog
Collie: Dog
Instances
Classes
Great Dane: Dog
![Page 94: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/94.jpg)
Oct 2011 OOAPL 103
Implementation: inheritance
pet← NEW Collie ‘Rex’⎕pet. nl -2⎕
Name Nlegs Sounds Typepet.(Name Type)
Rex canis familiaris
![Page 95: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/95.jpg)
Oct 2011 OOAPL 104
Implementation: inheritance
pet← NEW Collie ‘Rex’⎕pet. nl -2⎕
Name Nlegs Sounds Typepet.(Name Type)
Rex canis familiaris
Let’s go over this again slowly…
![Page 96: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/96.jpg)
Oct 2011 OOAPL 105
Implementation: inheritance
pet← NEW Collie ‘Rex’⎕pet. nl -2⎕
Name Nlegs Sounds Typepet.(Name Type)
Rex canis familiaris
![Page 97: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/97.jpg)
Oct 2011 OOAPL 106
Implementation: Encapsulation
Conceals the exact details of how a particular class works from objects that use its code.
To allow access to a member there are various access level definitions.
![Page 98: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/98.jpg)
Oct 2011 OOAPL 107
To expose a member you must make it public
For a property or a method (function) you must use the :Access public Statement
For a field (var) you must use this statement::Field public MyField
Implementation: Encapsulation
![Page 99: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/99.jpg)
Oct 2011 OOAPL 108
The class to the right shows a few fields (vars) and their access.
The access is the same for methods (fns) and properties.
Implementation: Encapsulation
![Page 100: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/100.jpg)
Oct 2011 OOAPL 109
Shared vs instanceEach instance gets a copy of each
instance member.If a member is shared it is kept in
the class for all instances to see (and use)
Implementation: Encapsulation
![Page 101: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/101.jpg)
Oct 2011 OOAPL 110
Shared vs instance
Example: keepCountHere each instance has its own private
ID and public nameThe class keeps track of the number of
instances created privately.
Implementation: Encapsulation
![Page 102: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/102.jpg)
Oct 2011 OOAPL 111
Shared vs instance
keepCount:Count←0,1,…
MyID MyName
'Count' remainsin the class
CountMyID=1
MyName=XYZ1
CountMyID=2
MyName=XYZ2
Instance members are created in theinstance
Implementation: Encapsulation
CountMyID=3
MyName=XYZ3
![Page 103: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/103.jpg)
Oct 2011 OOAPL 112
Shared vs instance in1← new keepCount⎕
I am XYZ1 in2← new keepCount⎕I am XYZ2 in3← new keepCount⎕I am XYZ3 keepCount.Count3 keepCount. nl-2⎕ Count in2. nl-2⎕ Count MyName in1.Count3
Implementation: Encapsulation
![Page 104: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/104.jpg)
Oct 2011 OOAPL 113
In APL a function CANNOT have different definitions using a single name.
OTOH, a constructor MAY have several definitions.
Since APL is typeless the only way to tell which function to use is by the NUMBER of arguments, from 0 (niladic) to any number.
Implementation: Polymorphism
![Page 105: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/105.jpg)
:Class manyCtors ∇ general x ⍝ This ctor any arg ⍝ It can be anything :Implements constructor :Access public ∇ ∇ nil ⍝ This ctor takes no arg :Implements constructor :Access public ∇ ∇ mon1(a1) ⍝ This ctor takes 1 arg ⍝ It MUST be a vector :Implements constructor :Access public ∇ ∇ mon5(a1 a2 a3 a4 a5) ⍝ This ctor takes 5 arg ⍝ It MUST be exactly 5 :Implements constructor :Access public ∇:EndClass
Oct 2011 OOAPL 114
Implementation: Polymorphism
Example:
Depending on the number of arguments, the proper fn will be used.
![Page 106: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/106.jpg)
Oct 2011 OOAPL 115
Implementation summary
• Dyalog follows C# rules closely• )ED to edit classes• classes are much like namespaces• their members (vars & fns) can all be
accessed using public access• ⎕NEW makes an instance• classes can be based on another one• they use constructors to initialize them
![Page 107: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/107.jpg)
Oct 2011 OOAPL 116
End of implementation
There are MANY other things related to OO that have been implemented but are out of the scope of this presentation.
To find more visit Dyalog’s website at www.dyalog.com and grab the free documentation.
![Page 108: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/108.jpg)
Oct 2011 OOAPL 117
End of implementation
QUESTIONS
?
![Page 109: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/109.jpg)
Oct 2011 OOAPL 118
Exercises
Choice of exercises:1.Simple numbers2.Complex numbers3.Parser4.Pre formatted forms5.Graphical objects6.Telnet client The end
![Page 110: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/110.jpg)
Oct 2011 OOAPL 119
Exercises – Preformatted form
Deriving from Dyalog GUI classes.
Besides classes you can write classes derived from
- Forms- OLE servers/controls- .Net classes
![Page 111: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/111.jpg)
Oct 2011 OOAPL 120
Exercises – Preformatted form
Ex:f1← new ⎕ 'form' (('caption' 'zzz')
('size'(100 200)))XL← new ⎕ 'oleclient' (⊂'classname'
'Excel.application')⎕using←''present←System.DateTime.Now
![Page 112: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/112.jpg)
Oct 2011 OOAPL 121
Exercises – Preformatted form
We need to write a class that will create a form, just like
f1← new ⎕ 'form' (('caption' 'zzz')('size'(100 200)))
But with 2 buttons already setup for SAVE and QUIT so we can do
f1← new ⎕ myForm ((…))
![Page 113: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/113.jpg)
Oct 2011 OOAPL 122
Exercises – Preformatted form
![Page 114: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/114.jpg)
Oct 2011 OOAPL 123
Exercises – Preformatted form
f1← new Dialog(‘pref form' (100 500) )⎕ ⍬
![Page 115: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/115.jpg)
Oct 2011 OOAPL 124
Exercises – Preformatted form
This class creates an edit object:
![Page 116: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/116.jpg)
Oct 2011 OOAPL 125
Exercises – Preformatted form
f1← new Dialog('pref form' (200 300) )⎕ ⍬f1.fn←f1. new EditField ('First name:' '' (10 60) ( 100) )⎕ ⍬ ⍬f1.ln←f1. new EditField ('Last name:' '' (38 60) ( 100) )⎕ ⍬ ⍬f1.ad←f1. new EditField ('Address:' '' (66 60) (90 230) ( 'style' 'multi'))⎕ ⊂f1.(fn ln ad).Text←'Jeremy' 'Cricket'('TopFlower' 'last field')
![Page 117: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/117.jpg)
Oct 2011 OOAPL 126
Exercises
Improvements:- Use an interface
Back to exercise choices
![Page 118: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/118.jpg)
Oct 2011 OOAPL 127
Exercises – Complex numbersCreate a class to do complex math.
A rational or “normal number” is a decimal number that can be viewed on the line of “real” numbers:
-10 -5 0 +5.3 +10
![Page 119: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/119.jpg)
Oct 2011 OOAPL 128
ExercisesA complex numberis a number in thecomplex plane:2 rational numbersare used torepresent it.
-4 -2 0 +2 +4 +6
+2
+4
+6
The number 3J4
![Page 120: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/120.jpg)
Oct 2011 OOAPL 129
Exercises
Create a class to do complex math.
The class will contain 2 real numbers and 3 methods to perform Add, Multiply and return the Reciprocal of a complex number.
![Page 121: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/121.jpg)
Oct 2011 OOAPL 130
Exercises
Create a class to do complex math.
The methods to perform Add and Multiply will be shared (in the class) methods and the Reciprocal will be an instance method.
![Page 122: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/122.jpg)
Oct 2011 OOAPL 131
ExercisesComplex math definition:
C = a + bi, where i = ¯1 * 0.5
Engineers use the J notation, like this:3j4 to denote the complex number with
a real value of 3 and an imaginary value of 4.
![Page 123: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/123.jpg)
Oct 2011 OOAPL 132
Exercises
Complex math definition:
Addition of 2 complex numbers
C1 + C2 = (a1+a2) + (b1+b2) i
![Page 124: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/124.jpg)
Oct 2011 OOAPL 133
Exercises
Complex math definition:Multiplication of 2 complex numbers C1 a1 b1i x C2 a2 b2i=(a1×a2) + (b1i × b2i) + (a1×b2i) +
(b1i×a2)=((a1×a2) - (b1 × b2)) + ((a1×b2) +
(b1×a2))i
![Page 125: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/125.jpg)
Oct 2011 OOAPL 134
Exercises
Complex math definition:Reciprocal of a complex number is 1÷C such that C × R = 1 = 1 + 0 ireal = ((a1×a2) - (b1 × b2)) = 1img = ((a1×b2) + (b1×a2)) i = 0Solving:Ra = Ca÷((Ca×Ca)-(Cb×Cb))Rb = -Cb÷((Ca×Ca)-(Cb×Cb))
![Page 126: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/126.jpg)
Oct 2011 OOAPL 135
Exercises
The first thing to do:)ED ○ complex
![Page 127: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/127.jpg)
Oct 2011 OOAPL 136
Exercises
Enter the fields:
![Page 128: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/128.jpg)
Oct 2011 OOAPL 137
ExercisesEnter the constructor, it takes 2
numbers as arguments, it merely sets the 2 fields:
![Page 129: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/129.jpg)
Oct 2011 OOAPL 138
ExercisesTest
c1← NEW complex (3,4)⎕c1.real
To ease creation of complex numbers:
j←{ new complex (⍺ ⍵)}⎕c2←5 j 12
c2.img
![Page 130: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/130.jpg)
Oct 2011 OOAPL 139
ExercisesTest
Typing c1 does not provide useful information.
It would be nice if we could find what the numbers are without having to use
c1.(real img)
![Page 131: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/131.jpg)
Oct 2011 OOAPL 140
Display form
• The display form can be improved• We can use ⎕DF to show a
different string, e.g. 3J4• Try c1. DF '3J4'⎕ (or whatever)• Try including the setting in the
constructor
![Page 132: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/132.jpg)
Oct 2011 OOAPL 141
Display form
• assignment does not change the display
Try c1.img←22 and verify that ←c1⎕ has the same display form
• use properties to change that• create 2 properties• use DF to change the display in the ⎕
<set> function
![Page 133: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/133.jpg)
Oct 2011 OOAPL 142
Exercises
Enter the methods, first, <plus>:
![Page 134: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/134.jpg)
Oct 2011 OOAPL 143
Exercises
Test <plus>: cs←c1 complex.plus c2 cs.real ?
![Page 135: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/135.jpg)
Oct 2011 OOAPL 144
Exercises
Enter the methods, 2nd, <times>:
![Page 136: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/136.jpg)
Oct 2011 OOAPL 145
Exercises
Test <times>: ct←c1 complex.times c2 ct.real ?
![Page 137: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/137.jpg)
Oct 2011 OOAPL 146
Exercises
Enter the methods, 3rd, <reciprocal>:
![Page 138: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/138.jpg)
Oct 2011 OOAPL 147
Exercises
Try it!complex.plus/4⍴c1 if funny display, ⍝
use dj←{⍵.(real img)}dj complex.plus/4⍴c1 12 16?⍝dj complex.times\3⍴c1 3 4 ¯7 24...⍝dj complex.plus/c1 complex.times¨4⍴c1
![Page 139: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/139.jpg)
Oct 2011 OOAPL 148
Exercises
Try it!dj c1.reciprocal
⍝ c1 ÷ c1 or c1 × ÷c1:dj c1 complex.times c1.reciprocaldj c2 complex.times c1.reciprocal2.52 0.64?
![Page 140: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/140.jpg)
Oct 2011 OOAPL 149
Exercises
Improvements:- other functions (magnitude?)- accept a single (real) number
Back to exercise choices
![Page 141: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/141.jpg)
Oct 2011 OOAPL 150
Exercises
Create a class to do simple math with 2 integers representing fractions.
Using a Numerator & a DenominatorEx.: 3 / 7th
![Page 142: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/142.jpg)
Oct 2011 OOAPL 151
Exercises
Create a class to do simple math.
The class will contain 2 integer numbers and 3 methods to perform Add, Multiply and return the Reciprocal of a pair of simple numbers representing a fraction.
![Page 143: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/143.jpg)
Oct 2011 OOAPL 152
Exercises
Create a class to do simple math.
The methods to perform Add and Multiply will be shared (in the class) methods and the Reciprocal will be an instance method.
![Page 144: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/144.jpg)
Oct 2011 OOAPL 153
ExercisesSimple math definition:- The 1st number will be called NN- The 2nd number will be called DD
They will be stored in a vector (2 elem)There will be two properties to access
them individually.
![Page 145: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/145.jpg)
Oct 2011 OOAPL 154
ExercisesSimple math definition:Addition of 2 simple numbers (fractions)S1 + S2 = (n1 x d2 + n2 x d1) , (d1 x d2)Multiplication of 2 simple numbersS1 x S2 = (n1 x n2) , (d1 x d2)Reciprocal of a simple number÷S1 = (d1) , (n1)
![Page 146: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/146.jpg)
Oct 2011 OOAPL 155
Exercises
The first thing to do:)ED ○ simple
![Page 147: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/147.jpg)
Oct 2011 OOAPL 156
Exercises
Enter the field:
![Page 148: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/148.jpg)
Oct 2011 OOAPL 157
ExercisesEnter the constructor, it takes 2
numbers as arguments, it merely sets the field:
![Page 149: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/149.jpg)
Oct 2011 OOAPL 158
ExercisesAt this point the class does not do
anything.‘Numbers’ is private and cannot be
accessed from outside.Let’s create properties to access it.
![Page 150: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/150.jpg)
Oct 2011 OOAPL 159
ExercisesEnter the properties, they take 1
number to set a value in the field:
N
![Page 151: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/151.jpg)
Oct 2011 OOAPL 160
ExercisesTest
s1← NEW simple(3,4)⎕s1.N
To ease creation of simple numbers:
s←{ new simple (⍺ ⍵)}⎕s2←5 s 12
s2.N
![Page 152: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/152.jpg)
Now enter the second property…
Oct 2011 OOAPL 161
Exercises
![Page 153: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/153.jpg)
Oct 2011 OOAPL 162
ExercisesTest
Typing s1 does not provide useful information.
It would be nice if we could find what the numbers are without having to use
s1.(N D)
![Page 154: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/154.jpg)
Oct 2011 OOAPL 163
Display form
• The display form can be improved• We can use ⎕DF to show a
different string, e.g. 3/4• Try s1. DF '3/4'⎕• Try including the setting in the
constructor
![Page 155: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/155.jpg)
Oct 2011 OOAPL 164
Display form
• assignment does not change the display
Try s1.N←22 and verify that ←s1⎕ has the same display form it had before
• use the properties’ code to change that
• use DF to change the display in the ⎕<set> function
![Page 156: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/156.jpg)
Oct 2011 OOAPL 165
Exercises
Enter the methods, first, <plus>:
![Page 157: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/157.jpg)
Oct 2011 OOAPL 166
Exercises
Test <plus>: cs←s1 simple.plus s2 cs.N ?
![Page 158: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/158.jpg)
Oct 2011 OOAPL 167
Exercises
Enter the 2nd method, <times>:
![Page 159: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/159.jpg)
Oct 2011 OOAPL 168
Exercises
Test <times>: ct←s1 simple.times s2 ct.N ?
![Page 160: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/160.jpg)
Oct 2011 OOAPL 169
Exercises
Enter the methods, 3rd, <reciprocal>:
![Page 161: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/161.jpg)
Oct 2011 OOAPL 170
Exercises
Try it!simple.plus/4⍴s1 if funny display, ⍝
use dj←{⍵.(N ‘/’ D)}dj simple.plus/4⍴s1 22/1?⍝dj simple.times\3⍴s1 11/2 121/4…⍝dj simple.plus/s1 simple.times¨4⍴s1
![Page 162: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/162.jpg)
Oct 2011 OOAPL 171
Exercises
Try it!dj s1.reciprocal
⍝ s1 ÷ s1 or s1 × ÷s1:dj s1 simple.times s1.reciprocaldj s2 simple.times s1.reciprocal5/66?
![Page 163: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/163.jpg)
Oct 2011 OOAPL 172
Exercises
Improvements:- other functions (square, abs?)- accept a single (real) number- make this into a complex number
class
Back to exercise choices
![Page 164: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/164.jpg)
Oct 2011 OOAPL 173
There are many advantages to using OO languages over procedural languages:
• ease of modification• reduced maintenance costs• easier to model• can speed up development time• etc.
Conclusion
![Page 165: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/165.jpg)
Oct 2011 OOAPL 174
OO languages make abstraction easyThey encourage architectures with
elaborate layers. They can be good when the problem
domain is truly complex and demands a lot of abstraction.
Conclusion
![Page 166: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/166.jpg)
Oct 2011 OOAPL 175
• All OO languages show some tendency to suck programmers into the trap of excessive layering
• Object frameworks and object browsers are not a substitute for good design or documentation
Careful!
![Page 167: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/167.jpg)
Oct 2011 OOAPL 176
• Too many layers destroy transparency: It becomes too difficult to see down through them and mentally model what the code is actually doing.
• The Rules of Simplicity, Clarity, and Transparency can get violated wholesale, and the result is code full of obscure bugs and continuing maintenance problems.
Careful!
![Page 168: Object Oriented Programming in APL A New Direction Dan Baronet 2011 V1.24](https://reader035.vdocuments.us/reader035/viewer/2022081516/56649eb05503460f94bb5826/html5/thumbnails/168.jpg)
Oct 2011 OOAPL 177
Buyer beware!
Careful!