Junior — Middle
What design patterns do you apply when developing the server part of a system?
sobes.tech AI
Answer from AI
When developing the server-side, I often use the following design patterns:
- Repository — for abstracting data access, separating database logic from business logic.
- Dependency Injection — for managing dependencies and simplifying testing.
- Singleton — for creating a single instance of services or managers, such as configuration.
- Factory — for creating objects with different configurations or types without tight coupling to specific classes.
- Strategy — for selecting algorithms or behaviors at runtime.
Example of using Repository in C#:
public interface IUserRepository {
User GetById(int id);
void Add(User user);
}
public class UserRepository : IUserRepository {
public User GetById(int id) {
// implementation of database access
}
public void Add(User user) {
// adding a user
}
}
This allows changing the data access implementation without modifying the business logic.