Is strict adherence to Adapter Pattern really a good thing?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • farseer

    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.

  • farseer

    #2
    Re: Is strict adherence to Adapter Pattern really a good thing?

    i guess C# developers have no need for such frivolous design
    patterns..LOL

    Comment

    • Rick Elbers

      #3
      Re: Is strict adherence to Adapter Pattern really a good thing?

      Farseer,


      On 21 May 2005 02:13:42 -0700, "farseer" <farseer@optonl ine.net>
      wrote:
      [color=blue]
      >Here is the scenario:
      >[/color]

      Your whole design is driven by the thought that *
      [color=blue]
      >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).[/color]

      *

      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



      Comment

      • Sa¹o Zagoranski

        #4
        Re: Is strict adherence to Adapter Pattern really a good thing?

        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:rick.elb ers@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" <farseer@optonl ine.net>
        wrote:
        [color=blue]
        >Here is the scenario:
        >[/color]

        Your whole design is driven by the thought that *
        [color=blue]
        >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).[/color]

        *

        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



        Comment

        • farseer

          #5
          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..

          Comment

          • Saso Zagoranski

            #6
            Re: Is strict adherence to Adapter Pattern really a good thing?


            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:farseer@ 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..

            Comment

            • farseer

              #7
              Re: Is strict adherence to Adapter Pattern really a good thing?

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

              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: Is strict adherence to Adapter Pattern really a good thing?

                farseer,
                Have you considered using the Data Mapper Pattern?

                A layer of mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself.


                Or possibly a Data Gateway Pattern?

                An object that acts as a gateway to a database table. One instance handles all the rows in the table.


                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.

                Details of books I've written, and those in my signature series


                Hope this helps
                Jay

                "farseer" <farseer@optonl ine.net> wrote in message
                news:1116666822 .553432.303360@ 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.
                |


                Comment

                Working...