design patterns used in STL(standard template library) [closed]
Asked Answered
A

7

10

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

Amaryllidaceous answered 23/4, 2010 at 12:34 Comment(5)
The STL is generic programming not object oriented. You need to retag.Sun
@kts: design-patterns are not exclusive to object-oriented design.Blitzkrieg
Do you look at some STL implementation. Source code can answer you a lot of questions.Hollister
You probably won't find any of the design patterns that use subtyping.Norenenorfleet
What do you mean with 'how design patterns are implemented'? You use design patterns to implement functionality, not the other way around. Next to that, STL implementations are platform (or compiler) dependent, there is no generic implementation.Alleyne
B
17

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.

Blitzkrieg answered 23/4, 2010 at 12:34 Comment(2)
Do you have an example for an iterator adaptor in the STL?Castigate
5. Template Method (STL algorithms using user-specified functions)Standley
O
1

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.

Odontoid answered 23/4, 2010 at 13:4 Comment(5)
STL and Design Patterns are definitely related. They are both about abstractions.Blitzkrieg
Iterators is a design pattern. Definately implemented in the STL.Legwork
@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.Odontoid
@D.Shawley: That is why it is called a pattern and not a design.Legwork
@D.Shawley: The iterator IS asking the underlying collection. That's the whole point. It does not expose it to the user.Blitzkrieg
C
1

The Iterator pattern is used pretty heavily in the STL.

Castigate answered 23/4, 2010 at 13:24 Comment(0)
W
1

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.

Winter answered 23/4, 2010 at 13:29 Comment(0)
R
0

From C++11, we got threes kinds of smart pointer, i.e, shared_ptr,unique_ptr and weak_ptr, the pattern behind them is :Proxy pattern.

Red answered 24/6, 2015 at 6:35 Comment(0)
R
0

std::vector<bool>::reference and std::bitset::reference are also examples of Proxy pattern

Reporter answered 29/7, 2019 at 14:21 Comment(0)
I
0

What I am aware of from the top of my head in modern STL is:

  1. Iterators are widely spread. They are present in every container
  2. Singleton (std::cout for example)
  3. Factory methods (std::make_unique, std::make_shared etc)
  4. Visitor (std::visit with std::variant)
  5. Adapters (std::stack adapts a collection you provide as template type)
  6. Command (std::function)
  7. Chain of responsibility (ranges?)
  8. Decorator (std::reference_wrapper?)
Inhalant answered 8/8, 2024 at 14:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.