Questions tagged [design-patterns]

A design pattern is a general reusable solution to a commonly occurring problem in software design. Use this tag for questions when you're having problems with the implementation of design-patterns. Please don't use this tag on questions about text pattern matching. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.

A design pattern is not a finished design that can be transformed directly into code. It is a description or template for solving a problem that can be used in many different situations.

Object-oriented design patterns typically show relationships and interactions between classes or objects without specifying the final application classes or objects involved. Many patterns imply object-orientation or a more generally mutable state, which may not be as applicable in functional programming languages, in which data is immutable or treated.

Design patterns are generally described using the Unified Markup Language ([tag: UML]) - a class diagram shows the relationship between the design pattern components. In addition, UML has a sufficiently extensive and expressive vocabulary that helps describe the details of patterns.

The seminal book that introduced the concept had four authors, often referred to as the "Gang of Four".

Gang of Four design patterns

Concurrency patterns

Other patterns

Useful links

Books

31951 questions
365
votes
16 answers

What's the difference between the Dependency Injection and Service Locator patterns?

Both patterns seem like an implementation of the principle of inversion of control. That is, that an object should not know how to construct its dependencies. Dependency Injection (DI) seems to use a constructor or setter to "inject" it's…
Charles Graham
  • 24,293
  • 14
  • 43
  • 56
351
votes
22 answers

How to study design patterns?

I have read around 4-5 books on design patterns, but still I don't feel I have come closer to intermediate level in design patterns? How should I go studying design patterns? Is there any good book for design patterns? I know this will come only…
anand
  • 11,071
  • 28
  • 101
  • 159
349
votes
19 answers

What are the downsides to using dependency injection?

I'm trying to introduce dependency injection (DI) as a pattern here at work and one of our lead developers would like to know: What, if any, are the downsides to using the dependency injection pattern? Note I'm looking here for an, if possible,…
Epaga
  • 38,231
  • 58
  • 157
  • 245
342
votes
20 answers

When should I use the Visitor Design Pattern?

I keep seeing references to the visitor pattern in blogs but I've got to admit, I just don't get it. I read the wikipedia article for the pattern and I understand its mechanics but I'm still confused as to when I'd use it. As someone who just…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
335
votes
17 answers

Factory Pattern. When to use factory methods?

When is it a good idea to use factory methods within an object instead of a Factory class?
jjshell
333
votes
26 answers

What should my Objective-C singleton look like?

My singleton accessor method is usually some variant of: static MyClass *gInstance = NULL; + (MyClass *)instance { @synchronized(self) { if (gInstance == NULL) gInstance = [[self alloc] init]; } …
schwa
  • 11,962
  • 14
  • 43
  • 54
320
votes
24 answers

Singleton: How should it be used

Edit: From another question I provided an answer that has links to a lot of questions/answers about singletons: More info about singletons here: So I have read the thread Singletons: good design or a crutch? And the argument still rages. I see…
Martin York
  • 257,169
  • 86
  • 333
  • 562
305
votes
14 answers

What are static factory methods?

What's a "static factory" method?
freddiefujiwara
  • 57,041
  • 28
  • 76
  • 106
295
votes
6 answers

Relational Database Design Patterns?

Design patterns are usually related to object oriented design. Are there design patterns for creating and programming relational databases? Many problems surely must have reusable solutions. Examples would include patterns for table design, stored…
Sklivvz
  • 30,601
  • 24
  • 116
  • 172
290
votes
2 answers

Repository Pattern Step by Step Explanation

Can someone please explain to me the Repository Pattern in .NET, step by step giving a very simple example or demo. I know this is a very common question but so far I haven't found a satisfactory answer.
Sa Patil
  • 3,077
  • 2
  • 14
  • 4
285
votes
12 answers

Is there a Java equivalent or methodology for the typedef keyword in C++?

Coming from a C and C++ background, I found judicious use of typedef to be incredibly helpful. Do you know of a way to achieve similar functionality in Java, whether that be a Java mechanism, pattern, or some other effective way you have used?
bn.
  • 7,739
  • 7
  • 39
  • 54
284
votes
12 answers

Which Architecture patterns are used on Android?

I'm doing a small research of mobile platforms and I would like to know which design patterns are used in Android? e.g. in iOS Model-view-controller is very widely used together with delegation and other patterns. What patterns and where in…
Burjua
  • 12,506
  • 27
  • 80
  • 111
272
votes
27 answers

Is it bad practice to make a setter return "this"?

Is it a good or bad idea to make setters in java return "this"? public Employee setName(String name){ this.name = name; return this; } This pattern can be useful because then you can chain setters like this: list.add(new…
Ken Liu
  • 22,503
  • 19
  • 75
  • 98
271
votes
21 answers

What is the difference between Strategy design pattern and State design pattern?

What are the differences between the Strategy design pattern and the State design pattern? I was going through quite a few articles on the web but could not make out the difference clearly. Can someone please explain the difference in layman's…
Chin Tser
  • 2,799
  • 2
  • 18
  • 6
269
votes
15 answers

What is an anti-pattern?

I am studying patterns and anti-patterns. I have a clear idea about patterns, but I don't get anti-patterns. Definitions from the web and Wikipedia confuse me a lot. Can anybody explain to me in simple words what an anti-pattern is? What is the…
g.revolution
  • 11,962
  • 23
  • 81
  • 107