Questions tagged [design-patterns]

A design pattern is a general reusable solution to a commonly occurring problem in software design.

In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

Design patterns are formalized practices a programmer can use to solve common problems when designing an application or system.

4387 questions
610
votes
13 answers

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, there are still many cases where I can't see a…
454
votes
12 answers

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 I have a User that has debt. I want to change his debt.…
Jeroen Vannevel
  • 5,736
  • 6
  • 18
  • 27
422
votes
17 answers

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 understanding, "Don't use global variables", or…
Madara's Ghost
  • 8,787
  • 9
  • 25
  • 33
326
votes
15 answers

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?
Lamin Sanneh
  • 3,975
  • 3
  • 19
  • 19
234
votes
10 answers

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 obvious solution is to pass a parameter into the…
RubenLaguna
  • 1,842
  • 2
  • 12
  • 11
225
votes
11 answers

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 classes. If we follow that method of designing…
user1318496
  • 1,767
  • 4
  • 9
  • 11
222
votes
10 answers

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 describe it in general terms that are unlikely to be…
Nicole
  • 28,111
  • 12
  • 95
  • 143
219
votes
16 answers

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 interesting book about clean code I've read.…
218
votes
18 answers

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 any landscape. They are affected by things like…
217
votes
14 answers

"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 handling it in code. The logic behind the idea is…
PhonicUK
  • 1,047
  • 3
  • 11
  • 12
196
votes
4 answers

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 it a clean separation from the undesirable…
knownasilya
  • 3,074
  • 3
  • 17
  • 16
190
votes
9 answers

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 use factory classes extensively? I have pretty…
rufanov
  • 1,953
  • 2
  • 12
  • 9
179
votes
6 answers

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?
Vinoth Kumar C M
  • 15,455
  • 23
  • 57
  • 86
167
votes
17 answers

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 the following two methods as an examples: string…
163
votes
10 answers

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 automated testing? Are there any situations were you…
Tom Squires
  • 17,695
  • 11
  • 67
  • 88
1
2 3
99 100