Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

polymorphism between packages

Thread view: 
VisionSet - 06 Jan 2006 17:26 GMT
That subject line may be misleading, but you'll get the drift...

Say we have model and view tiers/packages
In the model tier we have some subclasses that no how to carry out there
respective task()
We instantiate one of these subclasses depending on some condition.
The subclass merrily polymorphically carries out task().

However now we need to display something in the view and what we display
depends on the subclass instantiated.
I obviously don't want to put view logic in the subclass though that would
mean it could polymorphically deliver what is required.  And I don't want a
load of conditional logic in the view.

I don't want to do isFlavourX(), isFlavourY() etc that is as bad.

some kind of view wrapper in a mirrored subclass hierachy, don't like those
much either. And I'd still need to know which wrapper to use for the
subclass I'm given.

Any ideas?

TIA
--
Mike W
Robert Klemme - 06 Jan 2006 17:43 GMT
> That subject line may be misleading, but you'll get the drift...
>
[quoted text clipped - 17 lines]
>
> Any ideas?

IMHO if you're out for a clean solution and there is no easy common
functionality that you can use for displaying (i.e. toString() or a method
getStatus() that's defined in the base class and probably overriden in sub
classes) you need the mirrored class hierarchy.  You could use a
Map<ModelClass, ViewWrapperClass> to look up the class that you need to
instantiate at Runtime as wrapper.

My 0.02 EUR...

   robert
iamfractal@hotmail.com - 06 Jan 2006 18:50 GMT
> I obviously don't want to put view logic in the subclass though that would
> mean it could polymorphically deliver what is required.
Snippage.

> TIA
> --
> Mike W

You don't have to put the view logic in model subclass hierarchy, but
there's nothing wrong with having the *selection* of a particular view
interface in the model subclass hierarchy.

A la:
class TruckModel {
  go() {
     ViewFacade view = registry.getViewFacade();
     ThingPresentation thing =
viewFacade.getPresentation(ViewFacade.BIG_THING);
     thing.draw();
  }
}

class CarModel {
  go() {
     ViewFacade view = registry.getViewFacade();
     ThingPresentation thing =
viewFacade.getPresentation(ViewFacade.SMALL_THING);
     thing.draw();
  }
}

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition
VisionSet - 06 Jan 2006 21:05 GMT
> > I obviously don't want to put view logic in the subclass though that would
> > mean it could polymorphically deliver what is required.

> You don't have to put the view logic in model subclass hierarchy, but
> there's nothing wrong with having the *selection* of a particular view
> interface in the model subclass hierarchy.

There is in MVC, the model should have no knowledge of any view, the view is
client to the model.
This is the situation I have.

--
Mike W
John C. Bollinger - 07 Jan 2006 03:06 GMT
>>You don't have to put the view logic in model subclass hierarchy, but
>>there's nothing wrong with having the *selection* of a particular view
[quoted text clipped - 3 lines]
> client to the model.
> This is the situation I have.

I think you're looking at a job for the (C)ontroller part of your MVC
application.  When the controller triggers the polymorphic task, it
should also set up the view appropriately for displaying the result.
Presumably it has enough information to do so.  (If it doesn't, then you
probably haven't separated the model, view, and controller roles
rigorously enough.)

Your original post also touched on a question of the structure of the
view layer, which I think is quite distinct from the issue of where the
logic should go.  You may not need a hierarchy of view components
mirroring your model hierarchy, but it is difficult to give concrete
recommendations for so abstract a problem.  The main alternative to a
hierarchy of view classes would be one or a small number of view classes
that are sufficiently configurable to handle the view requirements of
all the model classes you have or can conceive.  I don't know how this
might mesh with your requirements.

Signature

John Bollinger
jobollin@indiana.edu

iamfractal@hotmail.com - 08 Jan 2006 18:42 GMT
Snippet.

> There is in MVC, the model should have no knowledge of any view, the view is
> client to the model.
> This is the situation I have.
>
> --
> Mike W

Actually, you probably know more about MVC than I do, but just for the
record, when I'm MVCing (which perhaps in a purist sense, I don't) I
tend to re-interpret your line above as, "... the model should have no
knowledge of any view *implementation* ..." I do allow my models to
poke at the View *interface* whenever it's
(Productivity/Punctuality/Quality) cheapest.

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition
Chris Smith - 10 Jan 2006 05:04 GMT
> Actually, you probably know more about MVC than I do, but just for the
> record, when I'm MVCing (which perhaps in a purist sense, I don't)

Correct.  That's not the classical MVC pattern.  You would avoid
confusion by referring to it as "modified MVC" or "pseudo-MVC" or some
such thing.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

iamfractal@hotmail.com - 09 Jan 2006 01:52 GMT
Snwip.

> There is in MVC, the model should have no knowledge of any view, the view is
> client to the model.
> This is the situation I have.
>
> --
> Mike W

Actually, you probably know more about MVC than I do, but just for the
record, when I'm MVCing (which perhaps in a purist sense, I don't) I
tend to re-interpret your line above as, "... the model should have no
knowledge of any view *implementation* ..." I do allow my models to
poke at the View *interface* whenever it's
(Productivity/Punctuality/Quality) cheapest.

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition
iamfractal@hotmail.com - 09 Jan 2006 03:49 GMT
(This crashed during first post - apologies if now double-posted.)

Snwip.

> There is in MVC, the model should have no knowledge of any view, the view is
> client to the model.
> This is the situation I have.
>
> --
> Mike W

Actually, you probably know more about MVC than I do, but just for the
record, when I'm MVCing (which perhaps in a purist sense, I don't) I
tend to re-interpret your line above as, "... the model should have no
knowledge of any view *implementation* ..." I do allow my models to
poke at the View *interface* whenever it's
(Productivity/Punctuality/Quality) expedient.

.ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition
Thomas Hawtin - 07 Jan 2006 23:26 GMT
> I don't want to do isFlavourX(), isFlavourY() etc that is as bad.

Sounds like the sort of problem the visitor pattern aims to solve.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

VisionSet - 08 Jan 2006 12:54 GMT
> > I don't want to do isFlavourX(), isFlavourY() etc that is as bad.
>
> Sounds like the sort of problem the visitor pattern aims to solve.

Not familiar with this.
I've just got familiar with it and don't think it helps with the - what
subclass have I got? part.
I can visit the subclass polymorphically, but how do I know what methods I
can call on the subclass without knowing the subclass - which brings me back
to isFlavourX() or worse instanceof

--
Mike W
Chris Uppal - 08 Jan 2006 13:22 GMT
> However now we need to display something in the view and what we display
> depends on the subclass instantiated.

Then you have got yourself into a slight tangle, and must expect to use more
tangled logic to get out of it again.

What you have is an architecture where the various possible models are /not/
mutally substitutable, but where you are trying to treat them as if they are.
So, /somewhere/, you will need to have logic that breaks the classical MVP/MVC
rules.

Some options:

Something must have created the model.  You could require that also to "know"
the correct corresponding view type.  (This is roughly the same as John's
suggestion).

You could use a lookup table to map model types to view types (Robert's
suggestion).

You could make the model aware of what the corrrect corresponding view type is
(note that this is not classical MVC, but but it nevertheless does not break
the more important rule that the model does not depend on, any /specific/ view
instances that may be connected to it).

If you build the view dynamically, then the model could be required to "know"
what aspects it has that are available for
displaying/printing/rendering/observing/etc.  You construct a view instance
based on that data.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.