Solved

What pattern is this

Posted on 2010-09-05
3
414 Views
Last Modified: 2012-06-27
Hi,

I saw that in boost::asio.
Maybe someone knows name of this pattern and why/when should i use it or why not.
Its remainds me pimpl idiom.
Pattern looks like this

 
template <typename ServiceName>
class basic_service
{
  typedef ServiceName ImplType
public:
     Init()
     {
        impl_.Init();
     }
private:
    ImplType impl_
};

From what i see. I could pass different types in template argument: 
basic_service<Service1> aaa
basic_service<Service2> bbb

Open in new window

0
Comment
Question by:lavitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 33608791
>>  From what i see. I could pass different types in template argument:

Yes you are correct, you can pass other types in this manner. What you are looking at is a template class. A template class is one in which you write a common set of logic once and you have the ability to reuse that logic by passing different types into the template parameter. Think of template classes in the same way you were taught classes--but instead of creating a blueprint that will be used to recreate objects of the same type, you are creating a blueprint to recreate logic for different types. Templates allow you to write logic once and reuse that same logic on multiple types of classes. Other languages, such as C# and Java, refer to this style as Generics.

I believe C++ also has the concept of templated functions--where only a single function is templated, not an entire class.
0
 
LVL 22

Assisted Solution

by:ambience
ambience earned 100 total points
ID: 33610185
I think it's called proxy class pattern, also famously known as pimpl pattern in c++ jargon.The basic_service wrapper will proxy calls to the implementation and hence a proxy class.
0
 
LVL 1

Expert Comment

by:XMarshall10
ID: 33610533
I agree that this is an pimpl pattern.

The code: ImplType impl_
  is a forward declaration of an opaque pointer, details of which is implemented in the implementation file (generally the corresponding CPP file).

In C++ jargon, this is also known as Compiler firewall.

-XM
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question