Design Pattern
- In Turkish
- Tasarım Deseni
In short
A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
What is a design pattern?
A design pattern is a named, well-tested way of structuring code to solve a problem that comes up again and again. It is not a library or a snippet you copy; it is a description of the idea, the parts involved, and the trade-offs. Shared names like Singleton or Observer also let developers describe a whole design in a single word.
The classic patterns were popularized by the 1994 book Design Patterns, whose four authors are known as the Gang of Four, and are grouped into three families. Creational patterns, such as Factory and Builder, deal with how objects are created. Structural patterns, such as Adapter and Decorator, deal with how objects are combined, and behavioral patterns, such as Observer and Strategy, deal with how objects communicate.
An everyday analogy is a proven kitchen layout: every kitchen is different, but the tried-and-tested arrangement of sink, stove, and fridge can be reused again and again. Patterns also exist beyond classes and objects, for example in front-end frameworks, distributed systems, and at the application level with architectural patterns like MVC.
A design pattern is different from an algorithm. An algorithm is a precise sequence of steps that computes a result, while a pattern is a flexible way of organizing code. Using a pattern where it isn't needed adds complexity, so patterns should solve a real problem rather than be applied for their own sake.
Key takeaways
- A design pattern is a reusable solution template, not ready-made code.
- Classic patterns are creational, structural, or behavioral.
- Pattern names give developers a shared vocabulary.
- Overusing patterns can make simple code needlessly complex.
Example
// Strategy: swap an algorithm without changing the code that uses it
interface ShippingStrategy {
cost(weightKg: number): number;
}
const standard: ShippingStrategy = { cost: (kg) => 5 + kg };
const express: ShippingStrategy = { cost: (kg) => 15 + kg * 2 };
function checkout(weightKg: number, shipping: ShippingStrategy) {
return shipping.cost(weightKg);
}
console.log(checkout(3, standard)); // 8
console.log(checkout(3, express)); // 21Readers ask
What are the most common design patterns?
Frequently used patterns include Singleton, Factory, Builder, Adapter, Decorator, Observer, and Strategy. Many are built into modern languages and frameworks, so you often use them without noticing; event listeners, for example, follow the Observer pattern.
Are design patterns still relevant today?
Yes. Modern language features have made some patterns simpler or unnecessary, but the underlying ideas and the shared vocabulary are still widely used in codebases, frameworks, and technical interviews.
What is the difference between a design pattern and an architectural pattern?
A design pattern solves a problem inside one part of a codebase, such as how a few classes work together. An architectural pattern, such as MVC or microservices, shapes the structure of an entire application or system.
See also
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- SOLIDSoftware Architecture, p. 42SOLID is a set of five object-oriented design principles that help developers write code that is easier to understand, extend, test, and maintain.
- MVCSoftware Architecture, p. 29MVC is an architectural pattern that splits an application into a Model for data and logic, a View for display, and a Controller that handles user input.
- AlgorithmProgramming Fundamentals, p. 2An algorithm is a finite, step-by-step set of instructions for solving a problem or completing a task, such as sorting a list or finding the shortest route.
- RefactoringSoftware Architecture, p. 33Refactoring is the process of restructuring existing code to make it cleaner and easier to maintain without changing what the code does from the outside.
- FrameworkProgramming Fundamentals, p. 20A framework is a reusable foundation of code, tools, and conventions that provides the structure of an application, so developers only fill in their own logic.
- Decorator PatternSoftware Architecture, p. 11The decorator pattern adds behavior like logging or caching to an object by wrapping it in another with the same interface, without changing the original.
- Builder PatternSoftware Architecture, p. 3The builder pattern builds a complex object step by step through a separate builder object, with named steps instead of a long constructor full of arguments.
- IteratorProgramming Fundamentals, p. 31An iterator is an object that lets code step through the items of a collection one at a time, remembering its position, without exposing how the data is stored.
Spotted a mistake or something missing on this page?Suggest an edit