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
2 answers

How to implement solution for Race simulation problems

There was a question asked in an interview: In a Formula-1 challenge, there are n teams numbered 1 to n. Each team has a car and a driver. Car’s specification are as follows: Top speed: (150 + 10 * i) km per hour Acceleration: (2 * i) meter per…
Alex
  • 1,178
  • 3
  • 9
  • 24
14
votes
9 answers

Is testability alone justification for dependency injection?

The advantages of DI, as far as I am aware, are: Reduced Dependencies More Reusable Code More Testable Code More Readable Code Say I have a repository, OrderRepository, which acts as a repository for an Order object generated through a Linq to Sql…
14
votes
5 answers

Limit number of parameters per method?

Assuming the parameters are all the same type, is there a rule of thumb in regards to the number of parameters for a method? Im just wondering where I should draw the line and what my alternatives are (ie interface, array, etc).
Ryan
  • 4,443
  • 4
  • 26
  • 26
14
votes
2 answers

What is the best approach to use multiple services inside a resource controller?

I have an controller that call three services : public class ProductController() { @Autowired private AccountService accountService; @Autowired private ProcessService processService; @Autowired private releaseService…
Vipercold
  • 599
  • 1
  • 7
  • 27
14
votes
4 answers

javascript: execute a bunch of asynchronous method with one callback

I need to execute a bunch of asynchronous methods (client SQLite database), and call only one final callback. Of course, the ugly way is: execAll : function(callBack) { asynch1(function() { asynch2(function() { …
Samuel
  • 5,439
  • 6
  • 31
  • 43
14
votes
5 answers

JavaScript Module Pattern - What about using "return this"?

After doing some reading about the Module Pattern, I've seen a few ways of returning the properties which you want to be public. One of the most common ways is to declare your public properties and methods right inside of the "return" statement,…
Rob Gibbons
  • 1,583
  • 3
  • 14
  • 24
14
votes
7 answers

When to call the gang of four? [When to use design patterns?]

In The Guerilla Guide to Interviewing Joel says that guys who want to get things done, but are not smart will do stupid things like using a visitor design pattern where a simple array would be sufficient. I find it hard to detect, if the design…
Black
  • 5,022
  • 2
  • 22
  • 37
14
votes
2 answers

Unit of work + repository + service layer with dependency injection

I am designing a web application and a windows service and want to use the unit of work + repository layer in conjunction with a service layer, and I am having some trouble putting it all together so that the client apps control the transaction of…
14
votes
7 answers

What is the difference between a Functor and the Command pattern?

I am very familiar with the Command pattern, but I don't yet understand the difference in theory between a Functor and a command. In particular, I am thinking of Java implementations. Both are basically programming "verbs" represented as objects.…
Elijah
  • 13,368
  • 10
  • 57
  • 89
14
votes
3 answers

Applying the LAW of DEMETER with a facade pattern

In essential skills for the agile developer, in the needs vs capabilities interface, chap, 12, I'm trying to understand the main solution proposed to the challenge of applying the LAW OF DEMETER that the author mention by the end of this chapter.…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
14
votes
9 answers

Why do static Create methods exist?

I was wondering, why do static Create methods exist? For instance, why use this code: System.Xml.XmlReader reader = System.Xml.XmlReader.Create(inputUri); over this code: System.Xml.XmlReader reader = new System.Xml.XmlReader(inputUri); I cannot…
GeReV
  • 3,195
  • 7
  • 32
  • 44
14
votes
6 answers

Singleton Properties

Ok, if I create a singleton class and expose the singleton object through a public static property...I understand that. But my singleton class has other properties in it. Should those be static? Should those also be private? I just want to be…
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
14
votes
2 answers

For a large validation task is chain of responsibility pattern a good bet?

I need to build a process which will validate a record against ~200 validation rules. A record can be one of ~10 types. There is some segmentation from validation rules to record types but there exists a lot of overlap which prevents me from…
yamori
  • 1,213
  • 4
  • 14
  • 27
14
votes
6 answers

Builders in Java versus C++?

In Google's Protocol Buffer API for Java, they use these nice Builders that create an object (see here): Person john = Person.newBuilder() .setId(1234) .setName("John Doe") .setEmail("jdoe@example.com") .addPhone( …
Frank
  • 64,140
  • 93
  • 237
  • 324
14
votes
4 answers

Difference aggregation, acquaintance and composition (as used by Gang Of Four)

I have been reading Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma et al. and got to the part explaining aggregation and acquaintance (page 22-23). Here is an excerpt (sorry if it is too long but I deemed all of it to…
ps-aux
  • 11,627
  • 25
  • 81
  • 128