• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in model design

 
Sylven Yip
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.

i designed model with servlet in the past.
in these days i am learning apache struts.
and it seems struts design model with normal java class.

now i have some problem about the efficiency:
if i design model with normal java class, is it the model objects in memory as much as request?
if i design model with servlet,is there only one object with multiple thread in the memory?

whitch is more effective?
thank you.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that it is generally considered "good practice" to keep your model separate from the servlet. The model doesn't need to know who is asking for information, or how it is going to be displayed. This would mean that the servlet takes care of requests and responses, and asks the other classes for the necessary information and resources.

I do not know what this means in terms of efficiency, but I am pretty sure that in most cases it will make it a lot easier to read and maintain the application in the long run.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Katrina. The MVC pattern expects a separation of model, view, and controller components. A Servlet and a Struts Action class are both controller components, and should therefore not contain model logic. They should instantiate and call methods on model objects as required, but should not directly contain model logic.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sylven Yip:

if i design model with normal java class, is it the model objects in memory as much as request?
if i design model with servlet,is there only one object with multiple thread in the memory?


I found the above two statements confusing.Could you please elaborate those.

Just to add to what has been previously posted for this very thread , keeping the model code independent of the servlet api would allow to unit test the model outside a srvlet container.
 
Sylven Yip
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone!
that means put models in normal java class is good for isolation,right?

but if i put model into a servlet,can i save more memory than do the same thing in normal java class?

let me make specify it as fllow:
beacuse a servlet class only has one instance in runtime,and process request just by multithread.
now if i do the same thing in a normal java class,i thing,in the servlet controller:

in this way,the controller create instance for every request(one request one instance),that's a big wast e of momery.
is it wrong?
[ September 19, 2007: Message edited by: Sylven Yip ]
 
siva kumar
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...

I don't understand some thing,

Is it true?
that there will be only one instance of servlet in memory...it could be container dependent... ( a container might also create a pool of servlets in order to serve multiple requests)

I suppose the spec says that there will be only that the methods in servlet interface are not thread safe (unless you use single thread model Interface -depecrated)

First coupling on servlet interface with model is a very bad idea�.

If you are so concerned about memory� then it would be great to embed the JDBC/ other DB interface code directly into the controller methods. - That way you wouldn�t need any other objects

(May be you can use model 1 architecture; where you wont have servlets too; you will use only JSP's)


Model is a pure abstraction that lies outside the preview of web-layer, why use servlet interface for the model class.

If you want only one model object in memory there are many ways to achieve it � (like using singleton)

The best practice in industry is to use the following layers

Controller � Business Objects � Data Access Objects � DB/File System ect�.

I would suggest what ever optimization you do needs to be done at DAO layer
 
Sylven Yip
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks siva.
one servlet has one instance,i think that's true.it was emphasized in many books.let's think about instance field of servlet,it's shared by every request(though not thread safe).but if servlet pool as you talk about,how can fields be shared? is it obey servlet standards?

singleton is not a good idea,bad performance is more worse than momery wasted.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sylven Yip:
thanks siva.
one servlet has one instance,i think that's true.it was emphasized in many books.let's think about instance field of servlet,it's shared by every request(though not thread safe).but if servlet pool as you talk about,how can fields be shared? is it obey servlet standards?


Servlets are only pooled if they implement SingleThreadModel, otherwise, it's one instance per servlet declaration, per JVM.
 
Sylven Yip
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm worry about big amount of Concurrency.there are too many model object's......
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm worry about big amount of Concurrency.there are too many model object's..


What does concurrency have to do with it? Your worry is too ill defined to address directly.

In my online exam servlets I typically have a "model" object that represents one user's current exam state. This object is only associated with one user so there is no concurrency problem. When the user is finished the model object is discarded so the memory used only depends on the number of concurrent users.

There are also "models" of exams - but they are never modified by users so there is no concurrency problem.

Don't go imagining problems where there are none - just follow good practice.

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!