What's the difference between design patterns and design principles? What's the difference between design patterns and design principles? ruby ruby

What's the difference between design patterns and design principles?


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.


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.


Design Principle

Design principles provide high-level guidelines to design better software applications. They do not provide implementation guidelines and are not bound to any programming language. The SOLID (SRP, OCP, LSP, ISP, DIP) principles are one of the most popular sets of design principles.

Single Responsibility Principle

Open/Closed Principle

Liskov Substitution Principle

Interface Segregation Principle

Dependency Inversion Principle

For example, the Single Responsibility Principle (SRP) suggests that a class should have only one reason to change. This is a high-level statement that we can keep in mind while designing or creating classes for our application. SRP does not provide specific implementation steps but it's up to you how you implement SRP in your application.

Design Pattern

Design Pattern provides low-level solutions related to the implementation, of commonly occurring object-oriented problems. In other words, the design pattern suggests a specific implementation for the specific object-oriented programming problem. For example, if you want to create a class that can only have one object at a time, then you can use the Singleton design pattern which suggests the best way to create a class that can only have one object.

Design patterns are tested by others and are safe to follow, e.g. Gang of Four patterns: Abstract Factory, Factory, Singleton, Command, etc.