Questions tagged [design-patterns]
A design pattern is a general reusable solution to a commonly occurring problem in software design.
4,520 questions
1
vote
1
answer
216
views
Is it an acceptable pattern to put derived classes inside an abstract base class in Java?
Suppose I have some Java code such as the following (in this case, the use of the name "interaction" is referring to interacting with an object in a video game):
public abstract class ...
3
votes
1
answer
2k
views
Specification pattern and maintaining domain logic inside entity
I would like to know more about the Specification Pattern, as it is described in Eric Evan's book "Domain Driven Design".
One of the key points Evans makes is that the domain model contains ...
0
votes
1
answer
113
views
How to refactor parallel inheritance tree?
I have a (php) program, which must change yearly. This program calculates tax for every year and there are sometime changes in requirements.
First, the user fills their incomes, expenses, etc.
Then ...
1
vote
1
answer
261
views
Are there any drawbacks to partial application?
Consider the following Typescript code:
function exampleAction(target: Target, options: ExampleActionOptions) {
// ...
}
export function getExampleAction(options: ExampleActionOptions) {
return (...
1
vote
4
answers
371
views
Most efficient way to represent a random ordering of the numbers 0-10
I am currently looking for a way to encode a list of random numbers most efficently (as in length).
To be specific, I have an array of 11 numbers containing each number from 0 to 10. The order will be ...
1
vote
1
answer
261
views
Refactoring instanceOf, moving logic to POJO when it has database interaction
I'm refactoring some old code, I have a lot of istanceOf in the business part:
if (record instanceof RecordA) {
RecordA recordA = (RecordA) record;
...
1
vote
2
answers
225
views
Designing Products with multiple variations
I have a Product entity:
public class Product
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
[ForeignKey("Brand"...
2
votes
2
answers
665
views
Replacing the Decorator design pattern with a list of methods
So I've been going over some design patterns and I came across this discussion
https://stackoverflow.com/questions/43565475/using-lists-instead-of-decorator-pattern
I've been thinking that in all ...
0
votes
1
answer
161
views
How to implement converters without needing to implement every permutation
I've got this class:
[UsedImplicitly]
public class ClassicalKeplerian
{
public ClassicalKeplerian(Angle argumentOfPeriapsis, Angle inclination, UnitFraction eccentricity, Angle ...
-1
votes
1
answer
237
views
How can I use builders for products with incompatible interfaces?
I am working on a program to automatically design heater units based on varying client specifications.
The process for creating each heater is quite involved and requires multiple optional steps ...
2
votes
1
answer
161
views
Design for the future or make it tightly coupled to the implementation
My question is regarding how would the developer know when you add interfaces/protocols to their code and inject them as dependency.
Consider an example of BudgetService.
protocol ...
-1
votes
1
answer
105
views
How to solve inter-dependency of the composed class on it's property
In one of the project I am working on, I am facing a problem in terms of creating an object with a dependency that sits at a deeper level in the class composition. Following diagram shows the class ...
-2
votes
1
answer
130
views
CRUD for hierarchical data in ASP.NET - single view/controller vs. separate view/controllers?
I'm trying to develop a CRUD feature for an ASP.NET app that lets me manage customer information. Each customer can be subscribed to one or more services, and each service can have one or more terms.
...
3
votes
3
answers
673
views
Definition of "collaborators" (of an object) in Software Design?
I have seen Martin Fowler using the term "collaborators" as some sort of synonym of "dependencies". Unfortunately, Martin Fowler does not seem to define the term anywhere, so it is ...
0
votes
1
answer
327
views
Exposing only the getters of a singleton interface in C++
I've got a file in my includes folder, which is the folder I expose, that isn't used externally and isn't supposed to be used too. When I noticed that and tried to remove it - I noticed that it's ...
0
votes
2
answers
801
views
How to create an interface in C that can work on two identical structs with differently named fields
Question Background
Consider a scenario in which I have two structs. Both consist of three fields for doubles. The only difference is the names used to refer to these fields. The first struct is for ...
1
vote
1
answer
537
views
What is an apporpriate design pattern when dealing with Pandas and databases?
We're dealing with a lot of "data analysis", basically different sorts of data mangling, aggregations and calculations using Pandas. Usually, the data is time series data.
All underlying ...
0
votes
0
answers
150
views
Python Typechecking versus TypedDicts?
From what I understand from this answer, it is not possible to use a typeddict and typechecking in a function. So for example, if one has a function:
def some_func(some_int: int, some_dict:...
1
vote
2
answers
110
views
Sharing a data class in a flow
Suppose there is a flow of functions in C++
step1();
step2();
step3();
step4();
step5();
and they interact by adding and modifying data on a data class D (only data, no functions). For example, step1(...
0
votes
3
answers
2k
views
Best way to provide default objects from a simple class in C#
I have a Material class :
public class Material
{
public string Name { get; private set; }
public double Density { get; private set; }
public double SpecificHeat { get; private set; }
...
15
votes
6
answers
6k
views
Accessing enemies' locations quickly in a 2D game
I have a simple 2D game which has squares, player (teal) and enemies (red).
class Game {
List<List<Square>> map;
Player player;
List<Enemy> enemies; ...
My problem is that I ...
2
votes
1
answer
3k
views
DDD: Can application services throw domain errors?
I'm new to DDD and I trying to create an API using DDD concepts for study purposes. Today, I faced a scenario where I've to create an Application Service to expose a functionality to be cosumed from ...
0
votes
1
answer
190
views
Is this a good use of events?
At my company we are revising some backend architecture. I think I have identified a use case where event streaming (for example with Apache Kafka or RabbitMQ) makes sense.
Because me (and everyone ...
3
votes
1
answer
191
views
Should I have different instances of the same model in an application or just keep a single instance?
What is correct in your opinion regarding the creation and handling of models in an application let's say using MVVM, or even an MVC design pattern?
I will try to illustrate the situation I see at my ...
3
votes
2
answers
2k
views
Is it good design to have a repository update multiple entities?
I'm building a web application using Laravel. I use the repository pattern as my data layer.
Imagine there's some entity like Product and a product can be assigned to a ProductCategory.
The Product ...
1
vote
2
answers
261
views
physical simulation: design thoughts
I'm an applied physics student and currently working on a simulation of the magnetic interactions of multiple protons within a protein.
Me having only little experience with programming and almost ...
3
votes
4
answers
1k
views
Giving multiple components access to a single database
This is in context of a distributed computing. There is a Service A which owns a database and hosts APIs for updating an entities in this database.
As time went by, the service has evolved and we are ...
-2
votes
1
answer
82
views
Pattern for flexible promotions/badges [closed]
Let’s imagine system like Garmin connect, you run, record your workout and then once you finish workout is uploaded to the system and being analyzed. Based on analysis you can receive badge: for 5 km ...
1
vote
0
answers
124
views
How to have only one consumer handle a long running task at a given point?
I need to design an application which makes requests to an external API. External API has a hierarchy of entities: User which contains ListOfItemss which contain Items. I need to get data on all Items ...
0
votes
1
answer
168
views
C# Best Practice DI Pattern?
Is there a best-practice pattern I can/should use for this scenario? I need access to some DI services in the OnFinished() method:
public class Product
{
public int Quantity {get;set;}
}
public ...
2
votes
3
answers
631
views
What is the name, or design pattern, for something that calls a list of functions one by one?
The following design emerged from using Azure Durable Functions with lots of fan out, which turned out to not work very well. Our starting point was an Orchestrator with a companion Durable Entity for ...
0
votes
1
answer
465
views
Where should I create my aggregate root? in api or frontend?
I am writing a simple application to apply what I have learned so far in DDD.
I have the following mysql tables in my api server
Sales
Column
id
pk, int
title
varchar
description
varchar
Images
Column
...
0
votes
1
answer
1k
views
Pattern to convert different types of similar unchangeable objects to the same object
I have an existing system that inserts books into a library database, and I want to generalize it to include other media.
The other media is similar, but not exactly the same, and all media are third ...
0
votes
1
answer
528
views
Multiple models/controllers python app
I am trying to tinker with the MVC pattern and I have a problem when trying to design the MVC structure. I need a model for products, however as I need to manage more and different data, I can help ...
3
votes
3
answers
1k
views
CQRS in a data heavy application involving a lot of CRUD
I've been developing a non-trivial personal application and I thought I'd use CQRS to learn it. The application is very data entry heavy, lots of entities and lots of forms. I find myself constantly ...
0
votes
1
answer
1k
views
What design pattern / solution helps me elegantly map classes that share the same base class
I'm looking for an elegant way (a design pattern if such exists, not a library) to map two classes that share the same base class, without duplicating the code that maps the properties of the base ...
2
votes
1
answer
406
views
C++ behavior as a template argument
I would like to have a class, let's say a queue, that can be specialized to its behavior. For example, let them be a SafeQueue (which disables IRQs before any access) and a FastQueue (which doesn't). ...
1
vote
1
answer
4k
views
Return type specification if a Python function output type, is dependent on the input arguments of that function?
Context
Suppose one has a list of algorithms, which each have a multiple run/parameter configurations. Next, one wrote a generic function def get_mdsa_configs(self) -> List[MDSA_config]: that ...
2
votes
1
answer
1k
views
Best practice/design pattern for working with a config file
I am writing an application where a user will have a custom config json file. The json is just a dictionary where they define new objects of the exact same type. Here is an example with outfits:
'...
3
votes
2
answers
622
views
Process many types of work in parallel, but sequential for each type of work
Imagine there is a stream of requests for about 500 types of work. There can be say 5 workers in parallel. One type of work should be executed by at most one worker at the same time. The requests for ...
0
votes
0
answers
146
views
How to handle Authorized & Non-Authorized Routes in Microservices with Tokens
Context
Suppose there is a e-commerce microservice architecture with the format:
In summary, a client will contact the Auth Service / Identity Service to receive a token. It passes the token to the ...
1
vote
2
answers
2k
views
Software design patterns for versioning of domain entities/logic
I'm looking for specific software design patterns that can help me understand how versioning of the same entity where the domain logic has changed between the versions, can be solved. The following ...
0
votes
1
answer
137
views
Data autonomy using files in a micro service application
We are rebuilding an on premise application using microservices and are wondering how to implement data autonomy given certain constraints that we are facing:
The application is built around a set of ...
1
vote
2
answers
222
views
Why do we separate interface when implementing interface injection variant of DI?
With interface injection (wikpedia) we have a method to set the dependency on the client as part of an interfase.
public interface ServiceSetter {
public void setService(Service service);
}
Why ...
0
votes
1
answer
192
views
Best practices to link, associate, or abstract similar but different entities together
What are the best practices that I can follow that can simplify or help abstract out entity modeling when there are many similar but different entities?
I have a large game system with different types ...
1
vote
1
answer
420
views
Self-Selecting Variant of the Strategy Pattern?
I've found this pattern useful, and am trying to classify or name it.
Basically, that:
A task should be performed by different strategies, depending on the context.
Each concrete strategy implements ...
0
votes
2
answers
399
views
Architecture for worker pool with different task complexities and sizes of boxes
I'm sorry if I'm using any wrong terminology here.
I'm trying to design an architecture where there can be big and small tasks (e.g. processing big or small images). Big tasks can only be handled by ...
1
vote
1
answer
220
views
Using the decorator pattern to populate entity properties
I have products saved in my database that my system fetches with the following structure:
public class OriginalProduct {
public string Id { get; set; }
public string Name { get;set; }
}
I need ...
-1
votes
3
answers
1k
views
Refactoring multiple non-nested if statements [C#]
I have a number of non-nested if statements that look like this:
if (!bytes[nameof(PropertyVersion.Price)].SequenceEqual(dbBytes[nameof(PropertyVersion.Price)])) changes += $"Price {TextHelper....
6
votes
6
answers
557
views
Best practice to architect a system in which local users may override global definitions
Our travel industry client, operating across three continents and several countries, wants to standardize the process of choosing travel packages, air routes, hotels, and hundreds of other travel ...