Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in APL A New Direction Dan Baronet 2009 V1.22.

Similar presentations


Presentation on theme: "Object Oriented Programming in APL A New Direction Dan Baronet 2009 V1.22."— Presentation transcript:

1 Object Oriented Programming in APL A New Direction Dan Baronet 2009 V1.22

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

3 Oct 2008OOAPL3 This presentation is based on two things: 1.What is Object Orientation programming? 2.How do I use it in APL?

4 Oct 2008OOAPL4 Coverage I.History of OO II.Definitions III.The APL way

5 Oct 2008OOAPL5 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

6 Oct 2008OOAPL6 History OO is not new, it’s been around for a while. Pretty much the same story as APL 50s: conception 60s: 1 st implementation (SIMULA compiler) 70s: 1 st user base, Smalltalk compiler 80s: starts to be used more seriously 90s: popularity grows (≠APL) thanks to C++ and Java & the Internet

7 Oct 2008OOAPL7 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.

8 Concepts of the OO paradigm The namespace From the beginning APL had this kind of notion in the form of the workspace Namespace = Workspace? Oct 2008OOAPL8

9 Concepts of the OO paradigm The workspace -It contains everything needed to run an application Oct 2008OOAPL9

10 Concepts of the OO paradigm The workspace -It contains everything needed to run an application -It can be initialized (via []LX) Oct 2008OOAPL10

11 Concepts of the OO paradigm The workspace -It can be initialized (via []LX) -It can be viewed as an object Oct 2008OOAPL11

12 Concepts of the OO paradigm The workspace -It can serve as base and be copied into another workspace Oct 2008OOAPL12 Current Workspace In memory FilesUtils )COPY FilesUtils FilesUtils

13 Concepts of the OO paradigm The 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 modified Oct 2008OOAPL13

14 Concepts of the OO paradigm The workspace problems -Name clashing (using )COPY) Oct 2008OOAPL14 Fn1 Fn3 Var1 varx Fn2 varx )COPY FilesUtils Fn2 varx

15 Concepts of the OO paradigm The workspace problems -Name clashing (using )COPY) -They cannot be stored as a unit on file -Only 1 workspace item per component (variable or function) -Need to cleanup when finished Oct 2008OOAPL15

16 Concepts of the OO paradigm The 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 it Oct 2008OOAPL16

17 Concepts of the OO paradigm The workspace – packages They were only a partial solution to a more general problem -Name clashing still happened -Clean up was still needed Oct 2008OOAPL17

18 Concepts of the OO paradigm The workspace – namespace In the 80s a company came up with the concept of namespace, probably from other languages like C++ and Java Namespaces are like workspaces but nested. Oct 2008OOAPL18

19 DOS V1.0APL V1.0 File1,extvar1 File2.extfn1 Abc.dwsvar2 Note.txtfunction_2 Etc.docetc flat structure Oct 2008OOAPL19 Concepts of the OO paradigm

20 DOS V2.0APL V8.0 Dir1ns1 File2.extvar1 Abc.dwsvar2 Note.txtfunction_2 Folder2namespace2 Etc.docfna subfsubns file1fna filexopb Fich.logobjxv Tree (nested) structure Oct 2008OOAPL20 Concepts of the OO paradigm

21 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 2008OOAPL21 Concepts of the OO paradigm

22 Each namespace is similar to a workspace Like 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 2008OOAPL22 Concepts of the OO paradigm

23 Reusability This is one of the key concepts From a template you create a new version possibly modifying it 1 namespace many possibly different versions Oct 2008OOAPL23 Concepts of the OO paradigm

24 Reusability Classes are very much like namespaces From a class you create a new version possibly modifying it 1 class many possibly different versions Oct 2008OOAPL24 Concepts of the OO paradigm

25 .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 2008OOAPL25

26 Oct 2008OOAPL26.Net and Dyalog This is not new! The ability to use.Net and create classes was already in V10 but it was more difficult to use.

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

28 Oct 2008OOAPL28 Data in Dyalog APL (DYW) In DYW all these objects exist. You can often simulate one by another. For ex: a table of names & values can represent a namespace of variables. DYW can also store the ⎕ OR of a namespace.

29 Oct 2008OOAPL29 The evolution of data In traditional (procedural) systems, data is separated from code: datadata code

30 Oct 2008OOAPL30 The evolution of data This is the same in traditional APL systems, data is separated from code: datadata code

31 Oct 2008OOAPL31 The evolution of data In OO systems, data is more tightly coupled with code: private code datadata

32 Oct 2008OOAPL32 The evolution of data Same thing in OO APL systems: private code datadata

33 Oct 2008OOAPL33 The Object Oriented paradigm

34 Oct 2008OOAPL34 OO fundamental concepts The Object Oriented paradigm is based on: 1.Classes & Interfaces 2.Instances 3.Members 4.Message passing 5.Inheritance 6.Encapsulation 7.Polymorphism

35 Oct 2008OOAPL35 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

36 Oct 2008OOAPL36 OO fundamental concepts Classes ex: House blueprint

37 Oct 2008OOAPL37 OO fundamental concepts Classes A 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.

38 Oct 2008OOAPL38 OO fundamental concepts Interfaces An interface describes how to communicate with an object. Ex: electrical system & electrical outlets

39 Oct 2008OOAPL39 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.

40 Oct 2008OOAPL40 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);

41 Oct 2008OOAPL41 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);

42 Oct 2008OOAPL42 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.

43 Oct 2008OOAPL43 OO fundamental concepts 2. Instances New House (Blue): After the creation of the new House the Constructor paints it in blue

44 Oct 2008OOAPL44 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.

45 Oct 2008OOAPL45 OO fundamental concepts Members These can reside inside the class if shared or in the instance. 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

46 Oct 2008OOAPL46 OO fundamental concepts Shared vs Instance S1 S2 S3 I1 I2 I3 I4 Shared members remain in the class S1 S2 S3 I1 I2 I3 I4 S1 S2 S3 I1 I2 I3 I4 S1 S2 S3 I1 I2 I3 I4 Instance members are created in the instance

47 Oct 2008OOAPL47 OO fundamental concepts Members: Methods Those 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.

48 Oct 2008OOAPL48 OO fundamental concepts Members: Fields Those can either reside in the class (shared field) or in the instance (instance field). They can also be read only.

49 Oct 2008OOAPL49 OO fundamental concepts Members: Properties Those 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).

50 Oct 2008OOAPL50 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.

51 Oct 2008OOAPL51 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.

52 Oct 2008OOAPL52 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)

53 Oct 2008OOAPL53 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

54 Oct 2008OOAPL54 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

55 Oct 2008OOAPL55 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

56 Oct 2008OOAPL56 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.

57 Oct 2008OOAPL57 OO fundamental concepts Inheritance: a derived class inherits all the base members C1 memberA memberB C2: C1 memberC memberD

58 Oct 2008OOAPL58 OO fundamental concepts Inheritance: a derived class inherits all the base members C1 memberA memberB memberC C2: C1 memberC memberD

59 Oct 2008OOAPL59 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.

60 Oct 2008OOAPL60 OO fundamental concepts Inheritance Some classes may be sealed to prevent other classes from inheriting them.

61 Oct 2008OOAPL61 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 ‘I am A’ M1 M2 ‘I am B’ Class A Class B

62 Oct 2008OOAPL62 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 ‘I am A’ There is no M1 in B so A’s (on which B is based) M1 is used M2 ‘I am B’ Class A Class B: A

63 Oct 2008OOAPL63 Based classes M1 M2 ‘I am A’ There is no M1 in B so 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

64 Oct 2008OOAPL64 M2: Override ‘I am B’ Based classes M1 M2: Overridable M2 ‘I am A’ Class A Class B: A There is no M1 in B so 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

65 Oct 2008OOAPL65 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).

66 Oct 2008OOAPL66 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.

67 Oct 2008OOAPL67 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.

68 Oct 2008OOAPL68 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

69 Oct 2008OOAPL69 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

70 Oct 2008OOAPL70 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

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

72 Oct 2008OOAPL72 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)

73 Oct 2008OOAPL73 OO fundamental concepts Polymorphism example Foo (integer arg) { // fn called with // an integer arg Print 42 //“Int arg” } Foo (string arg) { // fn called with // a string arg Print “String arg” } Same function name, different code

74 Oct 2008OOAPL74 OO fundamental concepts Polymorphism A program that accepts multiple definitions is said to be overloaded. Basic language example: + is used to add and catenate Print 2+2 4 Print ‘aaa’+’bbb’ aaabbb

75 Oct 2008OOAPL75 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.

76 Oct 2008OOAPL76 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

77 Oct 2008OOAPL77 End of OO basics QUESTIONS ?

78 Oct 2008OOAPL78

79 Oct 2008OOAPL79 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

80 Oct 2008OOAPL80 Implementation Let’s see how Dyalog APL does this: 1.Classes and interfaces 2.Instances 3.Members 4.Message passing 5.Inheritance 6.Encapsulation 7.Polymorphism

81 Oct 2008OOAPL81 Implementation: classes A class can be defined using the )EDitor as in )ED ○ MyClass Upon exiting, MyClass will be defined in the current namespace (e.g. #)

82 Oct 2008OOAPL82 Implementation: classes In the workspace MyClass appears as a namespace: ⎕ NC ‘MyClass’ 9 )CLASSES MyClass

83 Oct 2008OOAPL83 Implementation: members The 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)

84 Oct 2008OOAPL84 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.

85 Oct 2008OOAPL85 Implementation: members The class to the right shows a few methods (fns) and their access. is also used as a constructor. It MUST be public.

86 Oct 2008OOAPL86 Implementation: members The class to the right shows 2 properties and their access. The 1 st one is private and readonly (it has no set fn) The 2 nd one is public read/write

87 Oct 2008OOAPL87 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

88 Oct 2008OOAPL88 ⎕ NEW accepts a one or two elements argument. The 1 st one is the class (not in quotes) The 2 nd, if present, is what to start/initialize the instance with. It can only be supplied if the class allows it. Implementation: ⎕ NEW

89 Oct 2008OOAPL89 Example: myInstance← ⎕ NEW MyClass The display form includes [brackets] by default myInstance #.[MyClass] It is another type of namespace ⎕ NC ‘myInstance’ 9 Implementation: ⎕ NEW

90 Oct 2008OOAPL90 ⎕ DF allows to change the default Display Form of a namespace. Example: ⎕ ←obj← ⎕ NEW MyClass #.[MyClass] obj. ⎕ DF ‘OO class’ obj OO class Implementation: ⎕ DF

91 Oct 2008OOAPL91 Classes may require something to initialize the instance. Ex: )ED ○ Animal Implementation: constructors Here an animal must be have at least two characteristics: 1.# of legs 2.the sounds it makes

92 Oct 2008OOAPL92 Several instances can be made up of the same Animal class: Implementation: constructors After running this function in a clean space we have: )obs fish 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

93 Oct 2008OOAPL93 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 args or myInstance.Fn args ⍝ args if required

94 Oct 2008OOAPL94 Implementation: inheritance Inheritance: 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

95 Oct 2008OOAPL95 Example: a bird’s generic description is ‘plain Bird’ Birdy← ⎕ NEW Bird Birdy.Desc plain Bird Implementation: inheritance BirdBirdy (Bird) When Birdy is created, Bird’s constructor is called

96 Oct 2008OOAPL96 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

97 Oct 2008OOAPL97 Example: a parrot is a bird Coco← ⎕ NEW Parrot Coco.Desc Not a plain Bird, a Parrot Implementation: inheritance Bird Birdy (Bird) Parrot: BirdCoco (Parrot)

98 Oct 2008OOAPL98 We can create classes and instances at all levels Implementation: inheritance Bird Parrot: Bird Tweety (Macaw) Macaw: Parrot Instances Classes Birdy (Bird) Coco (Parrot)

99 Oct 2008OOAPL99 Implementation: inheritance Inheritance: 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

100 Oct 2008OOAPL100 Implementation: inheritance pet← ⎕ NEW Collie ‘Rex’ pet. ⎕ nl -2 Name Nlegs Sounds Type pet.(Name Type) Rex canis familiaris

101 Oct 2008OOAPL101 Implementation: inheritance pet← ⎕ NEW Collie ‘Rex’ pet. ⎕ nl -2 Name Nlegs Sounds Type pet.(Name Type) Rex canis familiaris Let’s go over this again slowly…

102 Oct 2008OOAPL102 Implementation: inheritance pet← ⎕ NEW Collie ‘Rex’ pet. ⎕ nl -2 Name Nlegs Sounds Type pet.(Name Type) Rex canis familiaris

103 Oct 2008OOAPL103 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.

104 Oct 2008OOAPL104 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

105 Oct 2008OOAPL105 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

106 Oct 2008OOAPL106 Shared vs instance Each 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

107 Oct 2008OOAPL107 Shared vs instance Example: keepCount Here each instance has its own private ID and public name The class keeps track of the number of instances created privately. Implementation: Encapsulation

108 Oct 2008OOAPL108 Shared vs instance keepCount: Count←0,1,… MyID MyName 'Count' remains in the class Count MyID=1 MyName=XYZ1 Count MyID=2 MyName=XYZ2 Instance members are created in the instance Implementation: Encapsulation Count MyID=3 MyName=XYZ3

109 Oct 2008OOAPL109 Shared vs instance in1← ⎕ new keepCount I am XYZ1 in2← ⎕ new keepCount I am XYZ2 in3← ⎕ new keepCount I am XYZ3 keepCount.Count 3 keepCount. ⎕ nl-2 Count in2. ⎕ nl-2 Count MyName in1.Count 3 Implementation: Encapsulation

110 Oct 2008OOAPL110 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

111 :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 2008OOAPL111 Implementation: Polymorphism Example: Depending on the number of arguments, the proper fn will be used.

112 Oct 2008OOAPL112 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

113 Oct 2008OOAPL113 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. www.dyalog.com

114 Oct 2008OOAPL114 End of implementation QUESTIONS ?

115 Oct 2008OOAPL115 Exercises Choice of exercises: 1.Simple numbersnumbers 2.Complex numbersnumbers 3.Parser 4.Pre formatted formsforms 5.Graphical objects 6.Telnet client The end

116 Oct 2008OOAPL116 Exercises – Preformatted form Deriving from Dyalog GUI classes. Besides classes you can write classes derived from -Forms -OLE servers/controls -.Net classes

117 Oct 2008OOAPL117 Exercises – Preformatted form Ex: f1← ⎕ new 'form' (('caption' 'zzz')('size'(100 200))) XL← ⎕ new 'oleclient' ( ⊂ 'classname' 'Excel.application') ⎕ using←'' present←System.DateTime.Now

118 Oct 2008OOAPL118 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 ((…))

119 Oct 2008OOAPL119 Exercises – Preformatted form

120 Oct 2008OOAPL120 Exercises – Preformatted form f1← ⎕ new Dialog(‘pref form' (100 500) ⍬ )

121 Oct 2008OOAPL121 Exercises – Preformatted form This class creates an edit object:

122 Oct 2008OOAPL122 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')

123 Oct 2008OOAPL123 Exercises Improvements: -Use an interface Back to exercise choices

124 Oct 2008OOAPL124 Exercises – Complex numbers Create 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

125 Oct 2008OOAPL125 Exercises A complex number is a number in the complex plane: 2 rational numbers are used to represent it. -4 -2 0 +2 +4 +6 +2 +4 +6 The number 3J4

126 Oct 2008OOAPL126 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.

127 Oct 2008OOAPL127 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.

128 Oct 2008OOAPL128 Exercises Complex 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.

129 Oct 2008OOAPL129 Exercises Complex math definition: Addition of 2 complex numbers C1 + C2 = (a1+a2) + (b1+b2) i

130 Oct 2008OOAPL130 Exercises Complex math definition: Multiplication of 2 complex numbers C1a1 b1i x C2 a2 b2i =(a1×a2) + (b1i × b2i) + (a1×b2i) + (b1i×a2) =((a1×a2) - (b1 × b2)) + ((a1×b2) + (b1×a2))i

131 Oct 2008OOAPL131 Exercises Complex math definition: Reciprocal of a complex number is 1÷C such that C × R = 1 = 1 + 0 i real = ((a1×a2) - (b1 × b2)) = 1 img = ((a1×b2) + (b1×a2)) i = 0 Solving: Ra = Ca÷((Ca×Ca)-(Cb×Cb)) Rb = -Cb÷((Ca×Ca)-(Cb×Cb))

132 Oct 2008OOAPL132 Exercises The first thing to do: )ED ○ complex

133 Oct 2008OOAPL133 Exercises Enter the fields:

134 Oct 2008OOAPL134 Exercises Enter the constructor, it takes 2 numbers as arguments, it merely sets the 2 fields:

135 Oct 2008OOAPL135 Exercises Test c1← ⎕ NEW complex (3,4) c1.real To ease creation of complex numbers: j←{ ⎕ new complex ( ⍺ ⍵ )} c2←5 j 12 c2.img

136 Oct 2008OOAPL136 Exercises Test 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)

137 Oct 2008OOAPL137 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

138 Oct 2008OOAPL138 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 function

139 Oct 2008OOAPL139 Exercises Enter the methods, first, :

140 Oct 2008OOAPL140 Exercises Test : cs←c1 complex.plus c2 cs.real ?

141 Oct 2008OOAPL141 Exercises Enter the methods, 2 nd, :

142 Oct 2008OOAPL142 Exercises Test : ct←c1 complex.times c2 ct.real ?

143 Oct 2008OOAPL143 Exercises Enter the methods, 3 rd, :

144 Oct 2008OOAPL144 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

145 Oct 2008OOAPL145 Exercises Try it! dj c1.reciprocal ⍝ c1 ÷ c1 or c1 × ÷c1: dj c1 complex.times c1.reciprocal dj c2 complex.times c1.reciprocal 2.52 0.64?

146 Oct 2008OOAPL146 Exercises Improvements: -other functions (magnitude?) -accept a single (real) number Back to exercise choices

147 Oct 2008OOAPL147 Exercises Create a class to do simple math with 2 integers representing fractions. Using a Numerator & a Denominator Ex.: 3 / 7 th

148 Oct 2008OOAPL148 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.

149 Oct 2008OOAPL149 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.

150 Oct 2008OOAPL150 Exercises Simple math definition: N -The 1 st number will be called N D -The 2 nd number will be called D They will be stored in a vector (2 elem) There will be two properties to access them individually.

151 Oct 2008OOAPL151 Exercises Simple math definition: Addition of 2 simple numbers (fractions) S1 + S2 = (n1 x d2 + n2 x d1), (d1 x d2) Multiplication of 2 simple numbers S1 x S2 = (n1 x n2), (d1 x d2) Reciprocal of a simple number ÷S1 = (d1), (n1)

152 Oct 2008OOAPL152 Exercises The first thing to do: )ED ○ simple

153 Oct 2008OOAPL153 Exercises Enter the field:

154 Oct 2008OOAPL154 Exercises Enter the constructor, it takes 2 numbers as arguments, it merely sets the field:

155 Oct 2008OOAPL155 Exercises At this point the class does not do anything. ‘Numbers’ is private and cannot be accessed from outside. Let’s create properties to access it.

156 Oct 2008OOAPL156 Exercises Enter the properties, they take 1 number to set a value in the field: N

157 Oct 2008OOAPL157 Exercises Test s1← ⎕ NEW simple(3,4) s1.N To ease creation of simple numbers: s←{ ⎕ new simple ( ⍺ ⍵ )} s2←5 s 12 s2.N

158 Now enter the second property… Oct 2008OOAPL158 Exercises

159 Oct 2008OOAPL159 Exercises Test 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)

160 Oct 2008OOAPL160 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

161 Oct 2008OOAPL161 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 function

162 Oct 2008OOAPL162 Exercises Enter the methods, first, :

163 Oct 2008OOAPL163 Exercises Test : cs←s1 simple.plus s2 cs.N ?

164 Oct 2008OOAPL164 Exercises Enter the 2 nd method, :

165 Oct 2008OOAPL165 Exercises Test : ct←s1 simple.times s2 ct.N ?

166 Oct 2008OOAPL166 Exercises Enter the methods, 3 rd, :

167 Oct 2008OOAPL167 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

168 Oct 2008OOAPL168 Exercises Try it! dj s1.reciprocal ⍝ s1 ÷ s1 or s1 × ÷s1: dj s1 simple.times s1.reciprocal dj s2 simple.times s1.reciprocal 5/66?

169 Oct 2008OOAPL169 Exercises Improvements: -other functions (square, abs?) -accept a single (real) number -make this into a complex number class Back to exercise choices

170 Oct 2008OOAPL170 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

171 Oct 2008OOAPL171 OO languages make abstraction easy They encourage architectures with elaborate layers. They can be good when the problem domain is truly complex and demands a lot of abstraction. Conclusion

172 Oct 2008OOAPL172 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!

173 Oct 2008OOAPL173 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!

174 Oct 2008OOAPL174 Buyer beware! Careful!


Download ppt "Object Oriented Programming in APL A New Direction Dan Baronet 2009 V1.22."

Similar presentations


Ads by Google