Singleton : Fields vs Globals

BlitzMax Forums/BlitzMax Programming/Singleton : Fields vs Globals

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Wow pert, That's pretty nasty code lol. Doesn't do at all what the user would think if they didn't look at the Tsingleton class code directly. [edit: Actually Pert has a good idea, so I retract the preceeding statement!]

If you are creating a singleton twice, your code is wrong, so instead of "return singleton" I'd have something like a Runtimeerror stating you're using the class wrong.

Way I see it, once a class is completed, you shouldn't need to go back to that code. a Summary comment will (should...) show in the IDE as you're typing and this should be all you need to use the class.

Sadly, going off on a tangent, Blide is the only IDE I know of that makes use of Summary, and it only works in Windows. :-(

here's how I see it:

A global is persistent throughout all instances of a class, Which makes it useful to store something like a list of those instances in a Global.

TPlayer.GetPlayerlist() for example returns the Tplayers global List.

'Example Singleton. This is one of two ways, and you can use a Field just as I have used a Global,
'but you would need To keep a reference of what was created.
'The advantage of using the Global is that the code can simply be Tsingleton.GetInstance()
'instead of having a global instance elsewhere in the code, it is kept tidily in the Type interface.

Type Tsingleton
	Global Instance:Tsingleton

	'Summary: Constructor- Create the Tsingleton singleton.
	Function Create:Tsingleton()	'You could also overload the New function...
		If instance=Null Then
			instance=New Tsingleton
		Else
			RuntimeError "TSingleton is a Singleton. You may only create one instance!"
		EndIf
	End Function
	Function GetSingleton:Tsingleton()
		Return Instance
	End Function
End Type

'Example use of Global in a none Singleton:
Type Tplayer
	Global PlayerList:TList
	Field Name:String
	
	'Summary: Constructor- Create a player
	Function Create:Tplayer(name:String)	'You could also overload the New function...
		Local player:Tplayer=New Tplayer
		If Playerlist=Null Then playerlist=New TList
		playerlist.addlast(Player)
		player.name=name
		Return Player
	End Function

	'Summary: Retrieve a list of players.
	Function GetPlayerList:TList()
		Return Playerlist
	End Function

	'Destructor
	Method Delete()
		Playerlist.remove(Self)
	End Method
	
	
	'Summary: Get this current players name.
	Method Getname:String()
		Return Self.Name
	End Method
End Type


'Correct use of Singleton with this code:
'Create the Tsingleton instance.
Tsingleton.Create()	
'Call a second time? (should error and tell the coder he's cocked up!
'Tsingleton.Create()	

'Correct use of Tplayer:
Global Player1:Tplayer
Global Player2:Tplayer

Player1=Tplayer.Create("Damien")
Player2=Tplayer.Create("Tracy")

'Code that doesn't know what players have been created:
Local Playerlist:TList=Tplayer.GetPlayerList()
For Local Player:Tplayer=EachIn Playerlist
	Print "Look! I can see "+player.getname()+"!"
Next