Solved

What pattern is this

Posted on 2010-09-05
3
397 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

896 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now