Jump to content
Sign in to follow this  
jaberwacky

Is this a good example of the observer pattern?

Recommended Posts

jaberwacky

I'm learning about various design patterns and I'm trying to squeeze them into autoitobject just for giggles.

I'm just wondering what anyone thinks of this. I might use it in scripts that manage multiple GUIs, perhaps across multiple monitors?

Example.au3

#include <AutoItObject.au3>

#include "Subject.au3"
#include "ChildObserver1.au3"
#include "ChildObserver2.au3"

OnAutoItExitRegister( "cleanup" )

Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")

_AutoItObject_Startup()

Global $Subject = Subject()

Global $Observer1 = ChildObserver1()
Global $Observer2 = ChildObserver2()

$Subject.Register( $Observer1 )
$Subject.Register( $Observer2 )

$Subject.Notify()

Func cleanup()
    $Subject.Unregister( $Observer2 )
    $Subject.Unregister( $Observer1 )

    $Observer2 = ''
    $Observer1 = ''
    $Subject = ''

    _AutoItObject_Shutdown()
EndFunc

Func _ErrFunc()
    ConsoleWrite("COM Error, ScriptLine(" & $oError.scriptline & ") : Number 0x" & Hex($oError.number, 8) & " - " & $oError.windescription & @CRLF)
EndFunc

Subject.au3

#include-once
#include <Array.au3>

Func Subject()
    Local Const $observers[ 1 ] = [ '' ]

    Local $this = _AutoItObject_Class()

    $this.AddProperty( "List" , $elscope_private , $observers )
;~  $this.AddProperty( "List" , $elscope_readonly , $observers ) ; readonly for testing purposes

    $this.AddMethod( "Register" , "Subject_Register" )
    $this.AddMethod( "Unregister" , "Subject_Unregister" )
    $this.AddMethod( "Notify" , "Subject_Notify" )

    Return $this.Object
EndFunc

Func Subject_Register( $this , Const $observer )
    Local $observers = $this.List

    If IsObj( $observer ) Then _ArrayAdd( $observers , $observer )

    $this.List = $observers

    Return $this
EndFunc

Func Subject_Unregister( $this , Const $observer )
    Local $observers = $this.List

    Local Const $element = _ArraySearch( $observers , $observer )

    _ArrayDelete( $observers , $element )

    $this.List = $observers

    Return $this
EndFunc

Func Subject_Notify( $this )
    Local $observers = $this.List

    For $i = 1 To UBound( $observers ) - 1
        $observers[ $i ].Update()
    Next

    Return $this
EndFunc

Observer.au3

#include-once

Func Observer()
    Local $this = _AutoItObject_Class()

    $this.AddDestructor( "Observer_Dtor" )

    Return $this.Object
EndFunc

Func Observer_Dtor( $this )
    MsgBox( 0 , '' , "Dtor" )
    Return $this
EndFunc

ChildObserver1.au3

#include-once

#include "Observer.au3"

Func ChildObserver1()
    Local $this = Observer()

    _AutoItObject_AddMethod( $this , "Update" , "Child_Update1" )

    Return $this
EndFunc

Func Child_Update1( $this )
    MsgBox( 0 , "Update" , "Child1" )
    Return $this
EndFunc

ChildObserver2.au3

#include-once

#include "Observer.au3"

Func ChildObserver2()
    Local $this = Observer()

    _AutoItObject_AddMethod( $this , "Update" , "Child_Update2" )

    Return $this
EndFunc

Func Child_Update2( $this )
    MsgBox( 0 , "Update" , "Child2" )
    Return $this
EndFunc
Edited by LaCastiglione

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.