473,809 Members | 2,668 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...deleg ation 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 ACMERowDataAdap ter 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 ACMERowDataAdap ter 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 1846
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*****@optonl ine.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.publi c.dotnet.langua ges.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*****@optonl ine.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.publi c.dotnet.langua ges.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*****@optonl ine.net> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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...deleg ation 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 ACMERowDataAdap ter 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 ACMERowDataAdap ter 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
6477
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, but we want to rename them so we want to implement the Adapter classes in such a way that we only have to rename the Adaptee classes. We prefer to use #define's because of the better run-time performance, in stead of implementing wrapper...
1
5385
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 minimize the assumption other classes must make to use it. By building interface adaptation into a class, you eliminate the assumption that other classes see the same interface. Put another way, interface adaptation lets us incorporate our class...
21
2472
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 maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
50
6090
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> <title>Strict kills my widths!</title> </head> <body> <table style="width:400px; table-layout:fixed;">
15
1551
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 a good thing. i have now been asked to debug a vb2005 web app for 3 weeks and there is no mention of option strict in it, (there are also no classes defined, just a couple of structures) everything is define as 'as object', data coming back...
3
2457
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. http://www.fluffycat.com/PHP-Design-Patterns/Adapter/ In the Adapter Pattern we adapt a class we have to provide methods another class needs, without changing the original class. If you work with a lot of classes that you can not change, then this
2
2072
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. http://www.fluffycat.com/PHP-Design-Patterns/Bridge/ In the Bridge Pattern we have an abstraction and implementation in different class hierarchies. Not something I use all the time, but if you have a situation where a
2
2371
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 that - in an effort to keen the Business Classes as clean as possible - I have *totally* excluded any data load/ persistance code.
92
5190
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" "http://www.w3.org/TR/html4/strict.dtd"> Some hyperlinks need to open to a new window, so I used the (wrong) target="" attribute.
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.