7d3008ec3cefec6ead838711408eb0c1.ppt
- Количество слайдов: 168
Object Oriented Programming in APL A New Direction Dan Baronet 2011 V 1. 24
Dyalog V 11 was released in 2006. One of the major features was: Object Orientation Oct 2011 OOAPL 2
This presentation is based on two things: 1. What is Object Orientation programming? 2. How do I use it in APL? Oct 2011 OOAPL 3
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. Oct 2011 OOAPL 7
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
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 2011 OOAPL 9
Concepts of the OO paradigm The workspace - It contains everything needed to run an application Oct 2011 OOAPL 10
Concepts of the OO paradigm The workspace - It contains everything needed to run an application - It can be initialized (via []LX) Oct 2011 OOAPL 11
Concepts of the OO paradigm The workspace - It can be initialized (via []LX) - It can be viewed as an object Oct 2011 OOAPL 12
Concepts of the OO paradigm The workspace - It can serve as base and be copied into another workspace Current Workspace In memory Oct 2011 )COPY Files. Utils OOAPL Files. Utils 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 2011 OOAPL 14
Concepts of the OO paradigm The workspace problems - Name clashing (using )COPY) Fn 1 Fn 3 Var 1 varx Oct 2011 )COPY Files. Utils OOAPL Fn 2 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 (variable or function) per component in APL file - Need to cleanup when finished Oct 2011 OOAPL 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 2011 OOAPL 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 2011 OOAPL 18
Concepts of the OO paradigm The workspace – namespace In the 80 s 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
Concepts of the OO paradigm DOS V 1. 0 File 1, ext File 2. ext Abc. dws Note. txt Etc. doc APL V 1. 0 var 1 fn 1 var 2 function_2 etc flat structure for files and a workspace Oct 2011 OOAPL 20
Concepts of the OO paradigm DOS V 2. 0 APL V 8. 0 Dir 1 File 2. ext Abc. dws Note. txt Folder 2 Etc. doc subf file 1 filex Fich. log ns 1 var 2 function_2 namespace 2 fna subns fna opb objxv Tree (nested) structure Oct 2011 OOAPL 21
Concepts of the OO paradigm 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 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 my. App new. App<-[]NS []OR ’my. App’ A<-3 new. App. A<-3 )SAVE new. App Oct 2011 OOAPL 23
Concepts of the OO paradigm 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 2011 OOAPL 24
Concepts of the OO paradigm 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
. 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
. Net and Dyalog This is not new! The ability to use. Net and create classes was already in V 10 but it was more difficult to use. Oct 2011 OOAPL 27
The Object Oriented paradigm Oct 2011 OOAPL 34
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 Oct 2011 OOAPL 35
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 Oct 2011 OOAPL 36
OO fundamental concepts Classes ex: House blueprint Oct 2011 OOAPL 37
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. Oct 2011 OOAPL 38
OO fundamental concepts Interfaces An interface describes how to communicate with an object. Ex: electrical system & electrical outlets Oct 2011 OOAPL 39
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 Slow. Down (strength); // go slower It does NOT specify HOW it should be done. Oct 2011 OOAPL 40
Interfaces - example This car object implements maneuvering: Class car : maneuvering; Method Steering. Wheel: implements maneuvering. Steer (turn); . . . Method Gas. Pedal: implements maneuvering. Accellerate (push); . . . Method Break. Pedal: implements maneuvering. Slow. Down (hit); Oct 2011 OOAPL 41
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. Slow. Down (degree); Oct 2011 OOAPL 42
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. Oct 2011 OOAPL 43
OO fundamental concepts 2. Instances New House (Blue): After the creation of the new House the Constructor paints it in blue Oct 2011 OOAPL 44
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 also Properties which are Fields implemented via functions to read/set them. Oct 2011 OOAPL 45
OO fundamental concepts Members These can reside inside the class if shared or in the instance otherwise. Ex: the. Net class Date. Time has a member Now that does not require an instance to be called. e. g. Date. Time. Now ⍝ straight from the class 2007/10/21 9: 12: 04 Oct 2011 OOAPL 46
OO fundamental concepts Shared vs Instance Shared members remain in the class S 1 S 2 S 3 I 1 I 2 I 3 I 4 Oct 2011 S 2 S 3 I 1 I 2 I 3 I 4 S 1 S 2 S 3 I 1 I 2 I 3 I 4 OOAPL Instance members are created in the instance S 1 S 2 S 3 I 1 I 2 I 3 I 4 47
OO fundamental concepts Members: Methods Those can either reside in the class (shared method) or in the instance (instance method). There also abstract methods (e. g. in interfaces) with no code, only calling syntax. Constructors and destructors are methods. Oct 2011 OOAPL 48
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. Oct 2011 OOAPL 49
OO fundamental concepts Members: Properties Those are Fields implemented by accessing methods, i. e. Prop. X←value SET value ⍝ is implemented by They can either reside in the class (shared property) or in the instance (instance property). Oct 2011 OOAPL 50
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 a procedural language (like Dyalog), it corresponds to a call made to a routine. Oct 2011 OOAPL 51
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. Oct 2011 OOAPL 52
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) Oct 2011 OOAPL 53
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 Great Dane: Dog Instances Dog: Animal Fido (Collie) Classes Collie: Dog Fish: Animal Oct 2011 OOAPL Rex (Collie) 54
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 Mule Donkey Oct 2011 OOAPL 55
OO fundamental concepts Inheritance: simulate multiple inheritance A class can implement several interfaces. Here class Penguin implements 2 different behaviours (interfaces): Fish (swim, eggs) Penguin: -Swims -Flies not -1 egg/yr -Croaks Bird (fly, eggs, sing) Oct 2011 OOAPL 56
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. Oct 2011 OOAPL 57
OO fundamental concepts Inheritance: a derived class inherits all the base members C 1 member. A member. B C 2: C 1 member. C member. D Oct 2011 OOAPL 58
OO fundamental concepts Inheritance: a derived class inherits all the base members C 1 member. A member. B member. C Oct 2011 C 2: C 1 member. C member. D OOAPL 59
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. Oct 2011 OOAPL 60
OO fundamental concepts Inheritance Some classes may be sealed to prevent other classes from inheriting them. Oct 2011 OOAPL 61
Override concept Class B Class A M 1 M 2 M 2 ‘I am A’ // M 1 calls B’s M 2: (NEW B). M 1 I am B // M 1 calls A’s M 2: (NEW A). M 1 I am A Oct 2011 M 2 ‘I am B’ OOAPL 62
Based classes Class A There is no M 1 in B so A’s (on which B is based) M 1 is used M 1 M 2 ‘I am A’ M 2 ‘I am B’ // M 1 calls A’s M 2: (NEW B). M 1 I am A // M 1 calls A’s M 2: (NEW A). M 1 I am A Oct 2011 Class B: A OOAPL 63
Based classes Class A There is no M 1 in B so A’s (on which B is based) M 1 is used M 1 M 2 Class B: A M 2: Override ‘I am B’ M 2 ‘I am A’ // M 1 calls A’s M 2: (NEW B). M 1 I am A // M 1 calls A’s M 2: (NEW A). M 1 I am A A: M 2 does not allow to be overridden Oct 2011 OOAPL 64
Based classes Class A There is no M 1 in B so A’s (on which B is based) M 1 is used M 1 M 2: Class B: A M 2: Override ‘I am B’ M 2: Overridable ‘I am A’ // M 1 calls B’s M 2: (NEW B). M 1 I am B // M 1 calls A’s M 2: (NEW A). M 1 I am A A: M 2 does allow to be overridden AND B wants to override it Oct 2011 OOAPL 65
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). Oct 2011 OOAPL 66
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. Oct 2011 OOAPL 67
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. Oct 2011 OOAPL 68
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 sub 1 seen in the white area sub 2 Oct 2011 OOAPL 69
OO fundamental concepts Encapsulation: private Analogy names are private in a D-fn: a←{z← 99 ⋄ b ⍵} b←{z+⍵} a 2 VALUE ERROR b[0] b←{z+⍵} ∧ Oct 2011 OOAPL 70
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 sub 1 seen in the white area sub 2 Oct 2011 OOAPL 71
OO fundamental concepts Encapsulation: 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 2 101 Oct 2011 OOAPL 72
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 Oct 2011 sub 2 OOAPL 73
OO fundamental concepts Encapsulation: friend A class declared friend can see the offerer’s private and public members. Class A Class B Private Apv Public Apu Private Bpv Public Bpu Here A sees B’s members as its own members Oct 2011 OOAPL Friend A 74
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) Oct 2011 OOAPL 75
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 Oct 2011 OOAPL 76
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 Oct 2011 OOAPL 77
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. Oct 2011 OOAPL 78
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 Oct 2011 OOAPL 79
End of OO basics QUESTIONS ? Oct 2011 OOAPL 80
Oct 2011 OOAPL 81
Implementation • Dyalog follows C# rules closely • The extensions to the language have been made in following with the : Keywords scheme introduced in the 90 s (All new keywords start with ‘: ’ ) • Some new system functions have been added and • Some new system commands Oct 2011 OOAPL 82
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 Oct 2011 OOAPL 83
Implementation: classes A class can be defined using the )EDitor as in )ED ○ My. Class Upon exiting, My. Class will be defined in the current namespace (e. g. #) Oct 2011 OOAPL 84
Implementation: classes In the workspace My. Class appears as a namespace: ⎕NC ‘My. Class’ 9 )CLASSES My. Class Oct 2011 OOAPL 85
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) Oct 2011 OOAPL 86
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. Oct 2011 OOAPL 87
Implementation: members The class to the right shows a few methods (fns) and their access. <mon 1> is used as a constructor. It MUST be public. Oct 2011 OOAPL 88
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 Oct 2011 OOAPL 89
Implementation: instances 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 Oct 2011 OOAPL 90
Implementation: ⎕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. Oct 2011 OOAPL 91
Implementation: ⎕NEW Example: my. Instance← ⎕ NEW My. Class The display form includes [brackets] by default my. Instance #. [My. Class] It is another type of namespace ⎕ NC ‘my. Instance’ 9 Oct 2011 OOAPL 92
Implementation: ⎕DF ⎕ DF allows to change the default Display Form of a namespace. Example: ⎕ ←obj←⎕ NEW My. Class #. [My. Class] obj. ⎕ DF ‘OO class’ obj OO class Oct 2011 OOAPL 93
Implementation: constructors Classes may require something to initialize the instance. Ex: )ED ○ Animal Here an animal must have at least two characteristics: 1. # of legs 2. the sounds it makes Oct 2011 OOAPL 94
Implementation: constructors Several instances can be made up of the same Animal class: 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? 4002 Oct 2011 OOAPL 95
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. shared. Fn args or my. Instance. Fn args ⍝ args if required Oct 2011 OOAPL 96
Implementation: inheritance Inheritance: Based classes A class can be based on another based class. See class Collie based on Dog based on Animal Dog: Animal Great Dane: Dog Fido (Collie) Classes Collie: Dog Fish: Animal Oct 2011 Instances Rex (Collie) OOAPL 97
Implementation: inheritance Example: a bird’s generic description is ‘plain Bird’ Birdy←⎕NEW Birdy. Desc plain Birdy (Bird) When Birdy is created, Bird’s constructor is called Oct 2011 OOAPL 98
Implementation: inheritance 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. Oct 2011 OOAPL 99
Implementation: inheritance Example: a parrot is a bird Coco←⎕NEW Parrot Coco. Desc Not a plain Bird, a Parrot Birdy (Bird) Bird Parrot: Bird Oct 2011 Coco (Parrot) OOAPL 100
Implementation: inheritance We can create classes and instances at all levels Birdy (Bird) Bird Coco (Parrot) Classes Instances Parrot: Bird Tweety (Macaw) Macaw: Parrot Oct 2011 OOAPL 101
Implementation: inheritance Inheritance: constructors with arguments Using the animal kingdom as example. Big Dane: Dog Great Dane: Dog Instances Dog: Animal Fido (Collie) Classes Collie: Dog Fish: Animal Oct 2011 Rex (Collie) OOAPL 102
Implementation: inheritance pet←⎕ NEW Collie ‘Rex’ pet. ⎕ nl -2 Name Nlegs Sounds Type pet. (Name Type) Rex canis familiaris Oct 2011 OOAPL 103
Implementation: inheritance Let’s go over this again slowly… pet←⎕ NEW Collie ‘Rex’ pet. ⎕ nl -2 Name Nlegs Sounds Type pet. (Name Type) Rex canis familiaris Oct 2011 OOAPL 104
Implementation: inheritance pet←⎕ NEW Collie ‘Rex’ pet. ⎕ nl -2 Name Nlegs Sounds Type pet. (Name Type) Rex canis familiaris Oct 2011 OOAPL 105
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. Oct 2011 OOAPL 106
Implementation: Encapsulation 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 My. Field Oct 2011 OOAPL 107
Implementation: Encapsulation The class to the right shows a few fields (vars) and their access. The access is the same for methods (fns) and properties. Oct 2011 OOAPL 108
Implementation: Encapsulation 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) Oct 2011 OOAPL 109
Implementation: Encapsulation Shared vs instance Example: keep. Count Here each instance has its own private ID and public name The class keeps track of the number of instances created privately. Oct 2011 OOAPL 110
Implementation: Encapsulation Shared vs instance 'Count' remains in the class Count My. ID=1 My. Name=XYZ 1 Oct 2011 keep. Count: Count← 0, 1, … My. ID My. Name Count My. ID=2 My. Name=XYZ 2 OOAPL Instance members are created in the instance Count My. ID=3 My. Name=XYZ 3 111
Implementation: Encapsulation Shared vs instance in 1←⎕new keep. Count I am XYZ 1 in 2←⎕new keep. Count I am XYZ 2 in 3←⎕new keep. Count I am XYZ 3 keep. Count. ⎕nl-2 Count in 2. ⎕nl-2 Count My. Name in 1. Count 3 Oct 2011 OOAPL 112
Implementation: Polymorphism 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. Oct 2011 OOAPL 113
Implementation: Polymorphism : Class many. Ctors Example: Depending on the number of arguments, the proper fn will be used. Oct 2011 OOAPL ∇ general x ⍝ This ctor any arg ⍝ It can be anything : Implements constructor : Access public ∇ ∇ nil ⍝ This ctor takes no arg : Implements constructor : Access public ∇ ∇ mon 1(a 1) ⍝ This ctor takes 1 arg ⍝ It MUST be a vector : Implements constructor : Access public ∇ ∇ mon 5(a 1 a 2 a 3 a 4 a 5) ⍝ This ctor takes 5 arg ⍝ It MUST be exactly 5 : Implements constructor : Access public ∇ : End. Class 114
Implementation summary • • Dyalog follows C# rules closely )ED to edit 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 Oct 2011 OOAPL 115
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. Oct 2011 OOAPL 116
End of implementation QUESTIONS ? Oct 2011 OOAPL 117
Exercises Choice of exercises: 1. Simple numbers 2. Complex numbers 3. Parser 4. Pre formatted forms 5. Graphical objects 6. Telnet client Oct 2011 OOAPL The end 118
Exercises – Preformatted form Deriving from Dyalog GUI classes. Besides classes you can write classes derived from - Forms - OLE servers/controls -. Net classes Oct 2011 OOAPL 119
Exercises – Preformatted form Ex: f 1←⎕new 'form' (('caption' 'zzz')('size'(100 200))) XL←⎕new 'oleclient' (⊂'classname' 'Excel. application') ⎕using←'' present←System. Date. Time. Now Oct 2011 OOAPL 120
Exercises – Preformatted form We need to write a class that will create a form, just like f 1←⎕new 'form' (('caption' 'zzz')('size'(100 200))) But with 2 buttons already setup for SAVE and QUIT so we can do f 1←⎕new my. Form ((…)) Oct 2011 OOAPL 121
Exercises – Preformatted form Oct 2011 OOAPL 122
Exercises – Preformatted form f 1←⎕ new Dialog(‘pref form' (100 500) ⍬ ) Oct 2011 OOAPL 123
Exercises – Preformatted form This class creates an edit object: Oct 2011 OOAPL 124
Exercises – Preformatted form f 1←⎕ new Dialog('pref form' (200 300) ⍬ ) f 1. fn←f 1. ⎕ new Edit. Field ('First name: ' '' (10 60) (⍬ 100) ⍬ ) f 1. ln←f 1. ⎕ new Edit. Field ('Last name: ' '' (38 60) (⍬ 100) ⍬ ) f 1. ad←f 1. ⎕ new Edit. Field ('Address: ' '' (66 60) (90 230) (⊂'style' 'multi')) f 1. (fn ln ad). Text←'Jeremy' 'Cricket'('Top. Flower' 'last field') Oct 2011 OOAPL 125
Exercises Improvements: - Use an interface Back to exercise choices Oct 2011 OOAPL 126
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 Oct 2011 -5 0 OOAPL +5. 3 +10 127
Exercises A complex number is a number in the complex plane: 2 rational numbers are used to represent it. -4 Oct 2011 -2 The number 3 J 4 +6 +4 +2 0 OOAPL +2 +4 +6 128
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. Oct 2011 OOAPL 129
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. Oct 2011 OOAPL 130
Exercises Complex math definition: C = a + bi, where i = ¯ 1 * 0. 5 Engineers use the J notation, like this: 3 j 4 to denote the complex number with a real value of 3 and an imaginary value of 4. Oct 2011 OOAPL 131
Exercises Complex math definition: Addition of 2 complex numbers C 1 + C 2 = (a 1+a 2) + (b 1+b 2) i Oct 2011 OOAPL 132
Exercises Complex math definition: Multiplication of 2 complex numbers C 1 a 1 b 1 i x C 2 a 2 b 2 i =(a 1×a 2) + (b 1 i × b 2 i) + (a 1×b 2 i) + (b 1 i×a 2) =((a 1×a 2) - (b 1 × b 2)) + ((a 1×b 2) + (b 1×a 2))i Oct 2011 OOAPL 133
Exercises Complex math definition: Reciprocal of a complex number is 1÷C such that C×R=1=1+0 i real = ((a 1×a 2) - (b 1 × b 2)) = 1 img = ((a 1×b 2) + (b 1×a 2)) i = 0 Solving: Ra = Ca÷((Ca×Ca)-(Cb×Cb)) Rb = -Cb÷((Ca×Ca)-(Cb×Cb)) Oct 2011 OOAPL 134
Exercises The first thing to do: )ED ○ complex Oct 2011 OOAPL 135
Exercises Enter the fields: Oct 2011 OOAPL 136
Exercises Enter the constructor, it takes 2 numbers as arguments, it merely sets the 2 fields: Oct 2011 OOAPL 137
Exercises Test c 1←⎕NEW complex (3, 4) c 1. real To ease creation of complex numbers: j←{⎕new complex (⍺ ⍵)} c 2← 5 j 12 c 2. img Oct 2011 OOAPL 138
Exercises Test Typing c 1 does not provide useful information. It would be nice if we could find what the numbers are without having to use c 1. (real img) Oct 2011 OOAPL 139
Display form • The display form can be improved • We can use ⎕ DF to show a different string, e. g. 3 J 4 • Try c 1. ⎕DF '3 J 4' (or whatever) • Try including the setting in the constructor Oct 2011 OOAPL 140
Display form • assignment does not change the display Try c 1. img← 22 and verify that ⎕←c 1 has the same display form • use properties to change that • create 2 properties • use ⎕DF to change the display in the <set> function Oct 2011 OOAPL 141
Exercises Enter the methods, first, <plus>: Oct 2011 OOAPL 142
Exercises Test <plus>: cs←c 1 complex. plus c 2 cs. real ? Oct 2011 OOAPL 143
Exercises Enter the methods, 2 nd, <times>: Oct 2011 OOAPL 144
Exercises Test <times>: ct←c 1 complex. times c 2 ct. real ? Oct 2011 OOAPL 145
Exercises Enter the methods, 3 rd, <reciprocal>: Oct 2011 OOAPL 146
Exercises Try it! complex. plus/4⍴c 1 ⍝ if funny display, use dj←{⍵. (real img)} dj complex. plus/4⍴c 1 ⍝ 12 16? dj complex. times3⍴c 1 ⍝ 3 4 ¯ 7 24. . . dj complex. plus/c 1 complex. times¨ 4⍴c 1 Oct 2011 OOAPL 147
Exercises Try it! dj c 1. reciprocal ⍝ c 1 ÷ c 1 or c 1 × ÷c 1: dj c 1 complex. times c 1. reciprocal dj c 2 complex. times c 1. reciprocal 2. 52 0. 64? Oct 2011 OOAPL 148
Exercises Improvements: - other functions (magnitude? ) - accept a single (real) number Back to exercise choices Oct 2011 OOAPL 149
Exercises Create a class to do simple math with 2 integers representing fractions. Using a Numerator & a Denominator Ex. : 3 / 7 th Oct 2011 OOAPL 150
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. Oct 2011 OOAPL 151
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. Oct 2011 OOAPL 152
Exercises Simple math definition: - The 1 st number will be called N - 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. Oct 2011 OOAPL 153
Exercises Simple math definition: Addition of 2 simple numbers (fractions) S 1 + S 2 = (n 1 x d 2 + n 2 x d 1) , (d 1 x d 2) Multiplication of 2 simple numbers S 1 x S 2 = (n 1 x n 2) , (d 1 x d 2) Reciprocal of a simple number ÷S 1 = (d 1) , (n 1) Oct 2011 OOAPL 154
Exercises The first thing to do: )ED ○ simple Oct 2011 OOAPL 155
Exercises Enter the field: Oct 2011 OOAPL 156
Exercises Enter the constructor, it takes 2 numbers as arguments, it merely sets the field: Oct 2011 OOAPL 157
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. Oct 2011 OOAPL 158
Exercises Enter the properties, they take 1 number to set a value in the field: N Oct 2011 OOAPL 159
Exercises Test s 1←⎕NEW simple(3, 4) s 1. N To ease creation of simple numbers: s←{⎕new simple (⍺ ⍵)} s 2← 5 s 12 s 2. N Oct 2011 OOAPL 160
Exercises Now enter the second property… Oct 2011 OOAPL 161
Exercises Test Typing s 1 does not provide useful information. It would be nice if we could find what the numbers are without having to use s 1. (N D) Oct 2011 OOAPL 162
Display form • The display form can be improved • We can use ⎕ DF to show a different string, e. g. 3/4 • Try s 1. ⎕DF '3/4' • Try including the setting in the constructor Oct 2011 OOAPL 163
Display form • assignment does not change the display Try s 1. N← 22 and verify that ⎕←s 1 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 Oct 2011 OOAPL 164
Exercises Enter the methods, first, <plus>: Oct 2011 OOAPL 165
Exercises Test <plus>: cs←s 1 simple. plus s 2 cs. N ? Oct 2011 OOAPL 166
Exercises Enter the 2 nd method, <times>: Oct 2011 OOAPL 167
Exercises Test <times>: ct←s 1 simple. times s 2 ct. N ? Oct 2011 OOAPL 168
Exercises Enter the methods, 3 rd, <reciprocal>: Oct 2011 OOAPL 169
Exercises Try it! simple. plus/4⍴s 1 ⍝ if funny display, use dj←{⍵. (N ‘/’ D)} dj simple. plus/4⍴s 1 ⍝ 22/1? dj simple. times3⍴s 1 ⍝ 11/2 121/4… dj simple. plus/s 1 simple. times¨ 4⍴s 1 Oct 2011 OOAPL 170
Exercises Try it! dj s 1. reciprocal ⍝ s 1 ÷ s 1 or s 1 × ÷s 1: dj s 1 simple. times s 1. reciprocal dj s 2 simple. times s 1. reciprocal 5/66? Oct 2011 OOAPL 171
Exercises Improvements: - other functions (square, abs? ) - accept a single (real) number - make this into a complex number class Back to exercise choices Oct 2011 OOAPL 172
Conclusion 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. Oct 2011 OOAPL 173
Conclusion 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. Oct 2011 OOAPL 174
Careful! • 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 Oct 2011 OOAPL 175
Careful! • 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. Oct 2011 OOAPL 176
Careful! Buyer beware! Oct 2011 OOAPL 177
7d3008ec3cefec6ead838711408eb0c1.ppt