25

What is the difference between Object Oriented Design Patterns and Principles? Are they different things? As far as I understood both of them try to achieve some common goal (e,g. flexibility). So can I say a pattern is a principle and vice versa?

Design Principle = SOLID (i.e. Dependency Inversion Principle)

Design Pattern = Gof (i.e. Abstract Factory Pattern)

gnat
  • 20,543
  • 29
  • 115
  • 306

7 Answers7

24

No, they aren't the same.

Patterns are common solutions to object-oriented programming problems. (I'm not aware of any similar books for functional or declarative programming.) The idea was crystallized in the famous "Design Patterns" book by the Gang of Four in 1995.

As Andre points out, patterns are common in every paradigm. I'll reiterate my previous statement: I'm not aware of any similar books for functional or declarative programming, but Andre has remedied my ignorance with the link he provided below. (Thank you, Andre.)

Principles are less about particular languages or paradigms, more general. "Don't Repeat Yourself" - DRY principle - is true for all programming.

duffymo
  • 3,032
19

These concepts are not same :

*Design Principle : * Software design principles represent a set of guidelines that help us to avoid having a bad design. like: Open Close Principle

*Design Pattern : * a Design Pattern is a general reusable solution to a commonly occurring problem within a given context in software design. Like: Singleton

7

Patterns are to principles, what implementations are to patterns.

A principle would be "indirection", which could be accomplished by a "factory" pattern, which gets implemented as a class with factory methods in the end.

K..
  • 596
4

Patterns are more high-level things, than principles. Patterns resolve specific issues. Principles could be applied anywhere regardless of context. Actually patterns based on principles (SRP, DRY, etc)

E.G. Lets look on Strategy pattern. It defines a family of algorithms, encapsulates each one, and makes them interchangeable. So, you have here high-level concept of algorithm. With State pattern you have high-level concept of state. With principles you don't have any high-level concepts. Principles are building blocks, which used by patters to achieve goal. When you implement Strategy pattern, you use SOLID:

  • SRP - you define code, which responsible for algorithm and extract it from another code.
  • OCP - you define abstraction, which represents all different algorithms and use it
  • LSP - you don't use concrete algorithm classes in client code, only abstraction
3

Well, Principles are rules while patterns are their concrete examples.

Ahmad
  • 147
2

Patterns where originally documented for Architecture. In architecture, apply to things ranging from placement of the door in a room to the layout of a village.

The Gang of Four applied the idea to Object-Oriented Programming. There may be more than one pattern which can be used to solve a problem, but each pattern will have a specific implementation. Patterns exist in other programming approaches, but I am not aware of any applicable books. As others have mentioned Patterns cover specific implementations. Using a pattern when it doesn't apply is often considered an anti-pattern.

Principles don't cover implementation, although there can be standard implementation approaches. Principles are more about covering general issues rather than specific problems. For Inversion of Control, I am aware of at least three implementation approaches. For DRY (Don't Repeat Yourself) I don't know of a singe specific implementation approach, although I use several.

Consider

  • You have been requested to use a Pattern like Abstract Factory Pattern as the only approach to develop a program. Would this be appropriate? No, then it is more likely a Pattern.
  • You have been requested to apply DRY to all components? Would this be appropriate? Yes, then it is more likely to be a Principle.
BillThor
  • 6,310
1

OO Design Principle—

OO principle is a set of guidelines that ensures OOP concept. Based on the OOP concept, this defines ways to design better way, a better design. The basic OO design principle is SOLID.

A Design pattern provides a general solution to a design problem. Please note “design pattern” can be applied to noon object oriented word too. So A OO design patterns (OODPs) are those which provide a general solution to object oriented design based OO principle. Design patterns are discovered, not invented. There are several ways to define OODPs and most famous one is BSC [Behavioral Structural Creational].

Following is the link for detailed explanation. http://techythought.wordpress.com/2013/01/21/design-principle-vs-ds-design-pattern-describing-oop-elements/

Tanzy
  • 11
  • 1