Solved

What pattern is this

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

822 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