Is strict adherence to Adapter Pattern really a good thing?

F

farseer

Here is the scenario:

I have an interface which defines get methods for data that will make
up a row in a table. However, the source of this data may, over time,
switch/change (The company may choose to change data providers).
Therefore i thought to myself, a type of Adapter Pattern is best here
and so i proceeded with that. here's an example of what i did (note
this implementation differs from the text book one due to the way data
must be handled...delegation would not be as clean here).

public interface IRowData {
public String getName()
public String getDescription()
public float getValue()
public float getHigh()
public float getLow()
public float getNetChange()
}

public interface IDataAdapter() {
public void adaptData( Data data )
}

public interface IRowDataAdapter extends IRowData, IDataAdapter {}

*** Since the app needs the ability to easily switch from one data
provider to another, the Adapter Pattern seems ideal here. The app
receives real-time data. These data come in packets and different
packets contain different parts of the data that will constitute an
entire row. So here, my Adapter will have to differ a bit from the
standard Adapter design pattern. In this case, an Adapter actual
adapts many types of data to the interface i define above. it looks
something like this.

public class ACMERowDataAdapter implements IRowDataAdapter {
private String _name;
private String _description;
private float _value;
private float _high;
private float _low;
private float _netChange; //calculated field

public String getName(){ return _name; }
...
public float getLow(){ return _low }

public void adaptData( Data data )
{
if ( data instanceof CompanyInfo)
//set _name and _description
else if ( data instanceof CompanyData )
//populate _value, _high, _low and calculate _netChange
}
}

In the above, code, I have simplified things greatly..but just assume
in this case the reason for this type of implementation of the Adapter
pattern is because simple delegation using the "adaptee's"
methods is not as easy or clean, esp considering some values are
calculated and data is from various types.

Ok, so here is my question finally. This data, IRowDataAdapter, will
represent one row in a JTable. So in essence, my TableModel will also
employ the adapter pattern, this time in the stricter sense,
implementing TableModel, and delegating to IRowDataAdapter. That table
may contain thousand's of rows, so performance is a concern. MY
QUESTION: wouldn't it be more efficient to break the "Adapting
code" out into a separate class, have one instance of that class that
takes a IRowData and populates it with data received? That way, you do
not have that "adaptData" method duplicated for ever row in your
table...that just seems like a waste of memory to me.
With the above implementation, EVERY ROWDATA carries with it some
unnecessary code (adaptData), with thousand's of objects, this could
add up, no?

In short, what is the best practice for this case...keeping in mind
speed is of utmost importance.


In the above, code, I have simplified things greatly..but just assume
in this case the reason for this type of implementation of the Adapter
pattern is because simple delegation using the "adaptee's" methods is
not as easy or clean, esp considering some values are calculated and
data is from various types.

Ok, so here is my question finally. This data, IRowDataAdapter, will
represent one row in a JTable. So in essence, my TableModel will also
employ the adapter pattern, this time in the stricter sense,
implementing TableModel, and delegating to RowDataAdapter. That table
may contain thousand's of rows, so performance is a concern.
MY QUESTION: wouldn't be more efficient to break the "Adapting code"
out into a separate class, have one instance of that class that takes a
IRowData and populates it with data received? That way, you do not
have that "adaptData" method duplicated for ever row in your
table...that just seems like a waste of memory to me.
With the above implementation, EVERY ROWDATA carries with it some
unnecessary code (adaptData) it seems, with thousand's of objects,
this could add up, no? Why not just have ONE class, with the following

method


public class ACMERowDataAdapter implements IDataAdapter{
...
public IRowData adaptData( IRowData appData, Data foreignData)
{...}
...
}


In short, what is the best practice for this case...keeping in mind
speed is of utmost importance.
 
R

Rick Elbers

Farseer,


Here is the scenario:

Your whole design is driven by the thought that *
I have an interface which defines get methods for data that will make
up a row in a table. However, the source of this data may, over time,
switch/change (The company may choose to change data providers).

*

This is a much overrated scenario. Change of dataproviders dont happen
often, so its a very invalid driver for design. Try to encapsulate
real variability is much better.

Rick
 
S

Sa¹o Zagoranski

I made a windows service, designed to work with different data providers
(particularly sql server and oracle)...

so instead of using
if ( dataprovider == "oracle" ) ... else if ( dataprovider ==
"sqlserver" )

I used the interfaces...

IDbCommand, IDbConnection...

Or did you have in mind something else?

-----Izvirno sporoèilo-----
Od: Rick Elbers [mailto:[email protected]]
Poslano: 22. maj 2005 12:44
Objavljeno v: microsoft.public.dotnet.languages.csharp
Pogovor: Is strict adherence to Adapter Pattern really a good thing?
Zadeva: Re: Is strict adherence to Adapter Pattern really a good thing?

Farseer,


Here is the scenario:

Your whole design is driven by the thought that *
I have an interface which defines get methods for data that will make
up a row in a table. However, the source of this data may, over time,
switch/change (The company may choose to change data providers).

*

This is a much overrated scenario. Change of dataproviders dont happen
often, so its a very invalid driver for design. Try to encapsulate
real variability is much better.

Rick
 
F

farseer

Sa'o,
yes, that's exactly it.

in essence, the application has defined a set of Data objects it knows
about and it knows how to operat on. The adapter allows data to of
different format to be fed to my application, it, in essence, converts
(and encapsulates) the incoming data to the object that the appication
expects.

I disagree with the previous poster that this is overrated. i am find
the need from these often. For intance, now that i have the data
adapter working, i finding this may also be a good design priniciple to
apply for displaying of that data in a table.
For instance, i have a generic implementation of a table that operates
a DataSet (which contains DataRows and which contains DataItem). the
table knows how to operate on those objects. So here might be a case
where i need a tableAdapter to allow the table to know about my data
objects..
 
S

Saso Zagoranski

If so... why write your own classes? Why not just use
the interfaces in .NET.
All valid .NET data providers have to abide by those standards...


-----Izvirno sporoèilo-----
Od: farseer [mailto:[email protected]]
Poslano: 22. maj 2005 20:02
Objavljeno v: microsoft.public.dotnet.languages.csharp
Pogovor: Is strict adherence to Adapter Pattern really a good thing?
Zadeva: Re: Is strict adherence to Adapter Pattern really a good thing?

Sa'o,
yes, that's exactly it.

in essence, the application has defined a set of Data objects it knows
about and it knows how to operat on. The adapter allows data to of
different format to be fed to my application, it, in essence, converts
(and encapsulates) the incoming data to the object that the appication
expects.

I disagree with the previous poster that this is overrated. i am find
the need from these often. For intance, now that i have the data
adapter working, i finding this may also be a good design priniciple to
apply for displaying of that data in a table.
For instance, i have a generic implementation of a table that operates
a DataSet (which contains DataRows and which contains DataItem). the
table knows how to operate on those objects. So here might be a case
where i need a tableAdapter to allow the table to know about my data
objects..
 
F

farseer

these are not database access objects. they are streaming socket data
which are parsed into data objects.
 
J

Jay B. Harlow [MVP - Outlook]

farseer,
Have you considered using the Data Mapper Pattern?

http://www.martinfowler.com/eaaCatalog/dataMapper.html

Or possibly a Data Gateway Pattern?

http://www.martinfowler.com/eaaCatalog/tableDataGateway.html

For the few times I've used them, they seemed to fit very well.

Martin Fowler's book "Patterns of Enterprise Application Architecture" from
Addison Wesley has a number of other patterns that you may find useful here.

http://www.martinfowler.com/books.html#eaa

Hope this helps
Jay

| Here is the scenario:
|
| I have an interface which defines get methods for data that will make
| up a row in a table. However, the source of this data may, over time,
| switch/change (The company may choose to change data providers).
| Therefore i thought to myself, a type of Adapter Pattern is best here
| and so i proceeded with that. here's an example of what i did (note
| this implementation differs from the text book one due to the way data
| must be handled...delegation would not be as clean here).
|
| public interface IRowData {
| public String getName()
| public String getDescription()
| public float getValue()
| public float getHigh()
| public float getLow()
| public float getNetChange()
| }
|
| public interface IDataAdapter() {
| public void adaptData( Data data )
| }
|
| public interface IRowDataAdapter extends IRowData, IDataAdapter {}
|
| *** Since the app needs the ability to easily switch from one data
| provider to another, the Adapter Pattern seems ideal here. The app
| receives real-time data. These data come in packets and different
| packets contain different parts of the data that will constitute an
| entire row. So here, my Adapter will have to differ a bit from the
| standard Adapter design pattern. In this case, an Adapter actual
| adapts many types of data to the interface i define above. it looks
| something like this.
|
| public class ACMERowDataAdapter implements IRowDataAdapter {
| private String _name;
| private String _description;
| private float _value;
| private float _high;
| private float _low;
| private float _netChange; //calculated field
|
| public String getName(){ return _name; }
| ...
| public float getLow(){ return _low }
|
| public void adaptData( Data data )
| {
| if ( data instanceof CompanyInfo)
| //set _name and _description
| else if ( data instanceof CompanyData )
| //populate _value, _high, _low and calculate _netChange
| }
| }
|
| In the above, code, I have simplified things greatly..but just assume
| in this case the reason for this type of implementation of the Adapter
| pattern is because simple delegation using the "adaptee's"
| methods is not as easy or clean, esp considering some values are
| calculated and data is from various types.
|
| Ok, so here is my question finally. This data, IRowDataAdapter, will
| represent one row in a JTable. So in essence, my TableModel will also
| employ the adapter pattern, this time in the stricter sense,
| implementing TableModel, and delegating to IRowDataAdapter. That table
| may contain thousand's of rows, so performance is a concern. MY
| QUESTION: wouldn't it be more efficient to break the "Adapting
| code" out into a separate class, have one instance of that class that
| takes a IRowData and populates it with data received? That way, you do
| not have that "adaptData" method duplicated for ever row in your
| table...that just seems like a waste of memory to me.
| With the above implementation, EVERY ROWDATA carries with it some
| unnecessary code (adaptData), with thousand's of objects, this could
| add up, no?
|
| In short, what is the best practice for this case...keeping in mind
| speed is of utmost importance.
|
|
| In the above, code, I have simplified things greatly..but just assume
| in this case the reason for this type of implementation of the Adapter
| pattern is because simple delegation using the "adaptee's" methods is
| not as easy or clean, esp considering some values are calculated and
| data is from various types.
|
| Ok, so here is my question finally. This data, IRowDataAdapter, will
| represent one row in a JTable. So in essence, my TableModel will also
| employ the adapter pattern, this time in the stricter sense,
| implementing TableModel, and delegating to RowDataAdapter. That table
| may contain thousand's of rows, so performance is a concern.
| MY QUESTION: wouldn't be more efficient to break the "Adapting code"
| out into a separate class, have one instance of that class that takes a
| IRowData and populates it with data received? That way, you do not
| have that "adaptData" method duplicated for ever row in your
| table...that just seems like a waste of memory to me.
| With the above implementation, EVERY ROWDATA carries with it some
| unnecessary code (adaptData) it seems, with thousand's of objects,
| this could add up, no? Why not just have ONE class, with the following
|
| method
|
|
| public class ACMERowDataAdapter implements IDataAdapter{
| ...
| public IRowData adaptData( IRowData appData, Data foreignData)
| {...}
| ...
| }
|
|
| In short, what is the best practice for this case...keeping in mind
| speed is of utmost importance.
|
 

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

Top