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
14
votes
4 answers

Design Patterns and Scala

I'm writing this question to maintain a register of design patterns associated with Scala, standards patterns or only from this language. Associated questions: Scala Catalog of functional Design Patterns Design patterns for static type…
adelarsq
  • 3,718
  • 4
  • 37
  • 47
14
votes
8 answers

Disadvantage of object composition over class inheritance

Most design patten books say we should "Favor object composition over class inheritance." But can anyone give me an example that inheritance is better than object composition.
Howard
  • 19,215
  • 35
  • 112
  • 184
14
votes
3 answers

Reference counting in Java

I have a system that keeps running out of disk space because that garbage collector doesn't free the objects that hold the file handles fast enough. (Files are created and deleted all the time, but since the process still has an open file handle,…
itsadok
  • 28,822
  • 30
  • 126
  • 171
14
votes
3 answers

Python - what is the correct way to copy an object's attributes over to another?

I have two classes. They're almost identical, except for 2 attributes. I need to copy all the attributes over from one to the other, and I'm just wondering if there is a pattern or best practice, or if I should just basically do: spam.attribute_one…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
14
votes
4 answers

Does the pattern where you chain methods in your object by returning a reference to itself have a name?

In most modern OO languages chaining methods together is common, and IMHO elegant, practice. In jquery, for example, you often see code like: $('div').addClass('container').css('color', 'white').length Does writing your objects to allow this have a…
thomasmalt
  • 1,718
  • 11
  • 15
14
votes
3 answers

Usefulness of java dynamic proxies vs regular proxies

I need some advice to which scenarios a dynamic proxy would prove more useful than a regular proxy. I've put alot of effort into learning how to use dynamic proxies effectively. In this question, set aside that frameworks like AspectJ can perform…
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
14
votes
4 answers

Should a ViewModel in MVVM reference the View?

In the MVVM (Model-View-ViewModel) pattern should the ViewModel reference the view. I would think that it should not. But how should the following scenario be handeled? I have a view that has a tab control as the main container, the viewmodel for…
Johan
  • 1,189
  • 3
  • 15
  • 28
14
votes
4 answers

Android Studio navigation drawer like Gmail app

We're making an android app, and there is something we want to add. Which is the effect the Gmail app has. You can choose which account you want to view (and the rest of the app will behave accordingly). EDIT: I now already have a (working)…
Tvde1
  • 1,246
  • 3
  • 21
  • 41
14
votes
5 answers

C++ Template : one list by class, how to factorize the code?

Suppose I have this class : class Component1; class Component2; // many different Components class Component42; class MyClass { public: MyClass(void) {}; std::list component1List; std::list component2List; //…
Tryss
  • 303
  • 2
  • 8
14
votes
4 answers

Why should one use factory method to create objects

Possible Duplicates: Factory Pattern. When to use factory methods? Why do static Create methods exist? Though I know what is Factory Design Pattern. But I am unable to comprehend what are the benefits of using it. Why should we create objects…
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163
14
votes
2 answers

What is Single and Double Dispatch?

i have wrote the visitor pattern as follow but i don't understand what is single and double dispatch. AFAIK, single dispatch is invoke a method based on caller type where double dispatch is invoke a method based on caller type and argument type. I…
nicholas
  • 2,581
  • 14
  • 66
  • 104
14
votes
3 answers

Why builder should be static inner class?

I am pretty much aware about builder pattern. Also already gone through with the builder pattern described in Item#2 in effective java of Joshua Bloch. Here my question is - Is there any specific benefits to keep builder class inside the class which…
Bhavesh Dangi
  • 999
  • 1
  • 8
  • 14
14
votes
8 answers

Is Dependency Injection a pattern and, is this it?

I was having a heated debate with one of my colleagues on dependency injection, and realized I didn't quite know all the facts on the subject. So, take this code (just so you know,we're using Castle Windsor) IPlayerService service =…
andy
  • 8,775
  • 13
  • 77
  • 122
14
votes
1 answer

Singleton pattern in JavaScript

Below is an example of a very popular implementation of the JavaScript Singleton pattern: var mySingleton = (function() { var instance; function init() { function privateMethod() { console.log("I am private"); } …
Gurpreet Singh
  • 20,907
  • 5
  • 44
  • 60
14
votes
6 answers

Async Load JavaScript Files with Callback

I am trying to write an ultra simple solution to load a bunch of JS files asynchronously. I have the following script below so far. However the callback is sometimes called when the scripts aren't actually loaded which causes a variable not found…
Gcoop
  • 3,372
  • 4
  • 26
  • 35