caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeffrey Palmer <jeffrey.palmer@acm.org>
To: Michael Vanier <mvanier@cs.caltech.edu>, caml-list@inria.fr
Subject: Re: [Caml-list] a design problem requiring downcasting? (long)
Date: Thu, 26 Sep 2002 10:53:52 -0500	[thread overview]
Message-ID: <200209261053.52231.jeffrey.palmer@acm.org> (raw)
In-Reply-To: <200209260901.g8Q910t03459@orchestra.cs.caltech.edu>

On Thursday 26 September 2002 4:01 am, Michael Vanier wrote:
> It turns out that the design of the core simulator doesn't pose any
> real problems.  However, part of the simulation infrastructure is
> very difficult to achieve without having a way of downcasting
> (casting from a supertype to a subtype).  It can be done, but the
> design becomes extremely non-modular.  The issue is that these
> simulations can contain tens of thousands of simulation objects which
> can invoke method calls on each other.  These are actually binary
> method calls e.g.
>
>   obj1#my_method obj2
>
> where "obj2" has to be of a specific class type (in actuality, it
> will be an interface type).  These method calls are highly dependent
> on the specific types of the objects.  If you create the objects like
> this:
>

I'm assuming that each object in the simulation has the ability send a 
"class-specific" message to all of the other objects in the system. (If 
this isn't correct, then please let me know). With this in mind, 
subclassing gets you very little, as most methods aren't applicable (in 
good OO design) at the abstract class level.

I can think of at least three options that can get you what you want 
without downcasting:

1) Use double-dispatch, along the lines of the Visitor pattern. You 
could implement an abstract Visitor class that defaults to no operation 
for most of the classes you'd like to cover, and then subclass 
appropriately to handle the cases that are required for a specific 
method/class combination. I've used this technique extensively (in 
Java), and it's worked well. (You might have to combine this with a 
variant of #2).

2) The problem you're having is because the method "message" isn't smart 
enough to know what it applies to. As they say, abstraction solves most 
problems, so perhaps you could implement a Message class that would be 
passed from object to object (you might not really need objects, at 
that point). You could make some clever use of HOFs and tags to allow 
each object to implement a "does this message apply to me" test, and if 
so it executes it.

3) Since you're already storing all of the objects in an array, why not 
use a subclass-specific storage and pass class-specific messages only 
to that collection. This doesn't handle shared methods so well 
(duplicate storage), but it is an option. You could then implement a 
message dispatcher that knows which collection to use for various 
message types.

Anyway, those are three that I came up with off the top of my head. 
Perhaps I've misunderstood your problem, though. If so, please let me 
know and I'd be happy to try to come up with some more options.

	- jeff

-- 
The river is moving.
The blackbird must be flying.

-------------------
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


  parent reply	other threads:[~2002-09-26 15:53 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-26  9:01 Michael Vanier
2002-09-26 14:32 ` Remi VANICAT
2002-09-26 15:19 ` nadji
2002-09-26 15:27   ` Remi VANICAT
2002-09-26 15:55     ` nadji
2002-09-26 15:53 ` Jeffrey Palmer [this message]
2002-09-26 16:35 ` Oleg
2002-09-26 17:47 ` brogoff
2002-09-26 19:14 ` Fred Smith
2002-09-27 17:01   ` Tim Freeman
2002-09-26 22:46 ` Alessandro Baretta
2002-09-27  7:20 ` Francois Pottier
2002-09-27 10:16   ` Michael Vanier
2002-09-29 22:59     ` Alessandro Baretta
2002-09-30  7:09       ` Michael Vanier
2002-09-30  9:54         ` Alessandro Baretta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200209261053.52231.jeffrey.palmer@acm.org \
    --to=jeffrey.palmer@acm.org \
    --cc=caml-list@inria.fr \
    --cc=mvanier@cs.caltech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).