473,511 Members | 14,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is strict adherence to Adapter Pattern really a good thing?

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.

Nov 17 '05 #1
7 1830
i guess C# developers have no need for such frivolous design
patterns..LOL

Nov 17 '05 #2
Farseer,
On 21 May 2005 02:13:42 -0700, "farseer" <fa*****@optonline.net>
wrote:
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

Nov 17 '05 #3
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:ri*********@chello.nl]
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,
On 21 May 2005 02:13:42 -0700, "farseer" <fa*****@optonline.net>
wrote:
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

Nov 17 '05 #4
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..

Nov 17 '05 #5

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:fa*****@optonline.net]
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..

Nov 17 '05 #6
these are not database access objects. they are streaming socket data
which are parsed into data objects.

Nov 17 '05 #7
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/eaaCatal...taGateway.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

"farseer" <fa*****@optonline.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
| 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.
|
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6464
by: Maurice | last post by:
Hi, We are implementing some wrappers in C++ according to the Adapter Pattern. The classes and their methods in the Adaptee classes (open-source library) have already the interface that we like,...
1
5366
by: Tony Johansson | last post by:
Hello Experts! I'm reading about design patter in the GAMMA book and there is something that I don't understand. That's why I ask you. It says "Pluggable adpters. A class is more reusable when...
21
2432
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
50
5993
by: Shadow Lynx | last post by:
Consider this simple HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head>...
15
1533
by: guy | last post by:
when i first started using .net (beta 1) i came across option strict and thought hey this could be really good, and since then have always turned it on, most people here seem to agree that this is...
3
2445
by: FluffyCat | last post by:
Last month I continued my series of design patterns examples using PHP 5 with the Observer Pattern and the Prototype Pattern. Here now is my 16th example, the Adapter pattern. ...
2
2060
by: FluffyCat | last post by:
In June I continued my series of design patterns examples using PHP 5 with the Adapter Pattern. Here now is my 17th design pattern example, the Bridge Pattern. ...
2
2353
by: oliharvey | last post by:
Hi - (not really a C# question -...apologies) I seem to have gravitated towards a particlar design pattern - and would welcome your opinions as to it's sanity - thanks... The basic idea is...
92
5060
by: Erwin Moller | last post by:
Hi group, I encoutered page validation error, but I don't know a way around. The page has the following doctype: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.