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

Array of methods: Adapter Pattern?

Problem Description: I want to be able to pass around a list of methods to other classes where the methods have been defined in only one class. If the methods, some of which have input parameters and non-void return types, are defined in one class,…
John
  • 775
  • 3
  • 11
  • 25
13
votes
3 answers

Java fallback pattern

I'm trying to find a nice way of implementing a service which relies on a third-party library class. I also have a 'default' implementation to use as fallback in case the library is unavailable or can not provide an answer. public interface Service…
13
votes
7 answers

Which is the better C# class design for dealing with read+write versus readonly

I'm contemplating two different class designs for handling a situation where some repositories are read-only while others are read-write. (I don't foresee any need for a write-only repository.) Class Design 1 -- provide all functionality in a base…
devuxer
  • 41,681
  • 47
  • 180
  • 292
13
votes
2 answers

Do we need a .build() method in the Builder Pattern?

I had a question regarding the "Builder Pattern" covered in "Effective Java". Do we need a .build() method for it to correctly implement the pattern? For instance, let's say that we have the following class: public class CoffeeDrink { private…
victormejia
  • 1,174
  • 3
  • 12
  • 30
13
votes
1 answer

Design Patterns for Data Access Layer

I have an application which uses a database (MongoDB) to store information. In the past, I have used a class full of static methods to save and retrieve data but I have since realised this is not very object-oriented or future-proof. Even though it…
user2248702
  • 2,741
  • 7
  • 41
  • 69
13
votes
1 answer

(Entity-Control-Boundary pattern) -> How to deal with two entities?

Premise I've recently read/watched a lot of articles/videos by Java Champion Adam Bien, where he advocates the usage of the ancient but renewed Entity - Control - Boundary Design Pattern JAVA EE >= 6. Leveraging on CDI, EJB 3.1, JPA 2 and other JAVA…
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
13
votes
2 answers

Looking for ideas how to refactor my algorithm

I am trying to write my own Game of Life, with my own set of rules. First 'concept', which I would like to apply, is socialization (which basicaly means if the cell wants to be alone or in a group with other cells). Data structure is 2-dimensional…
sventevit
  • 4,766
  • 10
  • 57
  • 89
13
votes
6 answers

Conditionally execute block in Ruby if value is not nil? (aka Smalltalk's ifNotNilDo:)

In Smalltalk there is the method ifNotNilDo: It is used like this: database getUser ifNotNilDo: [:user | Mail sendTo: user ] On objects that are not nil the block is executed, passing the object itself as a parameter. The implementation in class…
Sebastian N.
  • 1,962
  • 15
  • 26
13
votes
2 answers

Are all of the core J2EE patterns still good in context of Java EE?

With the arrival of Java EE and EJB 3, have any of these Core J2EE Patterns become obsolete, or stand deprecated in light of (better) alternatives? Are there any new patterns that one could use?
Harry
  • 3,684
  • 6
  • 39
  • 48
13
votes
2 answers

Best Practice of Repository and Unit of Work Pattern with Multiple DbContext

I plan to develop a web application using ASP.NET MVC with Entity Framework 6 (Code First / POCO). I also want to use generic Repository and Unit of Work Pattern in my application. This application connects to more than two databases so, I have to…
derodevil
  • 811
  • 1
  • 11
  • 37
13
votes
2 answers

Generating identities for entities in DDD

Edit To further clarify my initial problem, I rewrote the question with more 'DDD'-termini, common patterns and discussion arguments. The orginal version can be found under revisions. Where and how are identities for entities/aggregate roots being…
13
votes
6 answers

Iterator pattern in VB.NET (C# would use yield!)

How do implement the iterator pattern in VB.NET, which does not have the yield keyword?
c00ke
  • 2,245
  • 5
  • 26
  • 34
13
votes
4 answers

MVVM- View Model-View Model Communications

How do I go about having two view models communicate with one another using MVVM Light. I know how to use the messenger class and register etc.. Here is my Scenario A Settings View ---> a Settings View Model . …
Mike Diaz
  • 2,045
  • 4
  • 32
  • 55
13
votes
3 answers

Why do we need design patterns

Why do we need design patterns like Factory, Abstract Factory and Singleton?
0cool
  • 683
  • 2
  • 10
  • 27
13
votes
9 answers

Generic Singleton

I have a question, is this the correct approach to make a Generic Singleton? public class Singleton where T : class, new() { private static T instance = null; private Singleton() { } public static T Instancia …
MRFerocius
  • 5,509
  • 7
  • 39
  • 47