77

I have only heard the term "design pattern" being used for object oriented code, and the GoF patterns include only OOP design patterns, but design patterns are elegant solutions for commonly occuring programming problems, right? There is nothing in there saying that they must be limited to OOP, is there?

I would like to see some examples of design patterns outside the realm of object oriented programming. Do you have any? Do such even exist (no book, like the GoF book, must necessarily have been written, they should just be used; that is enough)?

They can be specific to some programming language(s), but general (paradigm-level) patterns are preferred, of other paradigms than the object oriented one.

Anto
  • 11,197

10 Answers10

11

"Design pattern" is actually an euphemism for "workaround". The design patterns were invented to work around shortcomings and flaws in OO languages. For example take the iterator pattern which eventually led to the introduction of the collections in Java. Groovy got rid of many more patterns by converting them into language features: You no longer need the decorator pattern because you can add methods to existing classes in Groovy.

This means you can find design patterns everywhere. In fact every "best practice" can be considered a simple form of a design pattern.

ElleJay
  • 103
11

LtU mentions that Jeremy Gibbons is writing a book on patterns in functional programming. Check out Mr. Gibbon's Patterns in Functional Programming blog for a few teasers. Note he recommends reading his posts from oldest-to-newest.

His paper Design Patterns as Higher-Order Datatype-Generic Programs (pdf) functionally models the Gang of Four patterns: Composite, Iterator, Visitor, and Builder. He describes the patterns of programming with recursive equations in Origami Programming (folds and unfolds).

Corbin March
  • 8,114
11

Actually it is a paradox - one of the most popular Non-OO patterns is... "Class".

Because OO was not invented in non-OO languages, developers had to simulate it (and they're doing it even right now) - so the pattern was born. LISP and C are examples of this.

But take my advice: Don't do common mistake - don't use patterns only because it is cool, you need serious reasons to justify use of pattern (at least OO ones).

Take Command pattern for example - although it's nice & it decouples caller from receiver, it shouldn't be used unless you actually need that - because operations should be expressed using verbs - which means methods. And using commands everywhere, you would end up with a bunch of completely decentralized OO-lambdas -> same would be true for a lot of Strategies.

lennon310
  • 3,242
10

There are SQL design patterns.

And some functional design patterns exist as well - see here for scala.

Oded
  • 53,734
8

Instead of naming non-oo design patterns I would like to give you a few examples of books that have many design patterns (in them some patterns will still be OO specific):

Hope this helps

KeesDijk
  • 8,968
2

In functional programming (especially Haskell), there are a lot of patterns and idioms that don't map very well to OOP. Phantom types are a well-known example, and you can find lots more on the haskell wiki page on Idioms.

yatima2975
  • 216
  • 1
  • 3
1

A good example of non-OOP patterns is my absolute favourite pattern catalog: Organizational Patterns of Agile Software Development, by James O. Coplien. This book isn´t about software patterns, is about people, a catalog for creating successful teams. Every manager should read this book!

JuanZe
  • 570
1

Modular programming is quite popular for numerical libraries and math-heavy applications (numerical software is notoriously hard to model using the Object Oriented patterns, mostly because there is very little you can encapsulate).

quant_dev
  • 5,227
0

I like to think of Data Strucutres such as Queues, linked Lists, Trees, graphs, etc as patterns. They define certain patterns to store data and process them. They may seem primitive as compared to Gang of Four's more high end patters but they are patterns nevvertheless. I mean, what would happen if someone implemented a stack as a FIFO instead of a LIFO and vice versa for a queue and named them otherwise?

DPD
  • 3,527