Re: Design issue....
mojtaba_danai@yahoo.com <mojtaba_danai@yahoo.com> wrote:
Hi
I am trying to make a adapter program.
-There can be different kind of adapters, which each can take data from
one source.
-It should be possible to plugin the new adapter for a new source of
data.
-The adapters all make a common structure to give it to the connected
clients. (I have implemented this part).
-Each adapter uses a specific API to collect data from data source.
How the design of the adapter class should look like? Which design
pattern?
There should probably be a superclass for all adapters, right? But they
have different APIs, as I said before, and one of the adapters inherits
already from couple of data source API classes.
You have a collection of reader/writers [devices] that read/write
various data
to all or parts of a commonly used user defined type say Message.
You want your code to read or write these Messages via these
devices without the code knowing actual method the data is transferred.
Correct??
struct Message;
wtruct transfer_base
{
virtual void get(message &) = 0;
virtual void put(message &) = 0;
virtual ~transfer_bbase(){}
protected;
transfer_base(){}
};
we need concrete classes to use the various devices, since each
requires a reference to the device and specific instructions to
do the transfer, we can separeate the actual transfer functions from
the hoder of the device reference and the virtual functions get(),put().
/* Action is a struct with two static functions
static void get(somedevice &,Message &);
static void put(somedevice &,Nessage &);
*/
template <class Device,class Action>
class transfer:public transfer_base
{
Device &d;
pubilc:
transfer(Device &x):d(x);
void get(message &m) {Action::get(d,m);}
void put(message &m) {Action::put(d,m);}
};
for each device we have a struct
template <class D> struct DeviceAction;
template <> struct DeviceAction<device>
{
static void get(device_1 &d,Message &m);
static void put(device_1 &d,Message &m);
};
for instance
template <class D>
inline
std::auto_ptr<transfer_base> make_transfer(D &d);
{
return std::auto_ptr<transfer_base>(
new tranfer<D,DeviceAction<D> >(d)));
}
is a possible factory function.
only specific code is in the specializations of DeviceAction<D>
constaining static functions to get/put data to/from a Device, needa
writing to 'connect a Device' to your code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]