What's the difference between an Algorithm and a Design Pattern

后端 未结 12 806
轮回少年
轮回少年 2020-12-24 13:54

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 al

12条回答
  •  有刺的猬
    2020-12-24 14:47

    How to organize types of features rather than specific features might be key to separate 'design patterns' from 'algorithms'...

    Design patterns describe generic solutions to common design problems. "Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice" (Christopher Alexander) In programming this is done by describing specific sets of relationships between software objects (standing in for conceptual or real world objects). Describing specific implementation should be avoided, because it makes the design pattern less generic.

    An algorithm is a set of steps that define how a task is performed. The execution of each step in an algorithm does not require creative skills. Rather, it requires only the ability to follow directions. (warning: nondeterministic algorithms, do not conform to this restriction and are an important topic of research)

    So, I think one description of the relationship might be to separate features from functions. However, the collection of features of an object will determine its function since each sub-feature has functions encapsulated in it. When you put together many small objects into one bigger object (e.g. instances of different classes into a program) some of them will work together to create new functions which they didn't have by themselves (the whole is greater than the sum of its parts). You can say that this is just a new algorithm, but it is also a new object. Features and functions are two sides of the same coin, so it is impossible to separate them completely. But how to organize types of features rather than specific features might be key to separate 'design patterns' from 'algorithms' since if design patterns are about organizing specific features, i.e. instances of specific classes, then the algorithm would already have been presented and the implementation would be exactly the same every time, i.e. it would not be generic and you can't "use this solution a million times over, without ever doing it the same way twice".

提交回复
热议问题