ArchitectMind
  1. Home
  2. Tutorials
  3. Book 5 · Software Design & Design Patterns
  4. Volume III
  5. Pattern vs Framework
Book 5 · Volume III — Creational & Structural Design Patterns · Topic 1.3

Pattern vs Framework

Pattern, library, framework and architecture

9 min readStage 2 · Frameworks & design8 interview questions
Chapter
Chapter 1 — Design Pattern Fundamentals
Prerequisites
Topics 1.1 and 1.2
You will be able to
Distinguish patterns, libraries, frameworks and architectures as different kinds of reuse, explain inversion of control, recognise patterns inside frameworks, and decide what to build yourself versus take from a framework
Covers
Pattern · Library · Framework · Architecture
On this page
  1. 1. Concept / Definition
  2. 2. Why It Exists
  3. 3. Core Concepts
  4. 4. How It Works Internally — Frameworks Built from Patterns
  5. 4A. Deep Dive — Framework Coupling and Architecture
  6. 5. APIs / Features / Syntax — Extension Points in Frameworks
  7. 6. Practical Examples
  8. 7. Common Problems & Mistakes
  9. 8. Performance & Production Considerations
  10. 9. Architecture & Design Considerations
  11. 10. Interview Questions & Answers

1Concept / Definition

Software reuse comes in different forms. The TOC compares four:

Patterns, libraries, frameworks and architecture
Figure 1 — Patterns, libraries, frameworks and architecture
FormWhat is reusedWho is in controlExample
PatternA design ideaYou implement itStrategy, Adapter
LibraryCode you callYour code calls the libraryJackson, Guava, java.time
FrameworkCode + control flow — a skeleton you fill inThe framework calls your codeSpring, JUnit, Jakarta EE
ArchitectureSystem structure and its rulesOrganisation-wide decisionsLayered, hexagonal, microservices
Table 1 — Four forms of reuse

2Why It Exists

In the 1990s, object-oriented frameworks (MacApp, ET++, later Java EE) and design patterns grew together — several GoF authors worked on frameworks, and frameworks came to be described as partially complete applications that are often built from patterns. Confusion between the terms persists: teams say "we use the Spring pattern" or "we implemented a hexagonal framework". Precise distinctions matter for decisions: patterns are free to adapt, libraries are easy to swap, frameworks shape your whole codebase, and architectures shape your organisation.

3Core Concepts

3.1 Pattern

A pattern is abstract design knowledge: no code to download, no version to upgrade. You apply it by writing code shaped by the pattern.

PropertyPattern
ArtefactDescription (intent, structure, consequences)
ReuseOf design experience
CouplingNone to a vendor or version
AdaptationUnlimited — implement as your context requires
Table 2 — Pattern properties

3.2 Library

A library is reusable code with an API that your code calls when it needs it. Your code keeps control of the flow.

JAVA
// Library: your code decides when to call Jackson
ObjectMapper mapper = new ObjectMapper();
Order order = mapper.readValue(json, Order.class);
PropertyLibrary
Control flowOwned by the application
CouplingTo the library API (can be wrapped behind an adapter)
ReplaceabilityUsually high — especially behind a port
ExamplesJackson, Apache Commons, Guava, Caffeine, java.time
Table 3 — Library properties

3.3 Framework

A framework is a reusable, semi-complete application: it defines the overall control flow and calls your code at defined extension points. This is inversion of control — the "Hollywood Principle": don't call us, we'll call you.

Library vs framework: who calls whom?
Figure 2 — Library vs framework: who calls whom?
JAVA
// Framework: Spring decides when your code runs
@RestController
class OrderController {
    @GetMapping("/orders/{id}")
    OrderView get(@PathVariable String id) { … }   // called by DispatcherServlet
}
 
// JUnit is a framework too: it discovers and calls your test methods
class OrderTest { @Test void totalsLines() { … } }
PropertyFramework
Control flowOwned by the framework
Extension pointsInterfaces, annotations, callbacks, configuration
CouplingDeep — application structure follows the framework
ReplaceabilityLow — migrations are large projects
ExamplesSpring, Jakarta EE, Quarkus, Micronaut, JUnit, Hibernate (partly)
Table 4 — Framework properties
Patterns embodied in Spring
Figure 3 — Patterns embodied in Spring

3.4 Architecture

An architecture is the fundamental organisation of a system — components, relationships, principles governing design and evolution. Architecture patterns (layers, hexagonal, microservices, event-driven, CQRS) are reusable architecture ideas; architecture is what you get when you apply them to a concrete system with its quality attributes and constraints.

Design pattern vs architecture pattern
Figure 4 — Design pattern vs architecture pattern
PropertyArchitecture
ScopeWhole system or subsystem
DecisionsExpensive to reverse (Book 4, Topic 10.7)
InfluencesQuality attributes, team structure, deployment
Relation to frameworksFrameworks should serve the architecture, not dictate it
Table 5 — Architecture properties

4How It Works Internally — Frameworks Built from Patterns

Framework mechanismPattern
DispatcherServlet delegating to handler mappings and adaptersFront Controller, Strategy, Adapter
JdbcTemplate with callbacksTemplate Method (callback variant), Facade
@Transactional, @CacheableProxy (AOP)
BeanFactory, FactoryBean, @BeanFactory Method / Abstract Factory
Bean scopesSingleton, Prototype (container-managed)
ApplicationEventPublisherObserver / Mediator
Servlet filters, Spring Security filter chainChain of Responsibility / Decorator
HandlerInterceptorInterceptor (Decorator-like)
Table 6 — Patterns inside Spring

Knowing these patterns explains framework behaviour: why @Transactional doesn't work on self-invocation (it's a proxy), why singleton beans must be thread-safe (one shared instance), and where to extend the framework (strategy interfaces, customizers).

4ADeep Dive — Framework Coupling and Architecture

4A.1 How deep should framework coupling go?

ApproachDescriptionTrade-off
Framework everywhereDomain classes use framework annotations and typesFast to build; hard to migrate or test without framework
Framework at the edgesDomain/application code plain Java; framework in adapters and configuration (hexagonal)More mapping/wiring; core independent
Pragmatic middleAnnotations like @Service/@Transactional allowed in application layer; domain framework-freeCommon balance
Table 7 — Degrees of framework coupling

4A.2 Build vs take

QuestionIf yes
Does a mature library/framework solve this generic problem?Take it (don't reinvent logging, HTTP, JSON)
Is it core business logic?Build it — patterns, not frameworks
Would the framework dictate your domain model?Keep it at the edges
Is the library volatile or vendor-specific?Wrap it behind a port (Adapter)
Table 8 — Deciding what to build

5APIs / Features / Syntax — Extension Points in Frameworks

FrameworkTypical extension points
SpringBeans, @Bean methods, *Customizer, BeanPostProcessor, HandlerInterceptor, event listeners
Spring BootAuto-configuration, @ConditionalOn…, starters
JUnit 5Extension API: BeforeEachCallback, ParameterResolver
Jakarta EECDI producers, interceptors, JAX-RS filters
Servlet APIFilter, ServletContainerInitializer
Table 9 — Where frameworks call your code

6Practical Examples

6.1 Same need, four forms of reuse

Need: "Retry calls to the payment provider."

FormWhat you'd use
PatternRetry pattern with exponential backoff and jitter (design idea)
LibraryResilience4j Retry called explicitly in your adapter
FrameworkSpring Framework 7 @Retryable applied by proxies
ArchitectureService mesh retries configured at platform level
Table 10 — One need, four levels

6.2 Wrapping a library behind a port

JAVA
public interface JsonCodec {                       // port owned by the application
    <T> T decode(String json, Class<T> type);
    String encode(Object value);
}
 
final class JacksonJsonCodec implements JsonCodec { // adapter over the library
    private final ObjectMapper mapper;
    JacksonJsonCodec(ObjectMapper mapper) { this.mapper = mapper; }
    public <T> T decode(String json, Class<T> type) {
        try { return mapper.readValue(json, type); }
        catch (JsonProcessingException e) { throw new InvalidJson(e); }
    }
    public String encode(Object v) {
        try { return mapper.writeValueAsString(v); }
        catch (JsonProcessingException e) { throw new InvalidJson(e); }
    }
}

Wrapping is worthwhile when the library is volatile or used widely in the core; for stable, ubiquitous libraries at the edges, direct use is fine.

6.3 Classification exercise

ItemKind
RepositoryPattern (PoEAA)
Spring Data JPAFramework (generates repositories, calls your queries)
Hibernate ORMFramework/library hybrid
HikariCPLibrary
Hexagonal architectureArchitecture pattern
Resilience4jLibrary (with Spring integration)
Circuit breakerPattern
Table 11 — Classifying common items

7Common Problems & Mistakes

  • Calling a framework "a pattern" or a pattern "a framework".
  • Letting the framework dictate the domain model without a conscious decision.
  • Reinventing libraries for generic problems (custom JSON, HTTP, logging).
  • Wrapping everything — adapters around stable, ubiquitous libraries add little.
  • Ignoring the patterns inside frameworks, leading to proxy, scope and lifecycle surprises.

8Performance & Production Considerations

AspectConsideration
Framework startupClasspath scanning, proxies and auto-configuration cost startup time (AOT helps)
Framework upgradesMajor upgrades are projects; plan them in the roadmap
Library CVEsDependency scanning and timely upgrades
Architecture patternsDrive infrastructure cost and operational complexity
Table 12 — Operational aspects

Framework choices dominate startup time, memory footprint and upgrade effort, so measure them rather than assume. Classpath scanning, proxy generation and auto-configuration typically add seconds to a Spring Boot start; Spring AOT processing, GraalVM native images, class data sharing or the Java 25 AOT cache reduce this where fast scaling or serverless deployment matters. Libraries bring operational risk through their transitive dependencies: generate an SBOM, scan it in CI with tools such as OWASP Dependency-Check, Dependabot or Renovate, and patch critical CVEs within an agreed time. Plan major framework upgrades, such as Spring Boot 3 to 4, as roadmap items and use automated migration recipes from OpenRewrite. Architecture patterns affect cost most of all: every additional microservice adds a pipeline, dashboards and on-call surface.

9Architecture & Design Considerations

  • Frameworks should serve the architecture: keep domain logic framework-light if long-term independence matters.
  • Technology radar: decide approved libraries/frameworks per concern.
  • Patterns are portable across frameworks and languages — invest in pattern knowledge.
  • Architecture decisions outlive frameworks; record them separately (ADRs).

10Interview Questions & Answers

Q1BasicConcept

What is the difference between a library and a framework?

The difference is who owns the control flow. Your code calls a library when it decides it needs it: you create an ObjectMapper and call readValue, and the library returns control to you. A framework inverts this: it owns the overall flow and calls your code at defined extension points, as Spring's DispatcherServlet calls your controller methods or JUnit discovers and runs your test methods. This is inversion of control. The consequence is coupling: a library sits behind a call site and can usually be wrapped and replaced, while a framework shapes your application's structure, lifecycle and configuration, so replacing it is a migration project rather than a local change.

Q2BasicTricky

Is Spring a pattern?

No. Spring is a framework, code you depend on that calls your code, while a pattern is a reusable design idea with no code to download. The confusion arises because Spring is built from many patterns. The IoC container uses Factory Method and Abstract Factory, beans are container-managed singletons or prototypes, @Transactional and @Cacheable work through proxies, JdbcTemplate is a Template Method with callbacks, DispatcherServlet is a Front Controller using Strategy and Adapter, and ApplicationEventPublisher is Observer. Dependency injection itself is a pattern that Spring implements. Saying we use the Spring pattern therefore mixes levels: you use the Spring framework, which applies these patterns on your behalf.

Q3IntermediateConcept

What is the Hollywood Principle?

The Hollywood Principle, don't call us, we'll call you, describes inversion of control. Instead of application code driving the flow and calling reusable code, the framework owns the main loop, the lifecycle and the call sequence, and calls application code at extension points such as interfaces, callbacks, annotations or configuration. Servlet containers call your filters and servlets, Spring calls your @Bean methods, listeners and controller methods, and JUnit calls @Test and @BeforeEach methods. The benefit is that concerns like request handling, transactions and lifecycle are implemented once in the framework. The cost is that your code must fit the framework's model and timing, which is why understanding its lifecycle matters.

Q4IntermediateConcept

How is a design pattern different from an architecture pattern?

They differ in scope and reversibility. A design pattern such as Strategy, Adapter or Observer structures a few classes and objects to solve a local problem; you can apply or remove it within one module, often in a single refactoring. An architecture pattern such as layered, hexagonal, microservices, event-driven or CQRS structures a whole system or subsystem, defining its components, their responsibilities and how they communicate. It drives quality attributes like scalability, deployability and team autonomy, and it is expensive to reverse because many components, teams and often infrastructure depend on it. Architecture patterns are realised with many design patterns; hexagonal architecture, for example, relies on Adapter at every port.

Q5Senior EngineerInternals

Why does knowing the patterns inside Spring matter?

Because most surprising Spring behaviour is a pattern's consequence. @Transactional, @Cacheable, @Async and method security are implemented with proxies, so a call from one method to another in the same bean bypasses the proxy and silently skips the transaction or cache; knowing it is a Proxy tells you to move the method to another bean or call through the proxy. Default beans are singletons shared by all threads, so they must be stateless or thread-safe. Template Method and callbacks explain JdbcTemplate, and Strategy explains why customisers, converters and handler adapters are the intended extension points. Pattern knowledge turns framework magic into predictable mechanics and makes debugging much faster.

Q6Senior EngineerTrade-off

When would you wrap a library behind an adapter?

I wrap a library behind an application-owned port when replacing or upgrading it later would otherwise ripple through the code. That is the case when it is volatile, with frequent breaking API changes; vendor-specific, like a payment provider or cloud SDK; or used widely in the domain core, where its types would leak into business logic. The adapter translates types and exceptions, such as JsonProcessingException into InvalidJson, so only one class changes on migration and tests can use simple fakes. I do not wrap stable, ubiquitous libraries used at the edges, such as java.time, SLF4J or Jackson inside a controller; the extra layer adds code without reducing real risk.

Q7Technical ArchitectDesign

How deep should framework coupling go in a service?

Ideally the framework stays at the edges. Controllers, persistence adapters, messaging listeners and configuration use Spring freely, while the domain model is plain Java with records, value objects and domain services that can be unit-tested without a Spring context. This is the hexagonal approach: it costs some mapping and wiring, but the core survives framework upgrades and stays easy to test. In practice most teams take a pragmatic middle path, allowing annotations such as @Service and @Transactional in the application layer while keeping the domain framework-free. What matters is that any deeper coupling, such as JPA annotations on domain entities, is a conscious, recorded decision rather than accidental drift.

Q8Solution ArchitectScenario

How do you evaluate adopting a new framework?

I start by asking what control it takes and where: does it own the request lifecycle, the domain model, data access or deployment? Then I assess fit with the target architecture and quality attributes, for example startup time and memory for serverless workloads, native-image support, observability and security. I check ecosystem maturity, release cadence, licence, community and vendor backing, CVE history, and the cost of upgrades and of leaving later. Team skills and hiring matter as much as features. I prove the critical assumptions with a time-boxed spike on a real use case, measure the results, and record the decision, alternatives and exit strategy in an ADR.

Newsletter

Get new tutorials every week

New ArchitectMind tutorials and interview questions, once a week. No spam, unsubscribe anytime.

Almost done! Check your inbox and click the confirmation link to complete your subscription.
Keep going in Book 5

Software Design & Design Patterns

Volume I · Topic 1.1

Software Design

Software Design Foundations & Principles

Volume I · Topic 1.2

Why Software Design Matters

Software Design Foundations & Principles

Volume I · Topic 1.3

Good vs Poor Design

Software Design Foundations & Principles

Volume II · Topic 1.1

Objects

Object-Oriented Design & SOLID

Volume II · Topic 1.2

Classes

Object-Oriented Design & SOLID

Volume II · Topic 1.3

Relationships

Object-Oriented Design & SOLID

Volume III · Topic 1.1

What Is a Design Pattern?

Creational & Structural Design Patterns

Volume III · Topic 1.2

Pattern Categories

Creational & Structural Design Patterns

Volume IV · Topic 1.1

Strategy Pattern

Behavioral Design Patterns

Volume IV · Topic 1.2

Strategy in Java

Behavioral Design Patterns

Volume IV · Topic 1.3

Strategy vs Conditional Logic

Behavioral Design Patterns

Volume V · Topic 1.1

Layered Architecture

Enterprise & Application Design Patterns

Volume V · Topic 1.2

Service Layer

Enterprise & Application Design Patterns

Volume V · Topic 1.3

Repository

Enterprise & Application Design Patterns

Volume VI · Topic 1.1

What Is Refactoring?

Refactoring, Code Quality & Evolutionary Design

Volume VI · Topic 1.2

Why Refactor?

Refactoring, Code Quality & Evolutionary Design

Volume VI · Topic 1.3

Refactoring Safety

Refactoring, Code Quality & Evolutionary Design

Volume VII · Topic 1.1

Method-Level Design

Advanced Design & Architecture-Level Design Decisions

Volume VII · Topic 1.2

Class-Level Design

Advanced Design & Architecture-Level Design Decisions

Volume VII · Topic 1.3

Module-Level Design

Advanced Design & Architecture-Level Design Decisions