using "this" keyword and loading data from caches

E

epicwinter

I am working on optimizing loading of objects. Here is my problem I
use a base class DataAccessObject.

public class DataAccessObject
{
/**
* gets instance of DataAccessObject represented by the id and loads
the data
*
**/
public DataAccessObject(int id)
{
//load data here
}
}

So to get the shape object from the db specified by the 2 id:
Shape aShape = new Shape(2);

Now for some of my DataAccessObject sub clases I would like to store a
cache that will be loaded the first time it is called. So I want to
override this constructor to instead grab the data from the cache like
so:

public Cached_DataAccessObject(int id)
{
this = cache.get(new Integer(id));//the cache is a map
}

But you can't do that cause the "this" key word is not intended to be
referenced in this manner. Does anyone know of a way to make this
work? Or a better alternative?

thanks
 
R

Robert

Your DAO should implement methods like initialize() load(), save(),
create(), update(). Things of this nature. Here we use service
objects and daos. Most of the daos only know about the above methods
and things like find(). Then you can have a DAO Registry make the
class specific object you need at runtime. This a long one, you're
probably gonna need to get a book.
 
E

epicwinter

Thanks for the reply. I realize the right way to proceed is to
separate my dao into more of a pojo and a service object. Then the
service object can have methods like getShape(int id). And yes I might
need several books. But nevertheless I am still interested in getting
this hack to work for the time being.
 
M

Mike Schilling

I am working on optimizing loading of objects. Here is my problem I
use a base class DataAccessObject.

public class DataAccessObject
{
/**
* gets instance of DataAccessObject represented by the id and loads
the data
*
**/
public DataAccessObject(int id)
{
//load data here
}
}

So to get the shape object from the db specified by the 2 id:
Shape aShape = new Shape(2);

Now for some of my DataAccessObject sub clases I would like to store a
cache that will be loaded the first time it is called. So I want to
override this constructor to instead grab the data from the cache like
so:

public Cached_DataAccessObject(int id)
{
this = cache.get(new Integer(id));//the cache is a map
}

But you can't do that cause the "this" key word is not intended to be
referenced in this manner. Does anyone know of a way to make this
work? Or a better alternative?

Use a factory:

public DataAccessObjectFactory {
public getDataAccessObject(int id) {
DataAccessObject obj = cache.get(id);
if (obj == null) {
obj = new DataAccessObject ();
// load object
}
return obj;
}
}
 
T

Tor Iver Wilhelmsen

override this constructor

No, you want to make the constructor unavailable to the "client" (ie.
private) and instead use a factory method which can look in the cache.
 
J

Joseph Dionne

I am working on optimizing loading of objects. Here is my problem I
use a base class DataAccessObject.

public class DataAccessObject
{
/**
* gets instance of DataAccessObject represented by the id and loads
the data
*
**/
public DataAccessObject(int id)
{
//load data here
}
}

So to get the shape object from the db specified by the 2 id:
Shape aShape = new Shape(2);

Now for some of my DataAccessObject sub clases I would like to store a
cache that will be loaded the first time it is called. So I want to
override this constructor to instead grab the data from the cache like
so:

public Cached_DataAccessObject(int id)
{
this = cache.get(new Integer(id));//the cache is a map
}

But you can't do that cause the "this" key word is not intended to be
referenced in this manner. Does anyone know of a way to make this
work? Or a better alternative?

thanks

Make your Cached_DataAccessObject() a singleton, allowing only the first
initial Object instance. Add a public final static
Cached_DataAccessObject.newInstance() that will be used to restore an
instance of a cached Object.
 
E

epicwinter

Thanks for all the suggestions. I guess the factory route is the way
to go. That way you can have a cache in your factory and let it manage
it for you i guess

i have been looking at spring as well and might try to eventually
migrate in that direction however this is not a webapp.
 
T

Tor Iver Wilhelmsen

i have been looking at spring as well and might try to eventually
migrate in that direction however this is not a webapp.

Spring is not foremost a "webapp" framework but a "inversion of
control" framework (sort of like Swing can be). The important part is
how components (objects) are put together (using an "assembler"), not
what the components are.
 
E

Eric Sosman

I am working on optimizing loading of objects. Here is my problem I
use a base class DataAccessObject.

public class DataAccessObject
{
/**
* gets instance of DataAccessObject represented by the id and loads
the data
*
**/
public DataAccessObject(int id)
{
//load data here
}
}

So to get the shape object from the db specified by the 2 id:
Shape aShape = new Shape(2);

Now for some of my DataAccessObject sub clases I would like to store a
cache that will be loaded the first time it is called. So I want to
override this constructor to instead grab the data from the cache like
so:

public Cached_DataAccessObject(int id)
{
this = cache.get(new Integer(id));//the cache is a map
}

But you can't do that cause the "this" key word is not intended to be
referenced in this manner. Does anyone know of a way to make this
work? Or a better alternative?

Can't be done as envisioned: the constructor must actually
initialize a brand-new object; it cannot "recycle" an already-
existing object that's lying around in some cache. (Consider
what would happen if somebody subclassed DataAccessObject ...)

Instead, I'd suggest using a factory method:

public class DataAccessObject {
private DataAccessObject {
// for internal use only
}
public DataAccessObject getInstance(int id) {
// load data if need be
DataAccessObject dao = getFromCache(id);
if (dao == null) {
dao = new DataAccessObject(...);
addToCache(dao);
}
return dao;
}
}

This class isn't extendable (because there's no way a
subclass can call the constructor); if that's a problem,
consider making the constructor protected instead of private.
 
E

epicwinter

True, but from what i have seen spring only runs inside webcontainers
whether that be tomcat, jetty or jboss. I am working on a system that
will require remote access and spring seems to do this extremely well
from the webapp side. But from what little I have seen, the mechanisms
they have for using swing with a remote server seem to be limited. I
am hoping the HttpInvoker might provide a solution here, but I am
rather skeptical.
 
E

epicwinter

Robert said:
Your DAO should implement methods like initialize() load(), save(),
create(), update(). Things of this nature. Here we use service
objects and daos. Most of the daos only know about the above methods
and things like find(). Then you can have a DAO Registry make the
class specific object you need at runtime. This a long one, you're
probably gonna need to get a book.

I decided to go with this migration. I got a couple best practices
questions. My data objects now are rather monolithic. They implement
crud, have finder methods in them, and also have some business logic
methods that are specific to them.

If I want to go to a more layered structure like this do I need 2 or 3
layers?
My guess is:
1)One layer is simply the dao bean object that implements like a crud.

2)A factory object that contains instantiators and static finder
methods.

3)What do I do with the business code though? Should this be in a
separate layer or can I just put it in the factory object? Or am I way
off????

thanks
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top