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
21 votes
10 answers
4k views

Can/should the Single Responsibility Principle be applied to new code?

The principle is defined as modules having one reason to change. My question is, surely these reasons to change are not known until the code actually starts to change?? Pretty much every piece of code ...
SeeNoWeevil's user avatar
21 votes
6 answers
12k views

Strategy vs Factory design pattern

I am new to design patterns and working my way through the Factory Method and Strategy patterns. I understand that Factory is a creational pattern and Strategy is behavioral but I struggle to ...
Reynier Booysen's user avatar
21 votes
1 answer
6k views

What is priming the pump? Sometimes called a priming read

I was taught this expression and pattern way back in the day. Sure, the name comes from old pumps that needed to be filled with water before they could pump water, but who cares? We're talking about ...
candied_orange's user avatar
21 votes
5 answers
2k views

How to avoid giant glue methods?

In my current job, I've been tasked with cleaning up old code a few times. Often the code is a labyrinth and the data behind it is even more tangled. I find myself combing out things into nice, neat,...
cmhobbs's user avatar
  • 321
21 votes
1 answer
47k views

Design pattern for object conversion (java)

I don't use design patterns very often, besides an occasional factory and MVC, and I want to start using them more. I have a concrete case at hand that I would like your opinion on the use of design ...
Julius's user avatar
  • 568
21 votes
2 answers
8k views

How do you manage config with dependency injection?

I am a big fan of DI/IOC. It is great for handling/abstracting away hard dependencies, and makes life a little easier. However I have a small gripe with it, which I am not sure how to solve. The ...
ArTs's user avatar
  • 720
21 votes
4 answers
6k views

When to use repository pattern

I have read recently that it is not good practice to use the repository pattern in conjunction with an ORM. From my understanding this is because the abstraction they provide over the SQL database is ...
user1450877's user avatar
  • 1,050
20 votes
5 answers
7k views

What are the benefits of dependency injection in cases where almost everyone needs access to a common data structure?

There are plenty of reasons why globals are evil in OOP. If the number or size of the objects needing sharing is too large to be efficiently passed around in function parameters, usually everyone ...
vsz's user avatar
  • 1,496
20 votes
7 answers
6k views

What is "premature abstraction"?

I've heard the phrase being thrown arround and to me the arguments sound completely insane (sorry if I'm strawmaning here, Its not my intention), generally it goes something along the lines of: You ...
James's user avatar
  • 327
20 votes
7 answers
5k views

Is writing your own Data Access / Data Mapping Layer a "good" idea?

We're currently in a situation where we have a choice between using an out-of-the-box object-relational mapper or rolling our own We have a legacy application (ASP.NET + SQL Server) where the data-...
Eoin Campbell's user avatar
20 votes
4 answers
27k views

Returning a boolean when success or failure is the sole concern

I often find myself returning a boolean from a method, that's used in multiple locations, in order to contain all the logic around that method in a single place. All the (internal) calling method ...
Ben's user avatar
  • 728
20 votes
5 answers
43k views

Best OOP design pattern for a sequence of operations

I am working on an application, a module of which does the following financial operations sequentially: When a user requests for a certain amount to be transferred into her bank account: check ...
kumar's user avatar
  • 335
20 votes
4 answers
10k views

What is the difference between Single Responsibility Principle and Separation of Concerns

a)What is the difference between SRP and SoC? Perhaps that SRP is applied at class level, while SoC can be applied at system, subsystem, module, class or function levels. b) If answer to a) is yes, ...
user1483278's user avatar
  • 1,131
19 votes
9 answers
9k views

How important are design patterns in programming?

I'm a university student and I've just started learning about design patterns and im struggling to understand the purpose of them. I have tried researching them but all the resources I have found seem ...
Roy James Schumacher's user avatar
19 votes
3 answers
66k views

In MVC , DAO should be called from Controller or Model

I have seen various arguments against the DAO being called from the Controller class directly and also the DAO from the Model class.Infact I personally feel that if we are following the MVC pattern , ...
user avatar
19 votes
2 answers
5k views

How are generics implemented in a modern compiler?

What I mean here is how do we go from some template T add(T a, T b) ... into the generated code? I've thought of a few ways to achieve this, we store the generic function in an AST as Function_Node ...
Jon Flow's user avatar
  • 373
19 votes
3 answers
23k views

Model-View-Controller: Does the user interact with the View or with the Controller?

I have recently learned about the MVC design pattern. I'm learning from the Head First Design Pattern book. According to this book (if I understand correctly): The Model is most of the application ...
Aviv Cohn's user avatar
  • 21.4k
19 votes
5 answers
4k views

What is the meaning of "inversion" in Dependency Inversion design principle?

I'm reading about design patterns. I know what this principle does. High-level and low-level classes depend on abstractions. But why we say this is inversion?
Milad Khajavi's user avatar
19 votes
3 answers
12k views

How to store prices that have effective dates?

I have a list of products. Each of them is offered by N providers. Each providers quotes us a price for a specific date. That price is effective until that provider decides to set a new price. In ...
edmz's user avatar
  • 301
18 votes
5 answers
12k views

Do enums create brittle interfaces?

Consider the example below. Any change to the ColorChoice enum affects all IWindowColor subclasses. Do enums tend to cause brittle interfaces? Is there something better than an enum to allow for ...
Matthew James Briggs's user avatar
18 votes
8 answers
4k views

Refactoring a long method which is based on large number of switch cases [duplicate]

We are using Java as a backend development language. One year back, we wrote a method which uses switch cases based on Enums values. Since we are continuously adding enum members and according adding ...
Free Coder's user avatar
18 votes
6 answers
3k views

Pure functional vs tell, don't ask?

"The ideal number of arguments for a function is zero" is plain wrong. The ideal number of arguments is exactly the number needed to enable your function to be side-effect free. Less than that and you ...
gaazkam's user avatar
  • 4,439
18 votes
8 answers
1k views

How do "You Ain't Gonna Need it" and "Now is better than never" play together?

I often find myself embracing "now is better than never" when I'm advancing the DRYness of a design. Typically, I find that I need to cultivate an understanding of the One Authoritative Location for ...
Justin Myles Holmes's user avatar
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. ...
MZawg's user avatar
  • 423
18 votes
2 answers
1k views

Is it a good idea to define one big private function in a class to maintain valid state, that is, to update the object's data members?

Although in the code below a simple single item purchase in an e-commerce site is used, my general question is about updating all data members to keep an object's data in valid state at all times. I ...
site80443's user avatar
  • 299
18 votes
6 answers
11k views

Advantages of Strategy Pattern

Why is it beneficial to use the strategy pattern if you can just write your code in if/then cases? For example: I have a TaxPayer class, and one of its methods calculates the taxes using different ...
Armon Safai's user avatar
18 votes
4 answers
15k views

Java - Is it a bad idea to have fully static classes?

I'm working on a larger solo project and right now, and I have several classes in which I do not see any reason to create an instance of. My dice class right now, for example, stores all of its data ...
YianniW's user avatar
  • 191
18 votes
2 answers
4k views

Should I expose a "computed" value as a property or a method?

I have a C# class that represents a content type in a web content management system. We have a field that allows a web content editor to enter an HTML template for how the object is displayed. It ...
Charles Wesley's user avatar
18 votes
4 answers
6k views

Should injecting dependencies be done in the ctor or per method?

Consider: public class CtorInjectionExample { public CtorInjectionExample(ISomeRepository SomeRepositoryIn, IOtherRepository OtherRepositoryIn) { this._someRepository = ...
StuperUser's user avatar
  • 6,123
18 votes
5 answers
4k views

Why is MVC more popular than PAC? [closed]

I just stumbled upon a question at SO about PAC and got interested in the pattern. I'm wondering why it's not as widely used as MVC? What is the MVC benefits compared to PAC?
jgauffin's user avatar
  • 4,552
18 votes
2 answers
2k views

Is there evidence that the use of dependency injection improves outcomes in software engineering?

Notwithstanding its popularity, is there any empirical evidence that shows that Dependency Injection (and/or using a DI container) helps with, say, reducing bug counts, improving maintainability, or ...
NMrt's user avatar
  • 479
18 votes
3 answers
21k views

Is it ok for services to reference one another?

I have a service that needs information implemented by another service in the same layer. Is it good practice for one service to take a dependency on another service in the same layer?
TugboatCaptain's user avatar
18 votes
1 answer
4k views

Does the "function returning function" pattern have a name in JavaScript?

I use this pattern quite often in JavaScript. Here is an example: const comments = [ { text: 'Hello', id: 1 }, { text: 'World', id: 4 }, ]; const byId = id => element => element.id === id; ...
Vince Varga's user avatar
18 votes
1 answer
12k views

Open Close Principle (OCP) vs Dependency Inversion Principle (DIP)

I was trying to understand the difference between Open Closed Principle (OCP) and Dependency Inversion Princible (DIP). Based on research I've made on the internet so far, I came to the conclusion ...
Jitendar's user avatar
  • 381
18 votes
6 answers
3k views

How do you make a GUI for a polymorphic class?

Let's say I've got a test builder, so that teachers can create a bunch of questions for a test. However, not all questions are the same: You have multiple choice, text box, matching, and so on. ...
Nathan Merrill's user avatar
18 votes
3 answers
3k views

Newbie question about Decorator design pattern

I was reading a programming article and it mentioned the Decorator pattern. I've been programming for awhile but without any kind of formal education or training, but I'm trying to learn about the ...
Jim's user avatar
  • 1,997
18 votes
1 answer
2k views

Does Liskov Substitution Principle also apply to classes implementing an interface?

LSP states that classes should be substitutable for their base classes, meaning that derived and base classes should be semantically equivalent. But does LSP also apply to classes implementing an ...
user1483278's user avatar
  • 1,131
18 votes
3 answers
7k views

How to design website workflow?

I have been thinking about this for really long time without reaching an optimum answer. First of all, I'm a medical doctor who loves programming but never really studied it, except for home learning ...
Zalaboza's user avatar
  • 411
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?
iStudent's user avatar
  • 221
17 votes
4 answers
5k views

Can't grasp programming design patterns

I've been working with javascript for the past 4 years. I'm very confident about my problem solving skills and I can see that my code quality is improving. I try to stay up to date with the community ...
Abensur's user avatar
  • 281
17 votes
8 answers
4k views

Is Dependency Injection worth it outside of UnitTesting

Given a constructor that will never, ever, have to use any different implementations of several objects that it initializes, is it still practical to use DI? After all, we might still want to unit ...
Seph's user avatar
  • 279
17 votes
2 answers
2k views

Principle of least astonishment (POLA) and interfaces

A good quarter of a century ago when I was learning C++, I was taught that interfaces should be forgiving and as far as possible not care about the order that methods were called since the consumer ...
Robbie Dee's user avatar
  • 9,815
17 votes
4 answers
45k views

Which design pattern is more suitable for logging?

I should log some events in a program but as far as I know it would be better to keep the logging code outside the program because it is not about the real functionality of the program. So may you ...
user1892555's user avatar
17 votes
4 answers
6k views

What is pattern based programming?

Can somebody explain the obsession with patterns and anti-patterns in programming? I ask because I have absolutely no idea what any of the patterns mean. When faced with a programming task I think ...
user avatar
17 votes
6 answers
3k views

DDD meets OOP: How to implement an object-oriented repository?

A typical implementation of a DDD repository doesn't look very OO, for example a save() method: package com.example.domain; public class Product { /* public attributes for brevity */ public ...
ttulka's user avatar
  • 363
17 votes
1 answer
49k views

What are Repositories, Services, and Actions/Controllers?

I started a project using Slim3 and PHP using limited knowledge of application architecture. The plan was to create the project and separate application concerns. It was all going well, but things got ...
BugHunterUK's user avatar
17 votes
1 answer
35k views

Business logic vs Service layer

I read this answer: https://softwareengineering.stackexchange.com/a/234254/173318 please correct my understanding. Business rules refers to list of steps of business in real world (no codes). ...
Kakashi's user avatar
  • 309
17 votes
6 answers
4k views

Strategies for avoiding SQL in your Controllers... or how many methods should I have in my Models?

So a situation I run into reasonably often is one where my models start to either: Grow into monsters with tons and tons of methods OR Allow you to pass pieces of SQL to them, so that they are ...
Keith Palmer Jr.'s user avatar
17 votes
2 answers
31k views

Factory Pattern and/or Dependency Injection? [duplicate]

I understand the concept and can use both the Factory Pattern and Dependency Injection, however they seem a little at odds with each other conceptually. Is it a case of using one over the other? Or ...
Samuel Hawksby-Robinson's user avatar
17 votes
3 answers
5k views

Does the state Pattern violate Liskov Substitution Principle?

This image is taken from Applying Domain-Driven Design and Patterns: With Examples in C# and .NET This is the class diagram for the State Pattern where a SalesOrder can have different states during ...
Songo's user avatar
  • 6,588

1
3 4
5
6 7
90