Questions tagged [design-patterns]

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

Filter by
Sorted by
Tagged with
45 votes
2 answers
21k 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 ...
user avatar
407 votes
17 answers
150k 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 ...
user avatar
82 votes
2 answers
12k 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 ...
user avatar
  • 14.6k
184 votes
4 answers
73k 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 ...
user avatar
  • 2,784
36 votes
9 answers
10k 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 ...
user avatar
  • 1,091
32 votes
6 answers
7k 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 ...
user avatar
  • 1,351
71 votes
5 answers
15k views

When is Singleton appropriate? [closed]

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 ...
user avatar
  • 25.8k
123 votes
9 answers
26k 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 ...
user avatar
  • 2,693
6 votes
1 answer
700 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 ...
user avatar
  • 309
26 votes
4 answers
8k 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 ...
user avatar
  • 379
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 ( ...
user avatar
  • 71
603 votes
13 answers
259k 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, ...
user avatar
  • 20.4k
218 votes
10 answers
231k 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 ...
user avatar
  • 27.8k
307 votes
15 answers
150k 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?
user avatar
  • 3,785
135 votes
6 answers
97k 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 ...
user avatar
  • 1,677
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 ...
user avatar
  • 6,520
72 votes
7 answers
71k 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 ...
user avatar
  • 1,257
216 votes
14 answers
106k 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 ...
63 votes
8 answers
27k 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 ...
user avatar
  • 839
42 votes
1 answer
20k 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 ...
user avatar
  • 1,298
17 votes
3 answers
6k 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. ...
user avatar
  • 403
23 votes
3 answers
22k 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 ...
user avatar
  • 1,063
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, ...
user avatar
31 votes
5 answers
34k 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 ...
user avatar
  • 599
442 votes
12 answers
171k 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 ...
user avatar
181 votes
9 answers
145k 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 ...
user avatar
  • 1,863
212 votes
16 answers
49k 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 ...
user avatar
47 votes
2 answers
17k 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?
user avatar
  • 20.3k
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'...
user avatar
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 ...
user avatar
  • 5,049
45 votes
11 answers
9k 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 ...
user avatar
20 votes
11 answers
3k 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. ...
user avatar
  • 402
173 votes
6 answers
276k 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?
user avatar
106 votes
8 answers
20k 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 ...
user avatar
  • 2,183
73 votes
10 answers
13k 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 ...
user avatar
  • 11.1k
47 votes
4 answers
23k 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 { ...
user avatar
65 votes
12 answers
33k 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 ...
user avatar
  • 1,603
28 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, ...
user avatar
27 votes
2 answers
10k 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?
user avatar
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 ...
user avatar
  • 5,647
34 votes
7 answers
4k 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 ...
user avatar
  • 22.3k
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?
user avatar
  • 221
28 votes
3 answers
12k 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 ...
user avatar
  • 2,162
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 ...
user avatar
  • 1,082
0 votes
3 answers
421 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-...
user avatar
150 votes
8 answers
69k 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 ...
user avatar
  • 17.5k
77 votes
7 answers
102k 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 ...
user avatar
  • 8,073
31 votes
9 answers
26k 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 ...
user avatar
  • 19.1k
14 votes
3 answers
4k views

Recommend a design pattern/approach to exposing/tolerating/recovering from system errors, Exception handling (e.g.s in Java, C++, Perl, PHP)

Can you recommend a design pattern/approach to exposing/tolerating/recovering from system errors, Exception handling (Java, C++, Perl, PHP)? Some errors need to be reported. Some errors can be ...
user avatar
15 votes
5 answers
94k views

A way to return multiple return values from a method: put method inside class representing return value. Is it a good design?

I need to return 2 values from a method. My approach is as follows: create an inner class with 2 fields that will be used to keep those 2 values put the method inside that class instantiate the ...
user avatar
  • 1,351

1
2 3 4 5
8