Browse thread
OO design
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Gerd Stolpmann <info@g...> |
| Subject: | Re: [Caml-list] OO design |
Am Freitag, den 05.05.2006, 11:35 +0200 schrieb David Baelde:
> Hi,
>
> I'm no OO guru, so my question may be irrelevant, or there just might
> not be an answer, which wouldn't hurt..
>
> Let's say that I have a base class, with some kind of activation
> procedure: anybody wanting to use the class must call #enter before,
> and then call #leave for releasing. Internally, the methods #do_enter
> and #do_leave are called respectively at the first #enter and last
> #leave.
>
> Nobody should call the #do_* directly, and I'd also like to make sure
> the #enter and #leave are never overriden, since their behaviour is
> important and actually much more complex than what I said.
>
> I could just rely on the user who derives my base class, but let's see
> what we can do. First the #do_* should be made private, so they can be
> defined in the derived classes, but never called from the outside. To
> avoid the overriding of #enter and #leave the only solution seems to
> make them normal functions instead of methods. But then how could
> #enter call #do_enter ? I tried to first define the class with public
> #enter and make that method private in the interface, but OCaml told
> me that was impossible.
>
> I'm just curious if anybody has an opinion/idea about that.
There is an easy solution if you completely forbid subclassing (see
below). However, there is no water-proof solution, because class types
are structural in O'Caml, i.e. you cannot prevent that a user simulates
subclassing using this style:
class pirate_foo (foo : official_foo) =
object
method enter = ...
method leave = ...
method other_method = foo # other_method
end
If you want to safely encapsulate a certain invariant into a structure
you must go with modules/functors in O'Caml.
To forbid explicit subclassing just do not to export the class as such:
module Foo : sig
class type foo_type =
object
method enter : ...
method leave : ...
...
end
val create_foo : ... -> foo_type
end = struct
class type foo_type =
object
method enter : ...
method leave : ...
...
end
class foo ... : foo_type =
object
method enter ... = ...
method leave ... = ...
...
end
let create_foo ... = new foo ...
end
Without class, the user can no longer inherit from it. The created
object, however, is fully usable.
I do not see a way how to completely hide enter and leave from the class
type.
Gerd
--
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany
gerd@gerd-stolpmann.de http://www.gerd-stolpmann.de
Phone: +49-6151-153855 Fax: +49-6151-997714
------------------------------------------------------------