Solved

What pattern is this

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

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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

21 Experts available now in Live!

Get 1:1 Help Now