473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Implementing Adapter Pattern with class and method renaming

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
Jul 19 '05 #1
1 6455
On 3 Oct 2003 06:34:51 -0700, ma*************@ict.nl (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 applicationlooks 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
Jul 19 '05 #2

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

Similar topics

4
by: cantabile | last post by:
Hi, I'm trying to write a small installer for a server. But this program should be able to run in the future under heterogenous environments and os (at least linux/windows). I mean, the install...
1
by: TEK | last post by:
Hello I'm wondering if anyone out there might give some input/suggestions/viewpoints around the Command pattern. In my case, the number one priority for using the pattern is undo support. Some...
2
by: cppaddict | last post by:
I have a design question which I am posting here because the implementation will be in C++ and I think there may be C++ specific language constructs (eg, friends) that might be relevant to the...
1
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...
3
by: Ivan Neganov | last post by:
Hi, I have a custom subclass of System.IO.Stream type. I wonder how to correctly implement the IDisposable pattern in this situation. The parent Stream type apparently uses explicit interface...
8
by: Mark Neilson | last post by:
1. What is the best way to make a single instance of my top level class (DLL) internally available to all other members of the assembly? The top level object is where all other access is made in...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
3
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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...

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.