• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Am I coupling too tightly? (Or am I just nuts?)

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on a little project on the side to create an application that will generate mock SCJP exams tailored to a user. For example, a user could indicate that they'd like to take a full exam, or just be tested on specific areas.
Anyway, my question comes up with my use of an MVC pattern. I originally brought up a question about this in this thread in which I got some very good feedback. From that, I planned to have a servlet be my controller but I was going to add some "action classes" that would separate my model classes from the servlet. This way, my model classes wouldn't have to by aware that they were being used in a servlet siutation and could be used just as easily in a standard Java application because the action classes would handle putting data into and retrieving data from the request and session.
Now, with that goal in mind (that the model would be unaware of its useage environment), I've been working on the design of the model. In that, I have a Question class which has a number of members, including an array of Links. The Link class really just contains two pieces of data: text, and a URL. Then, I put a toString() method in the Link class that would write out the link as an HTML anchor tag.
My code looks something like this:

Now, I know this design will work, but my original goal was to make the model classes unaware that they're being used in a web-based environment. But, my Link class has a method thaht turns the Link into HTML and the Question class would have a similar method that would turn that into HTML. From that, it would seem that I haven't coupled my model classes loosely enough.
Now, I could take all of the code to turn a Question into HTML out of the Question class and put that code into the action classes, but then it seems like my action classes become "Godlike" and my model classes are nothing more than data holders.
I don't really plan to reuse this code in a normal application anytime in the future - I'm just somewhat of an idealist (especially on my own little projects) and would like to strive for this type of decoupling.
This post has already become much too lengthy, but I thought I'd get some input from others around here. Am I missing the boat on the MVC pattern? Am I really coupling my model too closely to my controller/usage environment? Or am I just worrying about things that don't need to be worried about?
Thanks,
Corey
[ June 06, 2002: Message edited by: Corey McGlone ]
 
Sheriff
Posts: 17735
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might try looking into the Decorator pattern and see if that's applicable in your situation.
Junilu
 
Sheriff
Posts: 67759
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct in identifying that the Links are clearly not part of your view-unspecific model. Think of how Links might be used in a console version of your application.
What are the Links linking your Question to? Other Questions? What you'd like to do is to abstract the linkages to model objects rather than a linkage mechanism (HTML anchors). Then the view-specific controller for your application can apply whatever mechanism is necessary to represent the linkages.
hth,
bear
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is that yes, you are coupling too tightly. Creating MVC where the V and C are reasonably decoupled is tricky, but do-able. Even if you don't end up using it, I would recommend at least looking at Struts. The idea it uses is to have beans bound into the (typically request) scope. The JSP picks up the bean data and formats it. As a result, the bean knows little about how it will be rendered.
I've often thought that "MVC" was missing a letter, that there should be a "D" or "L" ('domain' or 'language'), to reflect that you would be passing certain kinds of data around, but you wouldn't think of that data as being parts of the model... just communication artifacts. In Struts the form beans really serve that purpose. The action class creates incremental controller connection functionality, but the bean is how that controller talks to the view (JSP).
As for being nuts, I can't answer that. I'd have to know I'm not nuts to know which conclusion I could safely draw.
[ June 06, 2002: Message edited by: Reid M. Pinchback ]
 
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Corey,
I guess u need a layer of abstraction between the model class and the servlet.But ideally speaking why would u need such a scenario where u want the class which does all the processing to be unaware of the environment,context to which it belong..I don't foresee any reason why would you need it.
If your concern is to make reusable-units in form of java classes which can be used independently as an application, then yes what you propose is correct.
But to maintain a loose coupling between the controller and a model in case of MVC is too difficult.
In this case wht u propose of having an action class which takes up request parameters, the environment and context and passes in onto the model class which has the data.If that is wht u propse I believe u need to have a class whose composition could be
Class Links{
Model m1;
private Links []referenceSites;
Vector v=new Vector();
if(!(v.elementAt(i).equals(referenceSite[i])){
v.add(refernceSite[i]);
}
m1.process(v.elementAt(i).toString());
}
you can check the vector , if it does not contains the link it add to it, and uses m1, the instance variable in Link class to call its method which has the data and which does the processing,
class Model{
public String process("siteURL"){
}
}
This way I think u will be able to achieve loose coupling between the servlet and model classes;
 
The only thing that kept the leeches off of me was this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic