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
13
votes
3 answers

Java collections covariance problem

Lets say we have a program which contains such classes: public interface AbstractItem { } public SharpItem implements AbstractItem { } public BluntItem implements AbstractItem { } public interface AbstractToolbox { //well the problem starts…
Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94
13
votes
8 answers

What security measures should be taken when creating "change your password" functionality?

I'm adding a "change password" functionality to my webgame http://ninjawars.net , which currently has fixed (and essentially never changing) passwords. I want to avoid making a mess of it, so I'd like to make sure that I have the basic security…
Kzqai
  • 22,588
  • 25
  • 105
  • 137
13
votes
3 answers

Does the "Open for extension, closed for modification" principle make any sense?

It looks to me like Bob Martin needed something starting with O to make SOLID and found in some old book this (possibly useless) Open/Closed principle. How can Open/Closed co-exists with the Single Responsibility, that states a class should have a…
13
votes
4 answers

Push, pull mechanism observer pattern

At the moment I'm studying design patterns and I've come to a part where I'm confused whether the observer pattern makes use of the push mechanism or does it make use of the pull mechanism? I've read different implementations of this and can't…
Liusara
  • 141
  • 1
  • 1
  • 7
13
votes
4 answers

Do Discriminated Unions conflict with the Open Close Principle

I can't help but question if the use of Discriminated Unions within a large system violates the Open/Close principle. I understand the Open/Close Principle is Object Oriented and NOT Functional. However, I have reason to believe that the same…
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
13
votes
5 answers

Why classes tend to be defined as interface nowadays?

These 2-3 last years, many projects I see, like Cuyahoga open source C# CMS, tends to define persistent and non persistent classes as Interface. Why? Is there a good reason? TDD? Mocking? A design pattern? ...
Afshar Mohebi
  • 10,479
  • 17
  • 82
  • 126
13
votes
4 answers

Implement a PHP singleton: static class properties or static method variables?

So, I've always implemented a singleton like so: class Singleton { private static $_instance = null; public static function getInstance() { if (self::$_instance === null) self::$_instance = new Singleton(); return…
Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
13
votes
3 answers

Symfony - How to access entity's repository

There are several ways that we can access the entity's repository in Symfony2 controllers or services which each has its advantage and disadvantage. First I list them here and then asking if there is any better solution or these are the only options…
Saman
  • 5,044
  • 3
  • 28
  • 27
13
votes
4 answers

Two Interface with Same Method Name - Implementation of Methods

Suppose I have two interfaces: interface IOne { public void method(); } and interface ITwo { public void method(); } A concrete class implements both of the interfaces: public class A implements IOne, ITwo { public void…
Razib
  • 10,965
  • 11
  • 53
  • 80
13
votes
5 answers

Mediator pattern vs Publish/Subscribe

Can someone point out the main differences between the two? It seems that, at least conceptually, the two are very closely related. If I were to hazard a guess, I would say that the publish/subscribe method is a subset of the mediator pattern…
Kristian D'Amato
  • 3,996
  • 9
  • 45
  • 69
13
votes
2 answers

If I use abstract class instead of interface while implementing factory pattern. Would it still be a factory pattern?

For example : http://www.tutorialspoint.com/design_pattern/factory_pattern.htm If I change interface shape on abstract class Shape, make concrete classes to extend Shape and Make the Shape factory return Shape abstract class typed objects. Is it…
Achozen
  • 161
  • 1
  • 9
13
votes
3 answers

Managing updates to nested immutable data structures in functional languages

I've noticed while on my quest to lean functional programming that there are cases when parameter lists start to become excessive when using nested immutable data structures. This is because when making an update to an object state, you need to…
13
votes
3 answers

State pattern and guard

Update: State pattern might a wrong way to solve this. Hence, any other pattern is welcome. Basically I'm looking for a way to have guard conditions for each state yet having a clean and maintainable code. How would front-end side routing systems…
Sam R.
  • 16,027
  • 12
  • 69
  • 122
13
votes
4 answers

Why does Scanner implement Iterator?

I was just wondering why java.util.Scanner implements java.util.Iterator? Scanner implements the remove method and throws an UnsupportedOperationException. But shouldn't a class, when implementing an interface, fulfill the contract of the…
Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
13
votes
4 answers

What is the Ghost Design Pattern?

Someone recently asked a question about the Ghost Design Pattern - I have not seen this before. What is the Ghost Design Pattern and how is it implemented? I can only find snippets on the web in reference to it.
Robben_Ford_Fan_boy
  • 8,494
  • 11
  • 64
  • 85