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

How to approach design problems like "design a vending machine"

I wanted to know what are the steps that I should follow to approach problems like design a vending machine and come up with a number of design documents (like use case, sequence diagram, class diagram). Is there any source/link that I can read…
ebaccount
14
votes
6 answers

Is Java's String Intern a flyweight?

Does the implementation of Java's String memory pool follows flyweight pattern? Why I have this doubt is, I see that there is no extrinsic state involved in Intern. In GoF I read that there should be a right balance between intrinsic and extrinsic…
Joseph
  • 877
  • 8
  • 20
14
votes
3 answers

ORM and Active Record Pattern in PHP?

There are two things that seem to be popular nowadays and I was wondering what are the pros and cons of using something like this: http://codeigniter.com/user_guide/database/active_record.html ? Another thing is ORM (Doctrine for instance). What are…
Tower
  • 98,741
  • 129
  • 357
  • 507
14
votes
8 answers

A more elegant way to write decision making code which evaluates multiple inputs with different priorities?

I'm writing some decision-making AI for a game, and I've come up with the following piece of code. if(pushedLeft && leftFree && leftExists) GoLeft(); else if(pushedRight && rightFree && rightExists) GoRight(); else if(leftFree &&…
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
14
votes
3 answers

PHP Builder pattern without inner classes

I've been reading through Effective Java by Joshua Bloch. I also develop in PHP and I wanted to implement the builder pattern outlined in item 2, but PHP doesn't have inner classes. Is there any way to achieve this pattern in PHP, keeping the…
Jackson
  • 607
  • 2
  • 6
  • 20
14
votes
2 answers

State pattern vs ENUM

From time to time it's need to make support for states for objects. As I understand there are two approaches: ENUM (SIMPLE) STATE pattern (OC principle) it's evident that need to use State pattern for such purposes(I am not sure). But reading…
user1074896
  • 1,025
  • 2
  • 15
  • 27
14
votes
7 answers

Data Access Layer design patterns

I have to design a Data Access Layer with .NET that probably will use more than one database management system (Mysql and Sql Server) with the same relational design. Basically, it has to be simple to switch from one database to another so I would…
David Espart
  • 11,520
  • 7
  • 36
  • 50
14
votes
14 answers

Objective-C switch using objects?

I'm doing some Objective-C programming that involves parsing an NSXmlDocument and populating an objects properties from the result. First version looked like this: if([elementName compare:@"companyName"] == 0) [character…
craigb
  • 16,827
  • 7
  • 51
  • 62
14
votes
4 answers

Infinite main loop in F#

A usual pattern for CLI application is to run in infinite loop, until user types some quit command. Like, in C language: while(1){ scanf("%c", &op); ... else if(op == "q") break; } What would be the pattern for such console…
Arnthor
  • 2,563
  • 6
  • 34
  • 54
14
votes
1 answer

Pattern for modifying knockout observable on parent from child view model

I have a parent-child view model object structure set up and need to update an observable on the parent from the child. I've basically come up with two patterns for doing so: 1] Pass a reference of the parent property to the child and update the…
KodeKreachor
  • 8,852
  • 10
  • 47
  • 64
13
votes
3 answers

DAO design pattern

So lets say we have a couple of entities we want to persist using DAO objects. So we implement the right interface so we end up with class JdbcUserDao implements UserDao{ //... } class JdbcAddressDao implements AddressDao{ //... } So if I want to…
Mercurial
  • 2,095
  • 4
  • 19
  • 33
13
votes
1 answer

How to implement visitor pattern in javascript?

As far as I understand, visitor pattern is often used to add methods to some hierarchy structure. But I still don't get it: see the example where I try to highlight left subtree: Javascript tree implementation: function node(val) { this.value…
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
13
votes
2 answers

What is System.Lazy and the Singleton Design Pattern

Can anyone help me to understand the benefit of using System.Lazy with Singleton Design Pattern.
Vijjendra
  • 24,223
  • 12
  • 60
  • 92
13
votes
4 answers

Do we ever need to prefer constructors over static factory methods? If so, when?

I have been reading Effective Java by Joshua Bloch and so far it really lives up to its reputation. The very first item makes a convincing case for static factory methods over constructors. So much that I began to question the validity of the good…
13
votes
2 answers

Should I stop using abstract base classes/interfaces and instead use boost::function/std::function?

I've just learned about what std::function really is about and what it is used for and I have a question: now that we essentially have delegates, where and when should we use Abstract Base Classes and when, instead, we should implement polymorphism…
Zeks
  • 2,265
  • 20
  • 32