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
584
votes
11 answers

What's an Aggregate Root?

I'm trying to get my head around how to properly use the repository pattern. The central concept of an Aggregate Root keeps coming up. When searching both the web and Stack Overflow for help with what an aggregate root is, I keep finding discussions…
Dinah
  • 52,922
  • 30
  • 133
  • 149
571
votes
30 answers

Dependency Injection vs Factory Pattern

Most of the examples quoted for usage of Dependency Injection, we can solve using the factory pattern as well. Looks like when it comes to usage/design the difference between dependency injection and factory is blurred or thin. Once someone told me…
Binoj Antony
  • 15,886
  • 25
  • 88
  • 96
569
votes
28 answers

Commonly accepted best practices around code organization in JavaScript

As JavaScript frameworks like jQuery make client side web applications richer and more functional, I've started to notice one problem... How in the world do you keep this organized? Put all your handlers in one spot and write functions for all the…
hugoware
  • 35,731
  • 24
  • 60
  • 70
564
votes
24 answers

On design patterns: When should I use the singleton?

The glorified global variable - becomes a gloried global class. Some say breaking object-oriented design. Give me scenarios, other than the good old logger where it makes sense to use the singleton.
Setori
  • 10,326
  • 11
  • 40
  • 46
525
votes
21 answers

Is there a simple, elegant way to define singletons?

There seem to be many ways to define singletons in Python. Is there a consensus opinion on Stack Overflow?
Jamie
  • 7,635
  • 4
  • 23
  • 16
524
votes
19 answers

What is the difference in case of intent and application between these two Patterns?

Factory and Abstract Factory are both creational patterns. What is the difference in case of intent and application between these two Patterns?
user366312
  • 16,949
  • 65
  • 235
  • 452
514
votes
21 answers

MVC pattern on Android

Is it possible to implement the model–view–controller pattern in Java for Android? Or is it already implemented through Activities? Or is there a better way to implement the MVC pattern for Android?
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
513
votes
10 answers

Why create "Implicitly Unwrapped Optionals", since that implies you know there's a value?

Why would you create a "Implicitly Unwrapped Optional" vs creating just a regular variable or constant? If you know that it can be successfully unwrapped then why create an optional in the first place? For example, why is this: let someString:…
Johnston
  • 20,196
  • 18
  • 72
  • 121
511
votes
11 answers

ViewPager and fragments — what's the right way to store fragment's state?

Fragments seem to be very nice for separation of UI logic into some modules. But along with ViewPager its lifecycle is still misty to me. So Guru thoughts are badly needed! Edit See dumb solution below ;-) Scope Main activity has a ViewPager with…
464
votes
13 answers

How do the Proxy, Decorator, Adapter, and Bridge Patterns differ?

I was looking at the Proxy Pattern, and to me it seems an awful lot like the Decorator, Adapter, and Bridge patterns. Am I misunderstanding something? What's the difference? Why would I use the Proxy pattern versus the others? How have you used them…
Charles Graham
  • 24,293
  • 14
  • 43
  • 56
456
votes
46 answers

Simplest/cleanest way to implement a singleton in JavaScript

What is the simplest/cleanest way to implement the singleton pattern in JavaScript?
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
423
votes
18 answers

Why is IoC / DI not common in Python?

In Java IoC / DI is a very common practice which is extensively used in web applications, nearly all available frameworks and Java EE. On the other hand, there are also lots of big Python web applications, but beside of Zope (which I've heard should…
384
votes
11 answers

How to implement the factory method pattern in C++ correctly

There's this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don't know how to do it, even though it sounds simple: How do I implement Factory Method in C++ correctly? Goal: to make it possible…
Kos
  • 70,399
  • 25
  • 169
  • 233
377
votes
10 answers

Singletons vs. Application Context in Android?

Recalling this post enumerating several problems of using singletons and having seen several examples of Android applications using singleton pattern, I wonder if it's a good idea to use Singletons instead of single instances shared through global…
Martín Schonaker
  • 7,273
  • 4
  • 32
  • 55
366
votes
5 answers

Design Patterns web based applications

I am designing a simple web-based application. I am new to this web-based domain.I needed your advice regarding the design patterns like how responsibility should be distributed among Servlets, criteria to make new Servlet, etc. Actually, I have few…
mawia
  • 9,169
  • 14
  • 48
  • 57