Jump to content

Object Orientation question


Recommended Posts

Hi, I'm experimenting with the awesome AutoItObject. I stayed up pretty late last night having so much fun! But now I have come across a snag. My question is about designing objects. Let's say I have three functions in a procedural program.

Functions:

1) Assign data into a random amount of variables

2) Sort that data using a bubble sort

3) Display the sorted data

If I were to design an oop version of this procedural program then would I put all three functions into one object, or, would I seperate them into their own objects?

If you want code then ths is what I have so far. I have the three functions as their own seperate objects:

#AutoIt3Wrapper_AU3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -d

#include <AutoItObject.au3>

Global $oError = ObjEvent("Autoit.Error", "_myErrFunc")

_AutoItObject_StartUp()

Global $oObject = _AssignObj()

With $oObject
    Global $varName = .VariableName = "$variable"
    Global $varLimit = .Limit = Random(30, 669, 1)
    .New()
EndWith

;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
; ==> The requested action with this object has failed.:
;$sort.BubbleSort()
;$sort.BubbleSort()^ ERROR
Global $sort = _SortObject()
$sort.BubbleSort()
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Global $display = _DisplayObject()
$display.Display()

$oObject = 0
$sort = 0
$display = 0

Exit

Func _AssignObj()
    Local $oClassObject = _AutoItObject_Class()
    $oClassObject.Create()
    $oClassObject.AddProperty("VariableName", $ELSCOPE_PUBLIC)
    $oClassObject.AddProperty("Limit", $ELSCOPE_PUBLIC)
    $oClassObject.AddMethod("New", "_assignVars")
    Return $oClassObject.Object
EndFunc ;==>_unknownAssignObj

Func _SortObject()
    Local $oClassObject = _AutoItObject_Class()
    $oClassObject.Create()
    $oClassObject.AddMethod("BubbleSort", "_BubbleSort")
    Return $oClassObject.Object
EndFunc

Func _DisplayObject()
    Local $oClassObject = _AutoItObject_Class()
    $oClassObject.Create()
    $oClassObject.AddMethod("Display", "_display")
    Return $oClassObject.Object
EndFunc

Func _BubbleSort() ; I bubbalieve! =D YAY!
    Local Const $name = $varName
    Local Const $limit = $varLimit
    Local $a, $b

    For $index = $limit To 0 Step -1
        For $j = 1 To $index
            $a = Eval($name & $j)
            $b = Eval($name & $j - 1)

            If $a > $b Then ; two variable swap
                $a -= $b
                $b += $a
                $a = ($b - $a)

                Assign($name & $j, $a)
                Assign($name & $j - 1, $b)
            EndIf
        Next
    Next
EndFunc ;==>_BubbleSort

Func _assignVars($oSelf)
    Local Const $name = $oSelf.VariableName
    Local Const $limit = $oSelf.Limit
    Local $data, $return

    ; loads random data into a random amount of variables
    For $index = 0 To $limit
        $data = Round(Random(0, 100), 3)

        $return = Assign($name & $index, $data, 2)

        If $return = 0 Then MsgBox(0, "$return", $return)
        If Not IsDeclared($name & $index) Then Return SetError(1)
    Next
EndFunc ;==>_assignVars

Func _display()
    Local Const $name = $varName
    Local $index = 0

    ; display the results of the bubblesort
    While IsDeclared($name & $index)
        ConsoleWrite($name & $index & " " & Eval($name & $index) & @CRLF)
        $index += 1
    WEnd
EndFunc ;==>_display

Func _ErrFunc()
    ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF)
    Return
EndFunc ;==>_ErrFunc
Link to comment
Share on other sites

Bubble sort is not really good practice. You don't want to make your sort function dependent on a specific type of input (special object), you want it to work for strings, numbers, bytes, anything. Most implementations of bubble sort in OO languages are simply one class that has one bubble sort and several functions for comparing different types. Sometimes they use a delegate (you can see this as a pointer to a function) for comparing different types. If you want some good practice, then try to make some business intelligence. Think real life objects, and model them in your computer.

Link to comment
Share on other sites

I'm not sure I understand what you mean by make some business intelligence.

And this code that I'm writing is throw away code that I'm experimenting with. Definately not intended to be used by any sane person.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...