Re: Object creation overhead
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/27/03
- Previous message: nos: "Re: Object creation overhead"
- In reply to: nos: "Re: Object creation overhead"
- Next in thread: nos: "Re: Object creation overhead"
- Reply: nos: "Re: Object creation overhead"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 27 Nov 2003 01:00:13 GMT
"nos" <nos@nospam.com> wrote in message
news:shbxb.316149$Tr4.986273@attbi_s03...
>
> please, what is a "proxy" object? how to make one?
>
It's just a plain object that is used to represent or take the place of
another object. While useful in many situations [i.e. there can be many
reasons for using proxies including security reasons, and minimising
resources loadtimes etc], one particular use is to reduce the memory
footprint of a Collection-type of data structure.
For example, you need to have an 'ArrayList' object manage 1000 objects of
'MyClass'; this class looks like:
class MyClass
{
...
// Lots of fields ...
private long id;
private String name;
private String address;
...
}
If 'MyClass' has twenty 'String' fields then that means that memory for
20,000 'String' objects is needed, both to manage them, and for the 'String'
data. This is, of course, over and above the memory required for the
'MyClass' objects themselves.
The 'proxy' class approach won't see a reduction in the number of 'MyClass'
objects, merely a reduction in the amount of memory each may require for its
field data. So, the original class is now split into two; there is the proxy
class:
class MyClassProxy
{
...
// Only a couple of fields needed...
private long id;
...
// Method to retrieve the actual object proxied by this one
public MyClass getMyClassObject() { ... }
...
}
and a modified, probably pared-down, version of the original class:
class MyClass
{
...
// Still lots of fields, but probably less methods ...
private long id;
private String name;
private String address;
...
}
With the 'ArrayList' object loaded with 'MyClassProxy' objects, less memory
is being used. Only when a 'MyClassProxy' object is selected is a 'MyClass'
object instantiated, using data stored somewhere else *outside memory* [e.g.
file, database, URL, etc].
Note:
* This approach has many variants - I have provided a very simple
example, and ignored / glossed over the implementation issues.
However the general approach is:
- Use proxy object in place of real object
- When proxy object is selected have it retrieve / create
the real object
* Proxy cannot [usually] be used where access / retrieval time is
crucial
More substantial examples, and relevant in-depth discussions may be found by
Google searching for 'Proxy Design Pattern'.
I hope this helps.
Anthony Borla
- Previous message: nos: "Re: Object creation overhead"
- In reply to: nos: "Re: Object creation overhead"
- Next in thread: nos: "Re: Object creation overhead"
- Reply: nos: "Re: Object creation overhead"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|