On Nov 24, 2009, at 2:54 PM, Leandro Damasio wrote: > Hello Teo! > > >I don't understand what do you mean by natively, if you mean to: > >- Limit the number of instances > >- automate the instantiation > >- calling in the app > > By native singleton pattern class implementation I mean to provide that a > class could be declared as a singleton class and that this singleton class > just instanciates once a single object instance which should be then > referenced every time the class would be referenced. By native implementation > I also mean the conceptual implementation of a singleton classes in harbour. > > I'm sure there are lots of ways to get singleton-like behaviour from a class, > but the more examples I see on how to get it, more it sounds like a general > need and more it seems that native implementation would be usefull to the > users (and maybe good to the language too). > > The sintax and behaviour I propose would work like the example below > > Regards, > Leandro Damasio > > > EXAMPLE: > ========= > > Case 1) A singleton class: > > <code> > > <PRG> > > //---------------------------------------------------------------------------------------------------// > CLASS MyClass SINGLETON // THIS IS A SINGLETON CLASS!!! > > DATA Status INIT 0 > METHOD ChangeStatus() INLINE ::Status := 1 > > ENDCLASS > > > ******************************* > Procedure Main > ******************************* > > ? MyClass():Status // 0 > > // first new object is instantiated and kept as the single one > > ? MyClass():ChangeStatus() // 1 > > // single object is referred to run :Changestatus() > > ? MyClass():Status // 1 > > // single object status has changed > > Return > > > Case 2) A not singleton class: > > //---------------------------------------------------------------------------------------------------// > > CLASS MyClass // NOT A SINGLETON CLASS!!! > > DATA Status INIT 0 > METHOD ChangeStatus() INLINE ::status := 1 > > ENDCLASS > > ******************************* > Procedure Main > ******************************* > > ? MyClass():Status // 0 > // first new object was instantiated > > ? MyClass():ChangeStatus() // 1 > // second new object runs :ChangeStatus() > > ? MyClass():Status // 0 > // third new object is instantiated > > Return > > </PRG> > > </code> >
Hello Leandro, I know that you want to integrate it at low level in the Harbour Object engine, but how about the following sample: <prg> #include "hbclass.ch" #xcommand SINGLETON CLASS <clsName> [ FROM <fromCls> ] => ; FUNCTION <clsName>() ;; STATIC obj ;; RETURN iif( obj = NIL, obj := S_<clsName>(), obj ) ;; CLASS S_<clsName> [ FROM <fromCls> ] #xcommand SINGLETON METHOD <mtdName> CLASS <clsName> => ; METHOD <mtdName> CLASS S_<clsName> SINGLETON CLASS MyClass DATA Status INIT 0 METHOD ChangeStatus() INLINE ::Status := 1 METHOD RevertStatus() END CLASS SINGLETON METHOD RevertStatus CLASS MyClass RETURN ::Status := 0 FUNCTION Main() ? MyClass():Status // 0 ? MyClass():ChangeStatus() // 1 ? MyClass():Status // 1 ? MyClass():RevertStatus() // 0 RETURN NIL </prg> best regards, Teo
_______________________________________________ Harbour mailing list (attachment size limit: 40KB) Harbour@harbour-project.org http://lists.harbour-project.org/mailman/listinfo/harbour