What's the difference between design patterns and design principles?
Asked Answered
H

4

30

I'm new to Ruby on Rails, and I went through these articles.

But I couldn't understand the actual difference between design patterns and design principles. Could someone please explain the distinction?

Horrific answered 9/7, 2015 at 12:12 Comment(1)
A principle is an abstraction, a guide to design. A pattern is an implementation that solves a particular problem.Granny
C
66

Design Principles:

Design principles are core abstract principles that we are supposed to follow while designing software. Remember they aren't concrete - rather abstract. They can be applied in any language, on any platform regardless of the state as long as we are within the permissible conditions.

Examples:

  • Encapsulate what varies.
  • Program to interfaces, not to implementations.
  • Depend upon abstractions. Do not depend upon concrete classes.

Design Patterns:

They are solutions to real-world problems that pop up time and again, so instead of reinventing the wheel, we follow the design patterns that are well-proven, tested by others, and safe to follow. Now, design patterns are specific; there are terms and conditions only in which a design pattern can be applied.

Examples:

  • Singleton Pattern ( One class can only have one instance at a time )

  • Adapter Pattern ( Match interface of different classes )

The following analogy will help you understand the difference better:

Principle: We should teach others in order to educate ourselves as well as others, and overall make our nation a progressive nation.

Pattern: In our country, each medical doctor graduate is supposed to teach 6 months in a far-away village to complete his/her degree.

Charlesettacharleston answered 9/7, 2015 at 12:55 Comment(1)
"Program to interfaces" and "Depend upon abstractions" is the same thing. Just sayin'. ;)Lewis
L
16

I think the answer from @ArslanAli is worth summarizing.

  • Principles apply to all of programming. You should have a very good reason any time you choose not to follow principles.
  • Patterns apply to specific, common problems. You should have a very good reason any time you choose to implement a pattern.
Lewis answered 14/12, 2018 at 14:58 Comment(0)
S
6

Principles are best practices to follow to allow scalable architecture and software craftmanship. Design patterns are techniques about how to do the design and architect your code. Each design pattern has a different use case and is applicable to a different scenario. On the other hand principles ; in most cases you need to follow them to have code quality. And yes some principles imply some design patterns : Ex. Open/Closed principle closely implies strategy pattern . Dependency injection has strong ties to MVC pattern.

Spectroradiometer answered 8/1, 2019 at 6:8 Comment(0)
A
4

To some extent, we can refer principles as a set of rules that we can follow in every step of programming, but patterns are a set of structured solutions for some specific problems that we may face during coding -not always happening.

Anjelicaanjou answered 6/12, 2020 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.