OOP Design Dilemma

BlitzMax Forums/BlitzMax Programming/OOP Design Dilemma

small example of my point:

you can comment out either the creation of cl or cle .. the functionality is gone, but the program still runs. As you see, the cl and cle really don't know eachother, all communication is done by using events. Ofcourse this is a small example, it's not even perfect.. if you add another cle (cle2) at some different coord, then that one also reacts on changes automatically. I didn't include ID's to limit functionality to a given cle, couldn't be bothered to either.. it's just for demo'ing purposes!

SuperStrict
' "work together"-example

Const EVENT_COLORLIST_CHANGED:Int=$13370590
Const EVENT_COLORLIST_SETHOOVER:Int=$13370591
Const EVENT_COLORLIST_SETCURRENT:Int=$13370592
Const EVENT_COLORLIST_UPDATE:Int=$13370593
Const EVENT_REQUESTCOLORCHANGE:Int=$13370594

' --------------------------------------------------------------------------
' --------------------------------------------------------------------------

Type TColorlist
	Field r:Byte[8]
	Field g:Byte[8]
	Field b:Byte[8]
	
	Field current:Int=0
	Field hoover:Int=0
	
	Field newevent:TEvent=New TEvent	



	Method New()
		Local t:Int
		For t=0 To 7
			r[t]=Rnd(255)
			g[t]=Rnd(255)
			b[t]=Rnd(255)
		Next
		
		AddHook EmitEventHook,eventhook,Self		
	End Method
	
	Function eventhook:Object(id:Int,data:Object,context:Object)
		If TColorlist(context) TColorlist(context).ev TEvent(data);Return data	
	EndFunction

	Method Free()
		RemoveHook EmitEventHook,eventhook
		GCCollect()
	End Method



	Method ev(event:TEvent)
		If event.id=EVENT_REQUESTCOLORCHANGE
			current=event.data
			newevent.id=EVENT_COLORLIST_CHANGED
			EmitEvent newevent
		EndIf
		
		If event.id=EVENT_COLORLIST_SETHOOVER
			hoover=event.data
			newevent.id=EVENT_COLORLIST_CHANGED
			EmitEvent newevent
		EndIf

		If event.id=EVENT_COLORLIST_SETCURRENT
			current=event.data
			newevent.id=EVENT_COLORLIST_CHANGED
			EmitEvent newevent
		EndIf
				
		If event.id=EVENT_COLORLIST_UPDATE

			For Local t:Int=0 To 7
				If t=current
					SetColor 0,0,0
					DrawRect t*16,0,16,16
				EndIf				
				If t=hoover
					SetColor 255,255,255
					DrawRect t*16+1,1,16-2,16-2
				EndIf				
				
				SetColor r[t],g[t],b[t]
				DrawRect t*16+2,2,16-4,16-4
			Next
			SetColor 255,255,255
		EndIf
		
	End Method
	
End Type

' --------------------------------------------------------------------------
' --------------------------------------------------------------------------

Type TColorlisteditor

	Field canvas:TGadget
	
	Field newevent:TEvent=New TEvent
	
	Field ex:Int

	Method New()
		AddHook EmitEventHook,eventhook,Self
	End Method
	
	Function eventhook:Object(id:Int,data:Object,context:Object)
		If TColorlisteditor(context) TColorlisteditor(context).ev TEvent(data);Return data	
	EndFunction

	Method Free()
		RemoveHook EmitEventHook,eventhook
		GCCollect()
	End Method
	
	Method ev(event:TEvent)
		If event.source=canvas
			If event.id=EVENT_GADGETPAINT update
			
			If event.id=EVENT_MOUSEMOVE
				ex=Max(Min(event.x/16,7),0)
				
				newevent.id=EVENT_COLORLIST_SETHOOVER
				newevent.data=ex
				EmitEvent newevent
				update
			EndIf
			
			If event.id=EVENT_MOUSEDOWN
				If event.data=1
					newevent.id=EVENT_COLORLIST_SETCURRENT
					newevent.data=ex
					EmitEvent newevent
					update
				EndIf
			EndIf
			
			If event.id=EVENT_MOUSELEAVE
				newevent.id=EVENT_COLORLIST_SETHOOVER
				newevent.data=-1
				EmitEvent newevent
				update
			EndIf
		EndIf
		
		If event.id=EVENT_COLORLIST_CHANGED
			update
		EndIf
	End Method
	
	Method update()
		SetGraphics CanvasGraphics(canvas)
			SetClsColor 160,160,160;Cls
			newevent.id=EVENT_COLORLIST_UPDATE
			EmitEvent newevent
			Flip			
	End Method
	
End Type

Function CreateColorlisteditor:TColorlisteditor(x:Int,y:Int,parent:TGadget)
	Local a:TColorlisteditor=New TColorlisteditor
	a.canvas=CreateCanvas(x,y,16*8,16,parent)
	Return a
End Function

' --------------------------------------------------------------------------
' --------------------------------------------------------------------------

Local window:TGadget=CreateWindow("oO",0,0,640,480,Null,1)


Local cl:TColorlist=New TColorlist
Local cle:TColorlisteditor=CreateColorlisteditor(2,2,window)

Repeat
	WaitEvent()
	If EventID()=EVENT_WINDOWCLOSE End
Forever