caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] prevent more than one object
@ 2003-07-02 16:14 Mirko Aigner
  2003-07-02 16:43 ` Nick Name
  2003-07-03 11:20 ` John Max Skaller
  0 siblings, 2 replies; 4+ messages in thread
From: Mirko Aigner @ 2003-07-02 16:14 UTC (permalink / raw)
  To: caml-list

Hi !

I'm looking for a way to prevent that from a class more than one object 
is builded. Therefor I wrote this little files

(*singleton.ml*)
class singleton x =
   object
     val mutable data : int = x;
     method get_data = data;
     method set_data newdata = data <- newdata;
   end;;

let instance = new singleton 0

let get_instance = instance

(*singleton.mli*)
class singleton :
   int ->
   object
     method get_data : int
     method set_data : int -> unit
   end;;

val get_instance : singleton

The problem is the class is still public and instances can be build, any 
way to prevent this ?!

thx for helping
Mirko Aigner

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] prevent more than one object
  2003-07-02 16:14 [Caml-list] prevent more than one object Mirko Aigner
@ 2003-07-02 16:43 ` Nick Name
  2003-07-03 11:20 ` John Max Skaller
  1 sibling, 0 replies; 4+ messages in thread
From: Nick Name @ 2003-07-02 16:43 UTC (permalink / raw)
  To: Mirko Aigner, caml-list

On Wed, 02 Jul 2003 18:14:23 +0200
Mirko Aigner <Mirko.Aigner@alcatel.de> wrote:

> 
>  The problem is the class is still public and instances can be build,
>  any way to prevent this ?!

I would make the class private in the module and supply only access
functions; if you need it to be a class you should expose only the class
type, but there is Oo.copy so your value will never be granted unique.

Try this

module M : sig
  class type singleton = object
    method f : int
  end

  val x : singleton
end = 
struct
  class singleton = object
    method f = 0
  end

  let x = new singleton
end

You can't create a new singleton with "new" but still can do that with
Oo.copy M.x. It is better to completely hide the type if you can, as in

module M : sig
  type singleton

  val x : singleton
end = struct ...

Vincenzo

-- 
Ho dato al mio dolore la forma di parole abusate
che mi prometto di non pronunciare mai più
[CSI]

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] prevent more than one object
  2003-07-02 16:14 [Caml-list] prevent more than one object Mirko Aigner
  2003-07-02 16:43 ` Nick Name
@ 2003-07-03 11:20 ` John Max Skaller
  2003-07-03 11:36   ` Mirko Aigner
  1 sibling, 1 reply; 4+ messages in thread
From: John Max Skaller @ 2003-07-03 11:20 UTC (permalink / raw)
  To: Mirko Aigner; +Cc: caml-list

Mirko Aigner wrote:

> Hi !
> 
> I'm looking for a way to prevent that from a class more than one object 
> is builded. 


Why? Its a design error. Fix your design.

There's never any need for a singleton object,

because there is no resource for which there
cannot be more than one. I have seen (C++) designs
using singletons and they create an utter mess
because someone wrongly assumed something was
unique.

If you want one object, define the class in your
mainline, create it, and then coerce it to
a shared class type which does not provide a constructor.

-- 
John Max Skaller, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] prevent more than one object
  2003-07-03 11:20 ` John Max Skaller
@ 2003-07-03 11:36   ` Mirko Aigner
  0 siblings, 0 replies; 4+ messages in thread
From: Mirko Aigner @ 2003-07-03 11:36 UTC (permalink / raw)
  To: caml-list

John Max Skaller wrote:

> Why? Its a design error. Fix your design.
> 
> There's never any need for a singleton object,
> 
> because there is no resource for which there
> cannot be more than one. I have seen (C++) designs
> using singletons and they create an utter mess
> because someone wrongly assumed something was
> unique.
> 
> If you want one object, define the class in your
> mainline, create it, and then coerce it to
> a shared class type which does not provide a constructor.
> 

Maybe you're right, but this is not the right place to discuss the 
usefullnesss of the design patterns described by the GoF.
Vincente and Wagner describe in their paper, that singleton pattern 
could be implemented with modules. 
sdg.lcs.mit.edu/~dnj/6898/projects/vicente-wagner.pdf

I was testing it and got some problems, mentioned it here and got some 
hints how to solve the problem.

THANKS to all helping me with it!!

Mirko Aigner


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-07-03 11:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-02 16:14 [Caml-list] prevent more than one object Mirko Aigner
2003-07-02 16:43 ` Nick Name
2003-07-03 11:20 ` John Max Skaller
2003-07-03 11:36   ` Mirko Aigner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).