473,692 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(voi d);
};

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 6470
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(voi d);
};
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(voi d)
{
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
1256
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 will be done either in text mode or curses or gtk or tk, either in debian or windows 2000 and so on... The idea, at the moment, is as follows :
1
2555
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 now, a lot more in the future. I think I have a pretty good understanding about how the command pattern is tought to work, however during implementation I'm htting a couple of quite large design considerations that I'm not sure I have the answare...
2
3595
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 solution. I am implementing the Mediator pattern to create an event-driven app. The Collaborators (which watch for various events and which all have a reference to the Mediator object) will call handleEvent(Event e) on the Mediator object when...
1
5373
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...
3
4503
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 implementation, and I could not find a way for my child type to override parent's IDisposable.Dispose() method. The intention was to clean up child's resources first,
8
1833
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 to the program. Where and how do I declare and initialise this object? For instance, it would have property implementation like this: //******************************************* public MyClass1 Class1 {
4
3327
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, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
3
2452
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
2366
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.
0
8608
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9086
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
8963
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...
1
8803
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6459
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
4324
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
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1959
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.