Author Topic: Singleton?  (Read 3694 times)

0 Members and 2 Guests are viewing this topic.

kryton9

  • Guest
Singleton?
« on: May 07, 2011, 12:15:06 AM »
Also can we use the Singleton pattern. For my game engine, I am hoping that by just creating a new instance of it I can create everything else and keep track via the CGLAPP so that it is very easy for users.
It would create default items and the user can just get them with defaults or be able to modify them via getters and setters for their needs.

Peter

  • Guest
Re: Singleton?
« Reply #1 on: May 07, 2011, 02:36:24 AM »
Hi K9,

I don't know what's the  meaning of GETTERS and SETTERS ?
Do you mean type setter ?   ???

Charles Pegge

  • Guest
Re: Singleton?
« Reply #2 on: May 07, 2011, 06:30:28 AM »
Kent,

I think I understand what singleton means: A class which has only one object belonging to it.

This is a way of encapsulating sets of global variables and devices into objects so that no conflicts can occur in the global namespace.

Some of my larger examples could benefit from this :)

This is not quite Singleton but one way to do it is create a class whose members are all static. Then it can be instantiated anywhere access is needed.

An object of this class has no body of its own but provides access to the static properties and methods shared with all the others.

I mention it here because it is simpler and faster than using a conventional singleton.

Code: [Select]
  '====================
  class SharedObject
  '====================
  '
  '
  static sys a,b,c
  '
  'MORE STATIC PROPERTIES HERE
  '
  '
  'ACCESSOR METHODS HERE (getters and setters)
  '
  end class

  SharedObject u
  SharedObject w

  u.c=42
  print w.c

Charles

kryton9

  • Guest
Re: Singleton?
« Reply #3 on: May 07, 2011, 12:39:28 PM »
Hi K9,

I don't know what's the  meaning of GETTERS and SETTERS ?
Do you mean type setter ?   ???

Peter, in classes you can have private variables. This means that a user can't directly access the data in those variables. They need to use getters and setters to access the private variables, this gives controlled access to the data by the class. This is one of the major benefits of classes, and is named encapsulation.

Here is an example in this thread Peter.
http://www.oxygenbasic.org/forum/index.php?topic=168.0




« Last Edit: May 07, 2011, 01:25:16 PM by kryton9 »

kryton9

  • Guest
Re: Singleton?
« Reply #4 on: May 07, 2011, 12:42:21 PM »
Thanks Charles, nice to know that it is doable.