.NET

.NET Design Patterns

Singleton, Factory, Builder, Strategy, Decorator, Repository, Unit of Work, SOLID

24 câu hỏi phỏng vấn·
Senior
1

What is the main issue with the Singleton pattern in modern .NET with Dependency Injection?

Câu trả lời

The classic Singleton pattern (static instance) conflicts with dependency injection because it creates tight coupling and makes testing difficult. In modern .NET, it's better to use the 'Singleton' service lifetime via DI (services.AddSingleton), which provides the same single-instance guarantee while enabling injection, mocking, and testability. Static Singleton bypasses the DI container and creates hidden dependencies.

2

In the Factory Method pattern, what is the difference between Factory Method and Abstract Factory in .NET?

Câu trả lời

Factory Method defines a virtual method to create a single type of object (e.g., CreateLogger), allowing subclasses to decide which concrete class to instantiate. Abstract Factory defines an interface for creating families of related objects (e.g., IUIFactory with CreateButton, CreateTextBox), allowing multiple coherent products to be created together. Factory Method focuses on a single product with polymorphism, Abstract Factory manages multiple products from the same family.

3

What is the main advantage of the Builder pattern in .NET for creating complex objects with many optional parameters?

Câu trả lời

The Builder pattern avoids telescoping constructors (multiple overloads with different parameter combinations) by providing a fluent interface to build the object step by step. It allows creating complex objects with optional parameters in a readable and maintainable way, unlike constructors with 10+ parameters. In modern C#, init properties and records can replace Builder for simple cases, but Builder remains useful for complex validation or multi-step construction.

4

What is the Single Responsibility Principle (SRP) in SOLID and how to apply it in .NET?

5

What is the Open/Closed Principle (OCP) in SOLID and how to implement it with interfaces in .NET?

+21 câu hỏi phỏng vấn

Nắm vững .NET cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí