design patterns concern; gui and business objects

V

vangjake

i am designing a graph api. i have defined interfaces for a node, arc,
and graph. there are multiple interfaces defined (using inheritance)
defined for each of these three interfaces.

all my implementations of these interfaces have not considered fields
relevant to display on a x-y coordinate graph. now that i have coded
the implementations, i am ready to display these objects.

the problem is that i have many implementations, but they now have to
extend another interface for display. i find that i have to create a
new class to implement the existing one and implement the new interface
for each implementation.

to illustrate, i have a Node interface. there are only two methods i.e.
String getId() and void setId(String id). i have two additional
interfaces that extends this Node interface i.e. SimpleNode and
ComplexNode (these have their own pertinent methods, not listed). I
have implemented the Node interface with NodeImpl, and implemented the
other 2 interfaces with SimpleNodeImpl and ComplexNodeImpl, both of
which extends NodeImpl and each implementing SimpleNode and ComplexNode
interfaces, respectively. at this point, i define another node
interface for the purpose of drawing i.e. DrawableNode (which extends
the Node interface and has getters/setters for x and y coordinate.

my problem is, how do i get SimpleNodeImpl and ComplexNodeImpl to
implement the DrawableNode interface? i have considered the following
approaches:

1. have SimpleNodeImpl and ComplexNodeImpl implement DrawableNode as
well as what they are already implementing (i.e. SimpleNode and
ComplexNode).

2. extend SimpleNodeImpl and implement DrawableNode interface i.e.
DrawableSimpleNodeImpl (and likewise with ComplexNodeImpl).

3. create an implementation of DrawableNode, DrawableNodeImpl, and use
composition to place type Node as a field member.

i find option #1 problematic because that means i have to define
getters/setters for x and y in SimleNodeImpl and ComplexNodeImpl. this
would NOT take advantage of inheritance.

i find option #2 problematic because this would mean i would have to
extends all my node implementations just to have them drawable. (i
actually have more than 2 extensions of nodes that i want to draw).
moreover, i would also run into the problem in option #1, where i have
to define getters/setters for x and y in each of these subclasses.

i am currently leaning towards option #3, but i find the relationship
clumsy and even counterintuitive to myself; i plan to use the MVC
design pattern, and i have calculated that this approach will result in
a web of event/listener complications.

i am sure there are more options, but these are the ones that i have
thought of. the goals are to keep the inheritance simple,
implementations to a bare minimum, and have something that will fit
nicely in with the MVC design pattern.

the general question may be: how do i design the relationships so that
they will operate in the business logic layer and then be displayable
on some GUI layer? is inheritance the way? or is composition the way?
or is there yet another way that i have overlooked?
 
R

rxreyn3

Off the top of my head, I would suggest defining a adaptorfactory that
will return the needed UI object given an instance or your domain
model. Meaning, your UI needs a certain set of information that is not
relevent to the business model. So, in your UI you create a factory
that knows how to create a UI-friendly object from a specific domian
class.

Eclipse happens to use this for the same problem. There is an article
on it that shows the factory method about half way down:

http://eclipsezone.com/articles/what-is-iadaptable/

This is similar to what I have suggested and may at least give you a
good starting point.

-Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top