SOLID & Design Patterns Interview Question #124

What is an Anti-Pattern?

Architectural Patterns, Application Boundaries & Distributed Design Patterns Senior Advanced

Quick Interview Answer

An anti-pattern is a recurring approach that appears useful but commonly produces negative consequences in a particular context. Examples can include God Objects, Service Locator misuse, excessive shared mutable state, or premature abstraction.

Detailed Explanation

An anti-pattern is a recurring approach that appears useful but commonly produces negative consequences in a particular context. Examples can include God Objects, Service Locator misuse, excessive shared mutable state, or premature abstraction.

Calling something an anti-pattern should include the reason it is harmful and the circumstances in which the problem appears. A technique is not automatically wrong in every context.

Code Example

// Service Locator hides dependencies:
var repository =
    ServiceLocator.Resolve<IOrderRepository>();

// Prefer explicit constructor dependency:
public OrderService(IOrderRepository repository)
{
    _repository = repository;
}