Questions tagged [design-patterns]
A design pattern is a general reusable solution to a commonly occurring problem in software design.
398
questions
50
votes
2
answers
24k
views
Choosing the right Design Pattern
I've always recognized the importance of utilizing design patterns. I'm curious as to how other developers go about choosing the most appropriate one. Do you use a series of characteristics (like a ...
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 ...
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 ...
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 ...
38
votes
9
answers
11k
views
How to determine if a class meets the single responsibility principle?
The Single Responsibility Principle is based on the high cohesion principle. The difference between the two is that a highly cohesive classes features a set of responsibilities that are strongly ...
34
votes
6
answers
8k
views
Should I extract specific functionality into a function and why?
I have a large method which does 3 tasks, each of them can be extracted into a separate function. If I'll make an additional functions for each of that tasks, will it make my code better or worse and ...
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 ...
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 ...
7
votes
1
answer
793
views
MVC: Display requested content via the View
I'm trying to learn how the MVC pattern works so have been playing around a bit. I just wrote this, which was making sense to me, until I wanted to display the array content which has been collect by ...
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, ...
27
votes
4
answers
9k
views
Legitimate "real work" in a constructor?
I am working on a design, but keep hitting a roadblock. I have a particular class (ModelDef) that is essentially the owner of a complex node tree built by parsing an XML schema (think DOM). I want to ...
5
votes
1
answer
2k
views
Derive from a base class but not include a condition in the base class's method
The current code looks like this:
public class Details
{
Public void Populate()
{
WriteChapterDetails();
}
public void WriteChapterDetails()
{
if ( ...
328
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?
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 ...
146
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 ...
12
votes
6
answers
4k
views
What are the practical ways to implement the SRP?
Simply what are the practical techniques people use to check if a class violates the single responsibility principle?
I know that a class should have only one reason to change, but that sentence is ...
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 ...
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 ...
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 ...
42
votes
1
answer
21k
views
What is the difference between the factory pattern and abstract factory?
Having finally begun seriously trying to learn some basic patterns (very late in the career, but that's a different story), I'm trying to get my head around the differences between the Factory Pattern ...
27
votes
3
answers
27k
views
Constructor with tons of parameters vs builder pattern
It is well know that if your class have a constructor with many parameters, say more than 4, then it is most probably a code smell. You need to reconsider if the class satisfies SRP.
But what if we ...
18
votes
3
answers
8k
views
Should Entity Framework 6 not be used with repository pattern?
So I am asking this after reading the following: Why shouldn't I use the repository pattern with Entity Framework?.
It seems there is a large split of people who say yay and those that say nay. ...
36
votes
2
answers
36k
views
Separating data access in ASP.NET MVC
I want to make sure I'm following industry standards and best practices with my first real crack at MVC. In this case, it's ASP.NET MVC, using C#.
I will be using Entity Framework 4.1 for my model, ...
31
votes
5
answers
35k
views
What is the difference between all-static-methods and applying a singleton pattern? [duplicate]
I am making a database to store information about the users of my website (I am using stuts2 and hence Java EE technology). For the database I'll be making a DBManager. Should I apply singleton ...
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 ...
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 ...
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 ...
53
votes
10
answers
11k
views
Should we avoid custom objects as parameters?
Suppose I have a custom object, Student:
public class Student{
public int _id;
public String name;
public int age;
public float score;
}
And a class, Window, that is used to show ...
48
votes
11
answers
11k
views
Are error variables an anti-pattern or good design?
In order to handle several possible errors that shouldn't halt execution, I have an error variable that clients can check and use to throw exceptions. Is this an Anti-Pattern? Is there a better way to ...
47
votes
2
answers
18k
views
Equivalent of SOLID principles for functional programming
I've found the SOLID principles quite useful when thinking about object-oriented design.
Is there a similar / equivalent set of language-agnostic principles tailored for functional programming?
39
votes
4
answers
13k
views
What is the principle of least astonishment?
In programming what is called Principle of Least Astonishment? How is this concept related to designing good APIs? Is this something applicable to only object oriented programming or does it permeate ...
21
votes
11
answers
4k
views
Why are we supposed to use short functions to sectionalize our code? [duplicate]
I've seen an increasing trend in the programming world saying that it is good practice to separate code blocks into their own functions. Obviously, if that code block is reusable, you should do that. ...
11
votes
2
answers
24k
views
.NET MVC project architecture / layering
When planning the architecture for a mid-large scale MVC web application how do you implement the layers to be as decoupled as possible and easy to test? (basically follow best practices) Let's say 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?
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 ...
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 ...
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 ...
51
votes
4
answers
24k
views
Where we should put validation for domain model
I still looking best practice for domain model validation. Is that good to put the validation in constructor of domain model ? my domain model validation example as follows:
public class Order
{
...
35
votes
7
answers
5k
views
How do I prevent unknowningly duplicating code?
I work on a rather large code base. Hundreds of classes, tons of different files, lots of functionality, takes more than 15 minutes to pull down a fresh copy, etc.
A big problem with such a large ...
30
votes
10
answers
13k
views
To design pattern, or not to design pattern [duplicate]
Design patterns are good, but complex. Should we use them in small projects? Implementing design patterns needs more sophisticated developers, which in turn raises project costs. On the other hand, ...
29
votes
3
answers
13k
views
What is the name for the idiom using method chaining to build an object?
I frequently use a pattern where I using method chaining to setup an object, similar to a Builder or Prototype pattern, but not creating new objects with each method call, instead modifying the ...
27
votes
2
answers
11k
views
Differences between Design by Contract and Defensive Programming
Could Designing by Contract (DbC) be a way to program defensively?
Is one way of programming better in some cases than the other?
17
votes
7
answers
4k
views
What if I will not use Software Design Patterns? [closed]
What kind of problems may I face, if I won't use Software Design Patterns? Can you tell me about the problems of approaching the design using standard object-oriented techniques?
9
votes
2
answers
2k
views
What is a good design pattern for multi-mode software?
I will explain my question by way of example, but I'd love a general solution.
I am writing a JavaScript application that needs to function differently depending on its run-time context. To handle ...
0
votes
3
answers
441
views
Tell one, but ask the others?
Consider we have three classes which want to collaborate, then, where is the behaviour?
I guess it can only be in one of the three classes or in a fourth one acting than as a procuedural connector-...
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 ...
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 ...
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 ...
34
votes
10
answers
30k
views
Alternatives to the singleton pattern
I have read different opinions about the singleton pattern.
Some maintain that it should be avoided at all costs and others
that it can be be useful in certain situations.
One situation in which I ...
17
votes
4
answers
9k
views
Is the Single Responsibility Principle applicable to functions?
According to Robert C. Martin, the SRP states that:
There should never be more than one reason for a class to change.
However, in his book Clean Code, chapter 3: Functions, he shows the following ...