Repaso
Español
Design & PracticesIntermediateDP-54

Which design patterns do you actually use in Java/Spring projects, and where?

Answer

  • Singleton: Spring beans are singletons by default (managed by the container).
  • Factory / Abstract Factory: BeanFactory, Calendar.getInstance(), List.of(...).
  • Builder: StringBuilder, Stream builders, Lombok @Builder, HttpRequest.newBuilder().
  • Strategy: Comparator, injecting different implementations of an interface.
  • Decorator: Java I/O streams (BufferedReader), wrapping services.
  • Proxy: @Transactional, @Cacheable, lazy loading in Hibernate.
  • Observer: events (ApplicationEventPublisher), listeners.
  • Template Method: JdbcTemplate, RestTemplate.
  • Chain of Responsibility: servlet and security filter chains.
  • Say when NOT to use a pattern: do not add one without a real problem (YAGNI).

Key terms

Practice this question in Repaso