Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this design pattern? How to use it?

Suppose I have class like this (simplified):

class Foo_p;
class Foo
{
private:
  Foo_p *p;
public:
  Foo();
  /* methods, etc... */
};

This class is a part of an API. The Foo_p is all the private parts of the class, which are not declared in the class Foo itself as usual, but rather in a separate forward-declared class that is only used by the underlying implementation not visible on the outside.

I've seen this pattern used in a couple of projects, is there a name for it?

Also, how do I use it properly (e.g. exception safety, etc.)? Where should the actual implementation go? In class Foo, as usual, only using Foo_p for storage of data, or in the Foo_p class with Foo being just a wrapper?

like image 668
kralyk Avatar asked Jan 31 '12 20:01

kralyk


People also ask

How do I know what design pattern to use?

To use design patterns effectively you need to know the context in which each one works best. This context is : Participants — Classes involved. Quality attributes — usability, modifiability, reliability, performance.

What is design pattern and why we use it?

A design pattern provides a general reusable solution for the common problems that occur in software design. The pattern typically shows relationships and interactions between classes or objects. The idea is to speed up the development process by providing well-tested, proven development/design paradigms.

What is design pattern with example?

Design patterns provide a standard terminology and are specific to particular scenario. For example, a singleton design pattern signifies use of single object so all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.

What is design pattern in design?

In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.


1 Answers

That is the pimpl idiom

See

  • Handle/Body Idiom - close cousin and the seminal idea for pimpl
    EDIT Found an online copy of this original James Coplien talk
  • GotW #100: Compilation Firewalls (Difficulty: 6/10)
  • GotW #101: Compilation Firewalls, Part 2 (Difficulty: 8/10) for the most recent on this
like image 170
sehe Avatar answered Oct 22 '22 09:10

sehe