an object lesson in classes “or how oo abl solves everything.”

40
AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

Upload: roderick-arnold

Post on 05-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

AN OBJECT LESSON IN CLASSES

“or how OO ABL solves everything.”

Page 2: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BRIEF INTRODUCTION

• Julian Lyndon-Smith, IT Director dot.r limited• Progress developer since v3, 1987• Living proof of “old dog, new tricks”

• Often hanging around on PEG and PSDN• Always up for a good argument ;)

Page 3: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

AGENDA

• Who’s afraid of the big bad wOOlf ?• Basics• Getting rid of the garbage• Inheritance, interfaces and all that• Advanced OO• Gotchas• Real world code ... Warts and all …

Page 4: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

WHO’S AFRAID OF THE BIG BAD WOOLF ?• Survey : Progress version• <10.2B• 10.2B• 11.0• 11.1• 11.2

Page 5: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

WHO’S AFRAID OF THE BIG BAD WOOLF ?• Survey : Who’s using OO• Not yet• Dabbling• Sometimes• Mostly• Wizard

Page 6: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS : OO ADVANTAGES

• Type-safe• Cannot tipe a bad metgod name

• Encapsulation• Group together methods and data

• Code reuse

Page 7: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS : OO ADVANTAGES

• Code completion• Makes you really cool• Talking about the fancy OO terms like “injection” ,

“factory”, “instantiation”, “inheritance” allows you to hang out with the cool kids

Page 8: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS

• A class is an 4gl file with a .cls extension• Compiles to .r• Two parts: package and class name• Package is the directory where source lives• foo.bar.MyClass is foo/bar/MyClass.cls

• Using statement

Page 9: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS

• An instance is a running class• Similar to persistent procedures• Run foo.p persistent set x • x = new foo().

Page 10: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS : CREATE AN INSTANCE• /** foo/bar/MyClass.cls */

class foo.bar.MyClass:• end class.

• def var oFoo as foo.bar.MyClass no-undo.

• oFoo = new foo.bar.MyClass().

Page 11: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS: PROPERTIES

• A property can be described as a procedure-wide “variable”• Extra toys to play with : get / set etc• Public and private

Page 12: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS : PROPERTIES• /** foo/bar/MyClass.cls */

class foo.bar.MyClass:

• def public property guid as char no-undo get . set .

• end class.

• def var oFoo as foo.bar.MyClass no-undo.

• oFoo = new foo.bar.MyClass().

• Message oFoo:guid view-as alert-box.

Page 13: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS: PROPERTIES

• Def [public | protected | private]• [public | protected | private] get.• [public | protected | private] set.

Page 14: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS: METHODS

• A method can be described as a function• Without having to forward declare it• Returns a value or void• Can optionally have parameters

Page 15: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS : METHODS• /** foo/bar/MyClass.cls */

class foo.bar.MyClass:

• method public void DoStuff():

• /** do some stuff */

• end method.

• end class.

• def var oFoo as foo.bar.MyClass no-undo.

• oFoo = new foo.bar.MyClass().

• oFoo:DoStuff().

Page 16: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS : METHODS• /** foo/bar/MyClass.cls */

class foo.bar.MyClass:

• method public void DoStuff(p_name as char):

• /** do some stuff */

• Message p_name view-as alert-box.

• end method.

• end class.

• def var oFoo as foo.bar.MyClass no-undo.

• oFoo = new foo.bar.MyClass().

• oFoo:DoStuff(“my_little_pony”).

Page 17: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

BASICS: METHODS

• Method [public | protected | private]

Page 18: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

GETTING RID OF THE GARBAGE

• Persistent procedures can create memory leaks• Run foo.p persistent set x.• Must remember to delete x or memory leaks

• Classes have an automatic Garbage collector• Automatically deletes unreferenced class instances• You have no control over this

Page 19: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

GETTING RID OF THE GARBAGE

• run DoMyStuff in this-procedure.

• /** do some more things like UI */

• procedure DoMyStuyff:• def var oFoo as foo.bar.MyClass no-undo.

• oFoo = new foo.bar.MyClass().

• oFoo:DoStuff().

• end procedure.

Page 20: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

NOT GETTING RID OF THE GARBAGE• def var oFoo as foo.bar.MyClass no-undo.

• run DoMyStuff in this-procedure.

• /** do some more things like UI */

• procedure DoMyStuyff:• oFoo = new foo.bar.MyClass().

• oFoo:DoStuff().

• end procedure.

Page 21: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

INHERITANCE, INTERFACES AND ALL THAT

• Inheritance • Interfaces• Polymorphism• Abstracts

Page 22: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

INHERITANCE

• Abstract out common functionality (and data) from similar classes• A sub-class inherits from a super-class• A sub-class can extend or change behavior of super-class• Overriding methods• Adding new methods / properties

Page 23: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

INTERFACES

• Specifies a set of method prototypes and properties • Similar to inheritance• No default implementation methods or properties

• Allows you to build different classes that conform to an API• Each class that uses an Interface must implement all methods and properties

defined in the interface

• Convention is that interface files start with an I

Page 24: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

POLYMORPHISM

• As shown previously, multiple classes can inherit from same super-class• Sub-classes can override methods or behavior in the super-class• Multiple implementations (different parameters)• Different behavior : Same method, multiple signatures

• Super:Method()

Page 25: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ABSTRACTS

• Abstracts are classes than cannot be instantiated by themselves• Need to inherit an abstract class• All appropriate properties and methods are

available to sub-class

Page 26: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO

• Events• Chaining• Parameter passing• Statics

Page 27: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : EVENTS

• Classes can raise “named” events• Define• Listen• Raise

Page 28: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : EVENTS• def public event StateChanged signature void (p_msg as char).

• StateChanged:publish(“foo”).

Page 29: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : EVENTS

• Def var oMessage as foo.Message no-undo.

• oMessage = new foo.Message().

• oMessage:StateChanged:Subscribe(this-object:StateChanged).

• /** do stuff */

• Method public void StateChanged(p_state as char):

• message p_state view-as alert-box info.

• End method.

Page 30: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : CHAINING

• Also called • Fluent-style coding• Fluent-coding style

•Methods return an instance of a class which is then used to call another method• Confused ?

Page 31: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : CHAINING• Association(BelongsTo("State"):Keys("StateID","State"):CascadeNullify()).

• Assocation(p_prop as dotr.property)• dotr.property – method BelongsTo [dotr.property] (p_name)• dotr.property – method Keys [dotr.property] (p_key1,p_key2)• dotr.property – method CascadeNullify()

• Association(BelongsTo("State")

:Keys("StateID","State"):CascadeNullify()

).

Page 32: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : CHAINING

• (new foo.bar()):DoStuff(). **• No need for variable• Does stuff• Gets GC’d automatically

• ** new foo.bar():DoStuff(). • Hopefully in 11.3

Page 33: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : PARAMETER PASSING

• Parameters : pita• Change over time• Need to write new ip’s to handle new parameters• May need to revisit old code and change

Page 34: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : PARAMETER PASSING

• Use an instance of a class as a Parameters• Change properties of class• All inputs now have additional properties without changing

a line of code

• Input == input-output

Page 35: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : STATICS

• A static method is loaded into memory. Once.• Can then use that method without having to define a

variable• Foo.bar()• Foo is the class, bar is a static method

Page 36: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

ADVANCED OO : STATICS

• Tempting to use• Cannot be unloaded at all from session• Do not use record buffers in any method of a class that

has a static member• Buffers are then “locked” into memory• Impossible to update db whilst app is running

Page 37: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

GOTCHAS

• Event Subscription• Potentially no garbage collection

• Sockets• AppServer boundaries• No serialization

Page 38: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

GOTCHAS

• Statics•OO-guys can sound more intelligent than you• Doing stuff in the constructor

Page 39: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

SHOW ME THE CODE

• Demos and walkthroughs of the various technqiues • perhaps even “on demand coding”

Page 40: AN OBJECT LESSON IN CLASSES “or how OO ABL solves everything.”

QUESTIONS,DEBATE