Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Implementing Adapter Pattern with class and method renaming

Reply
Thread Tools

Implementing Adapter Pattern with class and method renaming

 
 
Maurice
Guest
Posts: n/a
 
      10-03-2003
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 functions.

In our case we have an Adaptee class that looks like this (simplified
example):

class Adaptee
{
public:
int Method1(int in);
int Method2(void);
private:
int Attribute;
};

This Adaptee class is implemented and build in a library.

Now we create an Adapter class (derived from a (abstract) Target
class) that is defined in the .h-file and looks like this (simplified
example):

class Adapter : public Target
{
public:
int my_method_1(int in);
int my_method_2(void);
};

In the corresponing .cpp-file we include this .h-file and implement
the Adapter class in the following way:

#define Adapter Adaptee
#define my_method_1 Method1
#define my_method_2 Method2

If we build our Adapter and Target classes into a library, everything
goes fine. There are no compile or link errors.

Now we use our self-created library in an application, but then we got
linking errors to tell us that we have: "unresolved external symbols"
on the methods that are part of the Adapter classes. Our application
looks like this (simplified example):

int main(int argc, char *argv[])
{
int Result;
Adapter MyAdapter;
Target *pMyTarget = &MyAdapter;

Result = pMyTarget->my_method_1(0);
Result = pMyTarget->my_method_2();

return Result;
}

So my question is now. Can someone explain me what we are doing wrong
and how we can solve these linking errors?

Thanks,
Maurice
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      10-03-2003
On 3 Oct 2003 06:34:51 -0700, (Maurice) wrote:

>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 functions.


Inlined wrapper functions shouldn't suffer a performance hit -
performance should be indentical to the #define approach (and it will
actually work, unlike the #define approach).

>
>In our case we have an Adaptee class that looks like this (simplified
>example):
>
> class Adaptee
> {
> public:
> int Method1(int in);
> int Method2(void);
> private:
> int Attribute;
> };
>
>This Adaptee class is implemented and build in a library.
>
>Now we create an Adapter class (derived from a (abstract) Target
>class) that is defined in the .h-file and looks like this (simplified
>example):
>
> class Adapter : public Target
> {
> public:
> int my_method_1(int in);
> int my_method_2(void);
> };


Ok so far. You've got a library (and header) for Adaptee, and you've
created a header for your own class Adapter, derived from Target.

>In the corresponing .cpp-file we include this .h-file and implement
>the Adapter class in the following way:
>
> #define Adapter Adaptee
> #define my_method_1 Method1
> #define my_method_2 Method2


Where are the defines? Before the #includes?

What you are doing is creating the following class (after
preprocessing):

class Adaptee : public Target
{
public:
int Method1(int in);
int Method2(void);
};


>If we build our Adapter and Target classes into a library, everything
>goes fine. There are no compile or link errors.


But you never build a class called "Adapter"! You build one called
Adaptee.

>Now we use our self-created library in an application, but then we got
>linking errors to tell us that we have: "unresolved external symbols"
>on the methods that are part of the Adapter classes.


I'm not surprised, since you haven't compiled the methods of a class
called Adapter.

Our application
>looks like this (simplified example):
>
> int main(int argc, char *argv[])
> {
> int Result;
> Adapter MyAdapter;
> Target *pMyTarget = &MyAdapter;
>
> Result = pMyTarget->my_method_1(0);
> Result = pMyTarget->my_method_2();
>
> return Result;
> }
>
>So my question is now. Can someone explain me what we are doing wrong
>and how we can solve these linking errors?


Well, I've said what's wrong. To fix it you have two options.
1. Modify the source of Adaptee.cpp to this:

was something like:
#include "Adaptee.h"
//member definitions.

change it to something like:
#include "Adaptor.h" //your header!

#define Adaptee Adapter
#define Method1 my_method_1
#define Method2 my_method_2
//member definitions unmodified

and recompile Adaptee.cpp into its library, where it will actually be
linked in as Adaptor.


Or, much better, you can to do things properly:

#include "Target.h"
#include "Adaptee.h" //from library

class Adapter : public Target, private Adaptee
{
public:
int my_method_1(int in)
{
return Method1(in);
}
int my_method_2(void)
{
return Method2();
}
};

Because the functions are defined inline in the class, you shouldn't
get any performance hit.

Tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Implementing a singleton pattern on a given session RSH ASP .Net 4 10-10-2007 05:01 PM
Implementing fp pattern matching, using C++ Ole Nielsby C++ 2 09-18-2006 09:54 PM
A class implementing the archiver pattern eyal.susser@gmail.com C++ 2 04-28-2005 02:31 PM
Implementing Mediator Pattern in C++ cppaddict C++ 2 08-05-2004 12:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57