views:

208

answers:

4

I am learning STL and design patterns . i wanted to know is there any document or link that explains how design patterns are implemented in STL i did the google but not able to get much data

+2  A: 

I think that your problem is that design patterns are not implemented in STL. They can be implemented in C++ and use containers and algorithms from STL but STL and Design Patterns are not related in any other way.

My advice would be to learn about STL by reading something like Nicolai Josuttis' excellent book The C++ Standard Library: A Tutorial and Reference or STL Tutorial and Reference Guide. This will help in learning what the STL can do for you. Then dig into implementing design patterns in C++ using your knowledge about the STL.

D.Shawley
STL and Design Patterns are definitely related. They are both about abstractions.
Eddy Pronk
Iterators is a design pattern. Definately implemented in the STL.
Martin York
@MartinYork - Despite the name, I do not agree that the implementation of iterators in STL is an implementation of the Iterator design pattern any more than pointers are. The canonical Iterator pattern includes a method to determine whether traversal will no longer yield items without asking the underlying collection.
D.Shawley
@D.Shawley: That is why it is called a pattern and not a design.
Martin York
@D.Shawley: The iterator IS asking the underlying collection. That's the whole point. It does not expose it to the user.
Eddy Pronk
+4  A: 

I hope you mean, "which design patterns can be identified in the STL".

The STL stack is a container adapter. An adapter is a design pattern. The iterator is also a design pattern. The STL function objects are related to the command pattern.

Patterns:

  1. Adapter (container adapters)
    • stack
    • queues
    • priority queues
  2. Iterator
  3. Command + Adapter (function adapters)
  4. Iterator + Adapter (iterator adapters)
    • reverse iterators
    • insert iterators
    • stream iterators
  5. Template Method (STL algorithms using user-specified functions)
  6. Which creational pattern? (Allocators)

The way these patterns are implemented is very different from the way they are implemented in an object oriented design. Josuttis wrote "the STL concept contradicts the original idea of object-oriented programming". This is what is causing the confusion around your question.

Eddy Pronk
Do you have an example for an iterator adaptor in the STL?
@dehmann: I added them. Feel free to edit my answer.
Eddy Pronk
5. Template Method (STL algorithms using user-specified functions)
Gabriel Ščerbák
+1  A: 

The Iterator pattern is used pretty heavily in the STL.

+1  A: 

STL makes extensive use of templates. GoF call this parameterized types. Templates are useful for customizing a design pattern solution or in coming up with a new, intuitive solution. (For more details, see the section "Inheritance versus Parameterized Types" in "Design Patterns: Elements of Reusable Object-Oriented Software"). The advantage of getting familiar with STL (and boost) is that they are a good source to learn about templates (and meta-programming) in C++, which in turn can be used in devising better designs.

Vijay Mathew