Questions tagged [design-patterns]
A design pattern is a general reusable solution to a commonly occurring problem in software design.
4,397
questions
611
votes
13
answers
267k
views
So Singletons are bad, then what?
There has been a lot of discussion lately about the problems with using (and overusing) Singletons. I've been one of those people earlier in my career too. I can see what the problem is now, and yet, ...
454
votes
12
answers
181k
views
How accurate is "Business logic should be in a service, not in a model"?
Situation
Earlier this evening I gave an answer to a question on StackOverflow.
The question:
Editing of an existing object should be done in repository layer or in service?
For example if ...
422
votes
17
answers
170k
views
Why is Global State so Evil?
Before we start this, let me say I'm well aware of the concepts of Abstraction and Dependency Injection. I don't need my eyes opened here.
Well, most of us say, (too) many times without really ...
327
votes
15
answers
168k
views
Do I need to use an interface when only one class will ever implement it?
Isn't the whole point of an interface that multiple classes adhere to a set of rules and implementations?
233
votes
10
answers
33k
views
Is there a name for the (anti- ) pattern of passing parameters that will only be used several levels deep in the call chain?
I was trying to find alternatives to the use of global variable in some legacy code. But this question is not about the technical alternatives, I'm mainly concerned about the terminology.
The ...
225
votes
11
answers
30k
views
Why do we need so many classes in design patterns?
I am junior developer among seniors and am struggling a lot with understanding their thinking, reasoning.
I am reading Domain-Driven Design (DDD) and can't understand why we need to create so many ...
222
votes
10
answers
233k
views
What is MVC, really?
As a serious programmer, how do you answer the question What is MVC?
In my mind, MVC is sort of a nebulous topic — and because of that, if your audience is a learner, then you're free to ...
219
votes
16
answers
52k
views
My boss asks me to stop writing small functions and do everything in the same loop
I have read a book called Clean Code by Robert C. Martin. In this book I've seen many methods to clean up code like writing small functions, choosing names carefully, etc. It seems by far the most ...
218
votes
18
answers
108k
views
How can one manage thousands of IF...THEN...ELSE rules?
I am considering building an application, which, at its core, would consist of thousands of if...then...else statements. The purpose of the application is to be able to predict how cows move around in ...
217
votes
14
answers
107k
views
"Never do in code what you can get the SQL server to do well for you" - Is this a recipe for a bad design?
It's an idea I've heard repeated in a handful of places. Some more or less acknowledging that once trying to solve a problem purely in SQL exceeds a certain level of complexity you should indeed be ...
196
votes
4
answers
88k
views
What is an Anti-Corruption layer, and how is it used?
I'm trying to figure out what the Anti-Corruption layer really means. I know that it's a way to transition/work around legacy code or bad APIs. What I don't understand is how it works and what makes ...
190
votes
9
answers
150k
views
Why should I use a factory class instead of direct object construction?
I have seen the history of several С# and Java class library projects on GitHub and CodePlex, and I see a trend of switching to factory classes as opposed to direct object instantiation.
Why should I ...
179
votes
6
answers
291k
views
What is the point of using DTO (Data Transfer Objects)?
What is the point of using DTO and is it an out dated concept? I use POJOs in the view layer to transfer and persist data. Can these POJOs be considered as an alternative to DTOs?
168
votes
17
answers
287k
views
Is it better to return NULL or empty values from functions/methods where the return value is not present?
I am looking for a recommendation here. I am struggling with whether it is better to return NULL or an empty value from a method when the return value is not present or cannot be determined.
Take ...
163
votes
10
answers
77k
views
When is it not appropriate to use the dependency injection pattern?
Since learning (and loving) automated testing I have found myself using the dependency injection pattern in almost every project. Is it always appropriate to use this pattern when working with ...
162
votes
8
answers
71k
views
Criticism and disadvantages of dependency injection
Dependency injection (DI) is a well known and fashionable pattern. Most of engineers know its advantages, like:
Making isolation in unit testing possible/easy
Explicitly defining dependencies of a ...
152
votes
14
answers
21k
views
Did the Gang of Four thoroughly explore "Pattern Space"?
Ever since I first learned about the Gang of Four (GoF) design patterns, at least 10 years ago, I am having the impression that these 23 patterns should be only a small sample of something much larger ...
145
votes
6
answers
109k
views
What is the point of having every service class have an interface? [duplicate]
At the company I work at, every service class has a corresponding interface.
Is this necessary?
Notes:
Most of these interfaces are only used by a single class
We are not creating any sort of ...
139
votes
13
answers
22k
views
Are design patterns frowned upon?
I had a discussion with one of our senior developers who's been in the business for 20 years. He's pretty well known in Ontario for a blog he writes.
The strange thing is what he told me: he said ...
123
votes
9
answers
28k
views
Are design patterns really essential nowadays?
I was reading "Coders at Work" and have faced the fact that some of the professionals interviewed in the book are not so enthusiastic about design patterns.
I think that there are 2 main reasons for ...
115
votes
17
answers
24k
views
Why should 'boneheaded' exceptions not be caught, especially in server code?
I am confused because in quite a few places I've already read that the so-called 'boneheaded' exceptions (ones that result from bugs in code) are not supposed to be caught. Instead, they must be ...
107
votes
8
answers
21k
views
Are there any design patterns that are unnecessary in dynamic languages like Python?
I've started reading the design pattern book by the GoF. Some patterns seem very similar with only minor conceptual differences.
Do you think out of the many patterns some are unnecessary in a ...
102
votes
2
answers
25k
views
What is the "Free Monad + Interpreter" pattern?
I've seen people talking about Free Monad with Interpreter, particularly in the context of data-access. What is this pattern? When might I want to use it? How does it work, and how would I implement ...
87
votes
4
answers
60k
views
Is it bad practice that a controller calls a repository instead of a service?
Is it bad practice that a controller calls a repository instead of a service?
To explain more:
I figure out that in good design controllers call services and services use repositories.
But sometimes ...
86
votes
22
answers
13k
views
Is OOP hard because it is not natural?
One can often hear that OOP naturally corresponds to the way people think about the world. But I would strongly disagree with this statement: We (or at least I) conceptualize the world in terms of ...
86
votes
6
answers
59k
views
Where are all the functional programming design patterns? [closed]
OO programming literature is full of design patterns. Most books on object oriented programming dedicate a chapter or two to design patterns like factories and decorators. So what are the equivalent ...
83
votes
14
answers
10k
views
What should take precedence: YAGNI or Good Design?
At which point should YAGNI take precedence against good coding practices and vice versa? I'm working on a project at work and want to slowly introduce good code standards to my co-workers (currently ...
80
votes
3
answers
147k
views
What are DRY, KISS, SOLID, etc. classified as?
Is something like DRY a design pattern, a methodology, or something in between? They do not have specific implementations that could neccessarily be demonstrated(even if you can easily demonstrate a ...
80
votes
2
answers
13k
views
Are there any OO-principles that are practically applicable for Javascript?
Javascript is a prototype-based object oriented language but can become class-based in a variety of ways, either by:
Writing the functions to be used as classes by yourself
Use a nifty class system ...
78
votes
7
answers
103k
views
Why should I use an MVC pattern?
It seems everyone doing web applications nowadays wants to use MVC for everything. I find it hard to convince myself to use this pattern, however. I understand the general idea is to separate the ...
77
votes
5
answers
20k
views
What is the "Fix Everything" design pattern?
In this 2003 article by Stephen Figgins on linuxdevcenter.com, Bram Cohen's BitTorrent is described as using the "Fix Everything" design pattern.
A less common approach that both makes BitTorrent ...
76
votes
5
answers
84k
views
What are the benefits of using Dependency Injection and IoC Containers?
I'm planning to do a talk on Dependency Injection and IoC Containers, and I'm looking for some good arguments for using it.
What are the most important benefits of using this technique, and these ...
75
votes
10
answers
14k
views
Non-OOP Design Patterns? [closed]
I have only heard the term "design pattern" being used for object oriented code, and the GoF patterns include only OOP design patterns, but design patterns are elegant solutions for commonly occuring ...
72
votes
7
answers
74k
views
How essential is it to make a service layer?
I started building an app in 3 layers (DAL, BL, UI) [it mainly handles CRM, some sales reports and inventory].
A colleague told me that I must move to service layer pattern, that developers came to ...
72
votes
6
answers
74k
views
What is a helper? Is it a design pattern? Is it an algorithm?
Maybe a little tongue-in-cheek, but as I can't find this answer anywhere through Google, so to ensure Software Engineering has the answer:
What is a helper?
I have seen the name being used ...
71
votes
5
answers
16k
views
When is Singleton appropriate? [duplicate]
There is a widely accepted opinion that Singleton is an anti-pattern. As usual, there are always exceptions to the rule. Can you explain why Singleton is a bad choice in general and give an example of ...
70
votes
12
answers
9k
views
"Everything is a Map", am I doing this right?
I watched Stuart Sierra's talk "Thinking In Data" and took one of the ideas from it as a design principle in this game I'm making. The difference is he's working in Clojure and I'm working in ...
68
votes
5
answers
27k
views
What is inversion of control, and when should I use it?
I am designing a new system and I want to know what inversion of control (IOC) is, and more importantly, when to use it.
Does it have to be implemented with interfaces or can be done with classes?
67
votes
12
answers
34k
views
Is this a decent use-case for goto in C?
I really hesitate to ask this, because I don't want to "solicit debate, arguments, polling, or extended discussion" but I'm new to C and want to gain more insight into common patterns used in the ...
67
votes
7
answers
47k
views
Is dependency injection essential for unit testing?
Is using dependency injection (DI) essential for unit testing?
I can't think of another alternative for isolating code so it can be tested. Also, all the examples I have ever seen use this pattern. ...
65
votes
8
answers
29k
views
Is ORM an Anti-Pattern? [closed]
I had a very stimulating and interessting discussion with a colleague about ORM and its pros and cons. In my opinion, an ORM is useful only in the rarest cases. At least in my experience.
But I don't ...
65
votes
6
answers
22k
views
Is an event loop just a for/while loop with optimized polling?
I'm trying to understand what an event loop is. Often the explanation is that in an event loop, you do something until you're notified that an event has occurred. You then handle the event and ...
64
votes
10
answers
32k
views
Do you generally send objects or their member variables into functions?
Which is generally accepted practice between these two cases:
function insertIntoDatabase(Account account, Otherthing thing) {
database.insertMethod(account.getId(), thing.getId(), thing....
63
votes
6
answers
31k
views
Is object pooling a deprecated technique?
I am very familiar with the concept of object pooling and I always try to use it as much as possible.
Additionally I always thought that object pooling is the standard norm as I have observed that ...
62
votes
8
answers
65k
views
MVC Architecture -- How many Controllers do I need?
I have been coding for a while, but mostly scripts and simple applications. I've moved into a new role where it is all about developing Web Apps and using a proper MVC architecture, so I am ...
62
votes
2
answers
41k
views
Best practice/Patterns for two way data synchronisation
Quite often in my work the idea of 2-way data synchronisation between database systems crops up. The classic example is two slightly different CRM systems (say, Raiser's Edge and Salesforce) and the ...
60
votes
2
answers
29k
views
Communication between nested directives
There seem to be quite a few ways of communicating between directives. Say you have nested directives, where the inner directives must communicate something to the outer (e.g. it's been chosen by the ...
59
votes
11
answers
51k
views
Are utility classes with nothing but static members an anti-pattern in C++?
The question Where should I put functions that are not related to a class has sparked some debate over whether it makes sense in C++ to combine utility functions in a class or just have them exist as ...
58
votes
8
answers
10k
views
How do I ensure that interface implementations are implemented in the manner I expected?
Let's say there is a member SomeMethod in an interface ISomeInterface as follows:
public interface ISomeInterface
{
int SomeMethod(string a);
}
For the purposes of my program, all consumers of ...
58
votes
2
answers
50k
views
Is performance the only reason not to use SignalR (websockets) entirely in lieu of a traditional REST API?
I have used SignalR to achieve real-time messaging functionality in several of my projects. It seems to work reliably and is very easy to learn to use.
The temptation, at least for me, is to abandon ...