.NET

.NET Design Patterns

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

24 面接問題·
Senior
1

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

回答

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?

回答

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?

回答

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 面接問題

次の面接に向けて.NETをマスター

すべての問題、flashcards、技術テスト、コードレビュー演習、面接シミュレーターにアクセス。

無料で始める