Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Design Pattern library?

What is the most common C++ Design Pattern libraries?

I've read about Loki library in Alexandrescu's book, but looks like it somewhat dead now. Is there something similar out there?

like image 264
cody Avatar asked Jul 04 '13 07:07

cody


People also ask

Does C have design patterns?

Yes, there are. Lazy initialization, singleton, object pool, object state etc.

What is a design pattern library?

What is a pattern library? A 'pattern library', as the name suggests, is a library of user interface (UI) patterns that designers and design teams use to build digital products. They are referred to as 'patterns' because they're recurring solutions that solve design problems.


3 Answers

“Design patterns are bug reports against your programming language” -- Peter Norvig

To answer the question why there are not many C++ design pattern libraries, it is useful to know what design patterns were meant to solve in the first place. The classic GoF book states in the preface

Design patterns describe simple and elegant solutions to specific problems in object-oriented software design.

The 90s style of object-oriented programming relied heavily on using abstract classes as interfaces, with concrete implementation classes deriving from these interfaces. The GoF patterns describe creational, structural and behavioral relationships between objects of different class types. They key element was: encapsulate and parameterize whatever will frequently change. Many of the GoF patterns can also be reformulated using templates, but then the flexibility is constrained to compile-time rather than run-time.

Object-oriented programming makes it very easy to add different concrete implementations of an interface. What OOP has a hard time with is adding new functionality to existing interfaces. The Visitor pattern is the prime example: it is essentially a work-around that relies on an extra level of indirection to allow new algorithms to work on existing data structures.

This is the exact opposite of functional programming: with functional programming it is very easy to add new functions for existing data, but it is much harder to add new data types to which such functions apply. The difficulty in getting extensibility in both functions and types is called the expression problem.

OOP style polymorphism is heavily based on internal polymorphism: the dynamic function dispatch is based on the object's type. Modern C++ also uses external polymorphism where techniques such as type erasure allow run-time flexibility with a static interface. The new std::shared_ptr and boost::any or adobe::poly classes are prime example of these techniques.

A recent ACCU presentation by Tobias Darm showed many examples of transforming the old internally polymporhic GoF patterns to this new style externally polymorphic patterns. The rough idea is to replace abstract classes with a function argument that can take std::function as a parameter. The std::function then controls the polymorphic flexibility from the outside. Many of the GoF patterns can be greatly enhanced in terms of boilerplate this way.

TL;DR: The classic GoF patterns were tailored to solve OOP shortcomings. But OOP is no longer the dominant C++ style. A combination of generic programming (the Standard Library, Boost) and OOP can solve many problems more elegantly, making classic design patterns no longer the go-to solution.

like image 184
TemplateRex Avatar answered Oct 12 '22 16:10

TemplateRex


The original definition of a design pattern was a reusable approach to a reoccurring problem that could not be conveniently encapsulated in a library. Thus, the moment you can encapsulate a pattern in a library, it ceases to be a pattern, in my opinion. This has, for instance, largely happened with iterators in C++, as the standard C++ library has a comprehensive framework for implementing iterators now.

I’ve never tried to use Loki, but reading Alexandrescu’s book, I was not persuaded that a library based approach really had much to offer for many patterns.

like image 17
microtherion Avatar answered Oct 12 '22 15:10

microtherion


May seems tautology, but the most common is ... the standard library itself!

It is not -strictly speaking- a "pattern library", but a folder for a number of tools addressing common pattern implementation.

Note that your question is not answerable, being a pattern just a conceptual definition commonly used in a variety of problems. Libraries don't provide patterns, they (can) use patterns (like anybody else can) to provide implementation of specific problem solutions.

Patterns are at an higher abstraction layer than coding.

like image 3
Emilio Garavaglia Avatar answered Oct 12 '22 15:10

Emilio Garavaglia