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)

7 Answers 7

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.

3
  • 4
    Patterns exist in every paradigm. Jeremy Gibbons is writing a book called Patterns in Functional Programming (and blogging about it here). Patterns are just what the name says - recurring designs, that solve similar problems. They're everywhere, 'though you may not always recognize them. Commented Jun 20, 2012 at 12:26
  • @AndréParamés A quick skim through leads me to believe Jeremy Gibbons is talking about language idioms, not design patterns.
    – Izkata
    Commented Jun 20, 2012 at 15:32
  • @Izkata Non-OOP Design Patterns?
    – Roc Martí
    Commented Jun 21, 2012 at 11:49
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.

0
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
4
  • 5
    Actually patterns are of a lower level than principles. That is to say, that a pattern is closer to the actual implementation than a principle in this context. In other words, principles are more abstract than patterns with principles meaning general design guidelines and patterns representing solutions suitable for a particular class of problems.
    – Jarkko
    Commented Jun 20, 2012 at 10:07
  • @Jarkko see my example. When I talked about level, I meant that patterns are based on principles, not vice versa. Brick is not more high-level thing, than building.
    – lazyberezovsky
    Commented Jun 20, 2012 at 10:14
  • 4
    I can see what you meant by that and understand your thinking on the matter. It is, however, contrary to the common meaning of the concepts "high level" and "low level" in the context of software. (For some reason i can not @-tag you.)
    – Jarkko
    Commented Jun 20, 2012 at 10:19
  • 1
    "Patterns are more high-level things, than principles". I beg to differ ==> A pattern is close to realization (i.e. low level), while a principle is high level rule.
    – Raúl
    Commented Aug 18, 2016 at 11:35
3

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

3
  • 1
    can you give some examples?
    – Chandi Gorom
    Commented Jun 20, 2012 at 10:23
  • Please tell us the principle that calls for Factory or Chain of Responsibility or Flyweight.
    – duffymo
    Commented Jun 20, 2012 at 16:11
  • 2
    @duffymo Well Factory for example follows Dependency Inversion Principle (not injection); both the client and the instance depend upon the abstraction - the interface. Chain of Responsibility is based on the principles of loose coupling and separation of controlos. Flyweight I believe has only performance gains.
    – Random42
    Commented Nov 7, 2012 at 21:25
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.
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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.