views:

627

answers:

9

I was searching for "Undo/Redo algorithms" and found something marked as a duplicate, but the duplicate was a request for a "Undo Design Pattern". I'd really like an algorithm for this. I don't think I necessarily need a design pattern.

Is there a fundamental difference between "Design Pattern" and "Algorithm" or is it OK that someone uses the two interchangeably?

I'll hang up and take my answer off the air.

Ok, forgive me for thinking design patterns were just abstractions of algorithms. Here's a little table of my findings from the answers which were all very good.

   Design Pattern      |      Algorithm
----------------------------------------------
Abstract               |Concrete
Control Structure      |Set of Actions     
Template               |
OOP                    |Structured / OO Programming
Flexible               |Deterministic 
Architecture(blueprint)|Recipe
A: 

A design pattern would determine how you design an algorithm (or may have nothing to do with algorithms, but let's say we're dealing with one that does), whereas an algorithm will be a set of solid, repeatable, implementable, steps to doing something. So no, I wouldn't call them interchangable.

Bernard
+12  A: 

an algorithm is like a recipe: a step-by-step process for performing some activity

a design pattern is like a blueprint: an structured collection of objects and associations and actions to accomplish some goal

Steven A. Lowe
+7  A: 

A design pattern is a relatively vague description of how to solve a problem at an architectural level, with emphasis on flexibility and maintainability. An algorithm is a precise description of how to compute something specific, with an emphasis on correctness and efficiency.

dsimcha
+1  A: 

I would say that a design pattern defines structure, whereas an algorithm defines behaviour.

For example, you might use several different algorithms in conjunction with the Strategy design pattern.

Dan Dyer
+2  A: 

A design pattern can't be directly translated to code. It is a "strategy" that can be useful for the design of an application. The origin of the term is external to computer science. Read about Christopher Alexander to learn more.

An algorithm on the other side can be expressed in code. It is a sequence of operations that solve a specific problem for any input.

kgiannakakis
+7  A: 

Yes, there is a difference.

An algorithm is a recipe for performing some task - an unambiguous finite set of instructions that achieves some goal by operating on an input and producing an output. Typically an algorithm is expressed in a language-agnostic pseudo-code, which can then be implemented in the language of your choice.

A design pattern is a way of structuring your code in order to elegantly express a relationship between functional components. You might use design patterns within the implementation of an algorithm. For example, you might use an algorithm for an in-order walk of a tree to ensure that you visit all the nodes of a tree data structure in a certain order. You might also implement a visitor design pattern to express how your implementation returns control to the calling context to indicate that a node has been visited. This is not part of the algorithm, but part of the software design, and how you structure the interfaces that each component of your software can use.

Algorithms and design patterns are orthogonal, although they may well both be used at the same time.

HenryR
+1  A: 

An algorithm is a specific set of steps to perform a task. Decoding an audio or video file would use an algorithm.

A design pattern is more of a template for designing a system with certain characteristics.

Dana Holt
+1  A: 

An algorithm is a set of steps/actions/commands/instructions that work in a specified order/manner across all circumstances or state changes. In the case of undo/redo, it would involve storing previous state at each juncture and then reproducing it (through whatever means the app has) on command. But since this definition is so fuzzy and each particular case is different, we like to create a more generalized design pattern into which a specific app's functionality can be plugged-in.

A design pattern is a more abstract concept that exists within object-oriented programming as a result of objects' encapsulation of their inner values and workings and polymorphism allowing for potentially different behaviors from the "same" function calls. All this makes it possible to build control structure into the objects' interaction rather than into an algorithmic structure like a conditional or a loop, and can be much simpler since you don't need to know an object's details to make it work - you only need to know that the object works. In the case of undo/redo, there could be objects for remembering state, for reproducing state, and for managing control. They would communicate with each other by calling methods/functions, which would each handle their own part of the functionality.

The terms are not interchangeable because they refer to different levels of design, and DP's in particular are only a part of object-oriented programming.

notnot